Skip to content

Conversation

ruskaruma
Copy link
Contributor

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

ruskaruma and others added 2 commits October 11, 2025 22:33
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
@ruskaruma
Copy link
Contributor Author

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.

@ruskaruma
Copy link
Contributor Author

Greetings, @seratch. Could you please take a look at this PR when you get a chance? Thank you!

@seratch seratch added enhancement New feature or request feature:core labels Oct 14, 2025
@seratch seratch self-requested a review October 14, 2025 02:46
Copy link
Member

@seratch seratch left a 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?


result = ensure_strict_json_schema(schema)

assert "oneOf" not in str(result)
Copy link
Member

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?

Copy link
Contributor Author

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.

@ruskaruma ruskaruma requested a review from seratch October 14, 2025 14:01
@seratch seratch merged commit 75fd791 into openai:main Oct 15, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Union type not permitted in AaaT

2 participants