Repository navigation
fix(openai): keep $defs of a recursive output_schema in strict mode - #7806
Closed
Yasser-Ameur wants to merge 1 commit into
Closed
Yasser-Ameur wants to merge 1 commit into
Yasser-Ameur wants to merge 1 commit into
Conversation
A self-referencing output_schema (a tree of Node models) reached
OpenAILlm, OpenAIResponsesLlm and LiteLlm as {"$ref": "#/$defs/Node"},
a reference to a definition that was no longer sent.
Pydantic emits a recursive model as a root {"$defs": ..., "$ref": ...}.
The strict schema pass strips every sibling of a $ref, which at the root
deletes $defs, and returns before recursing.
Inline the referenced definition when a $ref sits beside $defs, so the
root is an object and the $defs it points to are kept and made strict.
The same change goes into both copies of the helper, in
integrations/openai/_openai_schema.py and models/lite_llm.py.
Yasser-Ameur
marked this pull request as ready for review
October 29, 2026 11:24
copybara-service Bot
pushed a commit
that referenced
this pull request
Nov 5, 2026
Merge #7806 A recursive Pydantic output_schema produces a root $ref beside $defs. The strict transform used by OpenAILlm, OpenAIResponsesLlm and LiteLlm's OpenAI route stripped the $defs, sending a dangling reference. The referenced definition is now inlined at the root (following $ref chains, stopping on cycles, keeping root keys) and $defs is kept. The three models now share one copy of the transform. PiperOrigin-RevId: 1014248509
Collaborator
|
Thank you @Yasser-Ameur for your contribution! 🎉 Your changes have been successfully imported and merged via Copybara in commit 245f5d5. Closing this PR as the changes are now in the main branch. |
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
output_schemareaches OpenAI with its$defs.OpenAILlm,OpenAIResponsesLlmandLiteLlm(OpenAI route) send{"$ref": "#/$defs/Node"}, a dangling reference.{"$defs": ..., "$ref": ...}.enforce_strict_openai_schema(integrations/openai/_openai_schema.py) and its copy inmodels/lite_llm.pydelete every sibling of a$refand return, so the root loses$defs.LlmAgent(output_schema=Node)withNode.children: list[Node]onOpenAILlmwith a mocked client; inspectresponse_format.jsonschemafails withPointerToNowhere: '/$defs/Node'.Solution: when a
$refsits beside$defsand names an entry there, inline that definition (deep copy) into the node and keep$defs, then run the normal strict pass. The root becomes an object, as OpenAI strict mode needs. Nested$refsibling stripping is unchanged. Same change in both copies.Testing Plan
Unit Tests:
test_enforce_strict_openai_schema_inlines_root_refintest_litellm.pyandtest_openai_responses_llm.py; both fail on main.pytest tests/unittests/models/test_litellm.py tests/unittests/integrations/openai:996 passed, 4 warnings in 14.95s.Manual End-to-End (E2E) Tests:
No live OpenAI call was made. A script with a mocked
AsyncOpenAIclient was run on main and on this branch, capturing the sentresponse_format.Before:
{"schema": {"$ref": "#/$defs/Node"}},jsonschemaraisesPointerToNowhere.After: object root with
additionalProperties: false,required: ["children", "name"],$defs.Nodemade strict,children.itemsstill{"$ref": "#/$defs/Node"};jsonschemareports valid.Checklist
Additional context
The OpenAI SDK's own strict helper (
openai/lib/_pydantic.py) inlines a$refthat has siblings the same way.