fix: bound $ref expansion in strict JSON schema conversion - #3838
#3838Merged
seratch merged 1 commit intoJul 15, 2026
openai:mainopenai/openai-agents-python:mainfrom
ioleksiuk:fix/strict-schema-ref-expansion-dosioleksiuk/openai-agents-python:fix/strict-schema-ref-expansion-dosCopy head branch name to clipboard
Merged
fix: bound $ref expansion in strict JSON schema conversion#3838seratch merged 1 commit intoopenai:mainopenai/openai-agents-python:mainfrom ioleksiuk:fix/strict-schema-ref-expansion-dosioleksiuk/openai-agents-python:fix/strict-schema-ref-expansion-dosCopy head branch name to clipboard
seratch merged 1 commit into
openai:mainopenai/openai-agents-python:mainfrom
ioleksiuk:fix/strict-schema-ref-expansion-dosioleksiuk/openai-agents-python:fix/strict-schema-ref-expansion-dosCopy head branch name to clipboard
Conversation
`_ensure_strict_json_schema` inlines each `$ref` that has sibling keys and re-recurses into the inlined subtree with no cycle detection or expansion limit. A schema whose `$defs` reference the next level twice therefore expands to 2**N nodes: a few-KB schema balloons to hundreds of MB and pins a CPU core. Because the conversion runs synchronously inside the async run loop, an untrusted schema (for example, a tool schema advertised by a third-party MCP server) can stall the event loop (CWE-400 / CWE-674). Thread a shared node-visit budget through the recursion and raise a UserError once it is exhausted. This turns the pathological case into a fast, bounded error while leaving normal schemas unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ensure_strict_json_schema(used for function-tool, output, and MCP tool schemas whenconvert_schemas_to_strictis enabled) inlines every$refthat has sibling keys and then re-recurses into the inlined subtree, with no cycle detection or expansion limit. A schema whose$defsreference the next level twice therefore expands to2**Nnodes:This threads a shared node-visit budget through the recursion. Once the budget is exhausted, conversion raises a
UserErrorinstead of expanding unbounded, turning the pathological case into a fast, bounded error. Normal schemas are orders of magnitude below the limit and are unaffected.Test plan
test_ref_expansion_bomb_is_rejected: a 30-level$reffan-out (which would inline to2**30nodes) now raisesUserErrorin milliseconds instead of exhausting CPU/memory.$refinlining, chained refs, unions, etc.) are unchanged and pass.make format,make lint,make typecheck, andmake testspass locally.Issue number
N/A
Checks
.agents/skills/code-change-verification/scripts/run.sh