Skip to content

fix(strict_schema): guard resolve_ref against missing $ref path#3552

Closed
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/keyerror-missing-guard-in-resolve-ref
Closed

fix(strict_schema): guard resolve_ref against missing $ref path#3552
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/keyerror-missing-guard-in-resolve-ref

Conversation

@devteamaegis
Copy link
Copy Markdown

What's broken

resolve_ref in strict_schema.py does a bare resolved[key] subscript with no KeyError guard. When ensure_strict_json_schema encounters a property with both a $ref and a sibling key (e.g. description), it unravels the ref via resolve_ref. If the referenced path does not exist — e.g. a hand-crafted params_json_schema with a $ref but no $defs section — the loop crashes with KeyError: '$defs' instead of a clear diagnostic. The adjacent code already raises ValueError for malformed $ref strings, but a well-formatted ref pointing to a missing path was unguarded.

Why it happens

The for key in path loop in resolve_ref (line 158–163) subscripts resolved[key] directly without catching KeyError.

Fix

Wrapped the subscript in a try/except KeyError that re-raises as ValueError naming the missing key and the offending $ref, consistent with the existing error handling pattern in the same function.

Test

test_resolve_ref_raises_valueerror_for_missing_path in tests/test_strict_schema.py calls ensure_strict_json_schema with a schema that has a $ref+description property but no $defs, and asserts a ValueError is raised with the expected message.

Fixes #3551

…e = resolved[key]`

Replace bare subscript with try/except KeyError that raises a descriptive
ValueError when a $ref points to a missing path in the schema, consistent
with the existing ValueError for malformed $ref formats.
@seratch seratch added feature:core wontfix This will not be worked on labels Jun 4, 2026
@seratch seratch closed this Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature:core wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: resolve_ref bare dict subscript raises KeyError for missing $ref path

2 participants