OpenAI structure output union #186
TanStack AI version#
0.2.0
Framework/Library version#
vanilla 0.2.0
Describe the bug and the steps to reproduce it#
Structured output doesn't support for unions:
chat({
adapter: openaiText("gpt-4o-mini"),
messages: [{ role: "user", content: "..." }],
outputSchema: z.object({
u: z.union([
z.object({ a: z.string() }),
z.object({ b: z.number() })
]),
}),
});
// throw {
// message: "Invalid schema for response_format 'structured_output': In context=('properties', 'u', 'anyOf', '0'), 'additionalProperties' is required to be supplied and to be false.",
// type: "invalid_request_error",
// param: "text.format.schema",
// code: "invalid_json_schema",
// }
The issue seems to come from openAI format converter: https://github.com/TanStack/ai/blob/a8667216fd081be1629f47a531ff3a39052b1156/packages/typescript/ai-openai/src/utils/schema-converter.ts#L48
It doesn't handle the JSON schema anyOf object like:
{
"anyOf": [
// ...
]
}
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
Screenshots or Videos (Optional)#
No response
Do you intend to try to help solve this bug with your own PR?#
None
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
I'm using the latest version,
zod@4.2.1If you don't include the union it works?
Yes, without the union it works
When I look at the
makeOpenAIStructuredOutputCompatiblefunction, which is adding theadditionalPropertieskey: https://github.com/TanStack/ai/blob/a8667216fd081be1629f47a531ff3a39052b1156/packages/typescript/ai-openai/src/utils/schema-converter.ts#L97-L98It does nothing on the
anyOfblock and the receive version doesn't include theadditionalPropertiesas it is generated here, viainputwhich create the schema in loose mode: https://github.com/TanStack/ai/blob/a8667216fd081be1629f47a531ff3a39052b1156/packages/typescript/ai/src/activities/chat/tools/schema-converter.ts#L209-L211can you try with this PR: https://github.com/TanStack/ai/pull/188 it will publish the pkg.pr.new versions soon
It works but fails with discrimited unions like:
await chat({ // ... outputSchema: z.object({ u: z.discriminatedUnion("type", [ z.object({ type: z.literal("A"), a: z.string() }), z.object({ type: z.literal("B"), b: z.number() }), ]), }), }), // throw { // message: "Invalid schema for response_format 'structured_output': In context=('properties', 'u'), 'oneOf' is not permitted.", // type: "invalid_request_error", // param: "text.format.schema", // code: "invalid_json_schema", // }Do you think we can handle this? Like mapping
oneOftoanyOf?To be honest I'd like to be in line with their spec, matching oneOf as anyOf feels hacky in the library itself, on the user side sure because you know what you're doing and the behavior is expected.
as per this answer: https://stackoverflow.com/a/34038277/13417096 the behavior is very similar but not identical so I'd rather have people do it on their side than us internally, but if openai adds support I'm completely fine to add it in
Ok it makes sens, maybe at least add an error / warning in this case so people don't loose too much time to investigate the root cause?
Anyway thanks for being so quick! 🚀
Alright, added a throw in oneOf cases to a link to the docs, if they change it we will hopefully get a bug report to add it back in!
AlemTuzlak closed this
Sign in to join this conversation.
@hugo082 i've had this same bug the other day, it turned out it was because i was not using zod v4.2+, can you update and try again?