Skip to content

feat: support local refs and defs in tool input schemas#23357

Open
celia-oai wants to merge 4 commits into
mainfrom
dev/cc/ref-def
Open

feat: support local refs and defs in tool input schemas#23357
celia-oai wants to merge 4 commits into
mainfrom
dev/cc/ref-def

Conversation

@celia-oai
Copy link
Copy Markdown
Collaborator

@celia-oai celia-oai commented May 18, 2026

Why

Some connector tool input schemas use local JSON Schema references and definition tables to avoid duplicating large nested shapes. Codex previously lowered these schemas into the supported subset in a way that could discard $ref-only schema objects and lose the corresponding definitions, which made non-strict tool registration less faithful than the original connector schema.

This keeps the existing minimal-lowering policy: Codex still does not raw-pass through arbitrary JSON Schema, but it now preserves local reference structure that fits the Responses-compatible subset and prunes definition entries that cannot be reached by following $refs from the root schema after sanitization, including refs found transitively inside other reachable definitions. The pruning matters because Responses parses definition tables even when entries are unused, so keeping dead definitions wastes prompt tokens.

What changed

  • Added $ref, $defs, and legacy definitions fields to the tool JsonSchema representation.
  • Updated parse_tool_input_schema lowering so $ref-only schema objects survive sanitization instead of becoming {}.
  • Sanitized definition tables recursively and dropped malformed definition tables so non-strict registration degrades gracefully.
  • Added reachability pruning for root definition tables by starting from refs outside definition tables, then following refs inside reachable definitions.
  • Added JSON Pointer decoding for local definition refs such as #/$defs/Foo~1Bar.

Verification

ran local golden-schema probes against representative connector schemas to validate behavior on real generated schemas:

Golden schema Before bytes After bytes $defs before -> after $ref before -> after Result
google_calendar/create_space 7111 4526 7 -> 7 7 -> 7 all definitions preserved because all are reachable
figma/apply_file_variable_changes 4609 999 8 -> 5 8 -> 5 unused defs pruned after unsupported oneOf shapes lower away
snowflake/list_catalog_integrations 1380 404 3 -> 0 0 -> 0 all defs pruned because none are referenced
dropbox/create_shared_link 8894 1836 14 -> 4 9 -> 4 only defs reachable from the root schema after sanitization are retained, including transitively through other retained defs

Token increase across golden schema due to this change:
Screenshot 2026-05-19 at 1 47 04 PM

@celia-oai celia-oai force-pushed the dev/cc/ref-def branch 2 times, most recently from 01661aa to d788814 Compare May 18, 2026 20:31
@celia-oai celia-oai force-pushed the dev/cc/ref-def branch 2 times, most recently from ea2d604 to 82b3b74 Compare May 18, 2026 21:34
@celia-oai celia-oai changed the title changes Support local refs and defs in tool input schemas May 18, 2026
@celia-oai celia-oai marked this pull request as ready for review May 18, 2026 21:57
@celia-oai
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a6f28279e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/tools/src/json_schema.rs Outdated
Comment thread codex-rs/tools/src/json_schema.rs Outdated
Comment thread codex-rs/tools/src/json_schema.rs Outdated
@celia-oai celia-oai force-pushed the dev/cc/ref-def branch 3 times, most recently from 01ea580 to 4a6f282 Compare May 18, 2026 23:13
@celia-oai
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f9bf071758

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +58 to +61
#[serde(rename = "$defs", skip_serializing_if = "Option::is_none")]
pub defs: Option<BTreeMap<String, JsonSchema>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub definitions: Option<BTreeMap<String, JsonSchema>>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Badge Cap preserved schema definitions before exposing them

For MCP/dynamic tools, these newly serialized $defs/definitions become part of the model-visible tool parameters on every request. A connector can return a reachable definition containing a huge enum, description, or nested schema; prune_unreachable_definitions only checks reachability and does not impose any byte/token cap, so this can inject >1k or >10k tokens into context. The context-review rules require hard caps for injected items, so please truncate, reject, or otherwise bound preserved definitions before serialization.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2026-05-19 at 1 47 04 PM Did a pass of token counts increase due to this change and it's not too bad. I think we can leave it as-is for now

Copy link
Copy Markdown
Collaborator Author

@celia-oai celia-oai May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can address the token cap issue separately as a follow-up, but don't think it's blocking this pr

Comment thread codex-rs/tools/src/json_schema.rs
@celia-oai celia-oai requested a review from pakrym-oai May 19, 2026 20:50
@celia-oai celia-oai changed the title Support local refs and defs in tool input schemas feat: support local refs and defs in tool input schemas May 19, 2026
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum DefinitionTable {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this? can we pass the string around like above in sanitize_schema_table(map, "$defs");.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sg

Copy link
Copy Markdown
Collaborator

@pakrym-oai pakrym-oai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a limit on tool size before we merge this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants