-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: convert oneOf to anyOf in strict schema for OpenAI compatibility #1884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: convert oneOf to anyOf in strict schema for OpenAI compatibility #1884
Conversation
OpenAI's Structured Outputs API does not support oneOf in nested contexts (e.g., inside array items). Pydantic generates oneOf for discriminated unions, causing validation errors when sending schemas to OpenAI. This change modifies ensure_strict_json_schema() to convert oneOf to anyOf, which provides equivalent functionality for discriminated unions while maintaining OpenAI API compatibility. Fixes openai#1091
|
Greetings to maintainers! I have tried my best to address the issue. If the approach aligns with what you were expecting, that’s great, and if it is not, I’d really appreciate your guidance on how I can improve or make the necessary edits. Thankyou for your time. |
|
Greetings, @seratch. Could you please take a look at this PR when you get a chance? Thank you! |
seratch
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sending this patch. The main code changes look good to me. Could you update the tests as suggested?
tests/test_strict_schema_oneof.py
Outdated
|
|
||
| result = ensure_strict_json_schema(schema) | ||
|
|
||
| assert "oneOf" not in str(result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these assertions are good, but can you have expected: dict and do assertions on the result's dict data structure instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seratch I’ve made the requested changes to use explicit expected dictionaries in the test assertions. Please let me know if any further adjustments or structural changes to the tests are needed, and then I’ll update them as soon as possible. I hope this addresses everything.
OpenAI's Structured Outputs API does not support oneOf in nested contexts (e.g., inside array items). Pydantic generates oneOf for discriminated unions, causing validation errors when sending schemas to OpenAI.
This change modifies ensure_strict_json_schema() to convert oneOf to anyOf, which provides equivalent functionality for discriminated unions while maintaining OpenAI API compatibility.
Fixes #1091