Skip to content
Source
Sign inSign up
TanStack/ai

OpenAI structure output union #186

Closedhugo082(GitHub profile, opens in a new tab) opened this issue 10 comments

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": [
    // ...
  ]
}

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.
  1. @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?

  2. I'm using the latest version, zod@4.2.1

  3. If you don't include the union it works?

  4. hugo082(GitHub profile, opens in a new tab)commented

    Yes, without the union it works

    When I look at the makeOpenAIStructuredOutputCompatible function, which is adding the additionalProperties key: https://github.com/TanStack/ai/blob/a8667216fd081be1629f47a531ff3a39052b1156/packages/typescript/ai-openai/src/utils/schema-converter.ts#L97-L98

    It does nothing on the anyOf block and the receive version doesn't include the additionalProperties as it is generated here, via input which create the schema in loose mode: https://github.com/TanStack/ai/blob/a8667216fd081be1629f47a531ff3a39052b1156/packages/typescript/ai/src/activities/chat/tools/schema-converter.ts#L209-L211

  5. can you try with this PR: https://github.com/TanStack/ai/pull/188 it will publish the pkg.pr.new versions soon

  6. hugo082(GitHub profile, opens in a new tab)commented

    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 oneOf to anyOf?

  7. 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.

  8. 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

  9. 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! 🚀

  10. 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!

  11. AlemTuzlak closed this

Sign in to join this conversation.