Skip to content

fix(openai): keep $defs of a recursive output_schema in strict mode - #7806

Closed
Yasser-Ameur wants to merge 1 commit into
google:mainfrom
Yasser-Ameur:fix/strict-schema-root-ref
Closed

Yasser-Ameur wants to merge 1 commit into
google:mainfrom
Yasser-Ameur:fix/strict-schema-root-ref

Conversation

@Yasser-Ameur

Copy link
Copy Markdown
Contributor

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):

  • Closes: none, no issue exists
  • Related: none

2. Or, if no issue exists, describe the change:

Problem:

  • Expected: a recursive pydantic output_schema reaches OpenAI with its $defs.
  • Actual: OpenAILlm, OpenAIResponsesLlm and LiteLlm (OpenAI route) send {"$ref": "#/$defs/Node"}, a dangling reference.
  • Cause: pydantic emits a recursive model as a root {"$defs": ..., "$ref": ...}. enforce_strict_openai_schema (integrations/openai/_openai_schema.py) and its copy in models/lite_llm.py delete every sibling of a $ref and return, so the root loses $defs.
  • Reproduction: LlmAgent(output_schema=Node) with Node.children: list[Node] on OpenAILlm with a mocked client; inspect response_format. jsonschema fails with PointerToNowhere: '/$defs/Node'.
  • Version: origin/main 08c289b, Python 3.14.7, pydantic v2.

Solution: when a $ref sits beside $defs and 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 $ref sibling stripping is unchanged. Same change in both copies.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change: test_enforce_strict_openai_schema_inlines_root_ref in test_litellm.py and test_openai_responses_llm.py; both fail on main.
  • All unit tests pass locally.

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 AsyncOpenAI client was run on main and on this branch, capturing the sent response_format.
Before: {"schema": {"$ref": "#/$defs/Node"}}, jsonschema raises PointerToNowhere.
After: object root with additionalProperties: false, required: ["children", "name"], $defs.Node made strict, children.items still {"$ref": "#/$defs/Node"}; jsonschema reports valid.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end. (mocked client only, see above)
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The OpenAI SDK's own strict helper (openai/lib/_pydantic.py) inlines a $ref that has siblings the same way.

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
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
@adk-bot

adk-bot commented Nov 5, 2026

Copy link
Copy Markdown
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.

@adk-bot adk-bot added the merged [Status] This PR is merged label Nov 5, 2026
@adk-bot adk-bot closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants