Skip to content

fix(py/openai): wrap non-object output schemas for OpenAI and unwrap the reply - #179

Open
adesinah wants to merge 3 commits into
mainfrom
fix/openai-non-object-root
Open

adesinah wants to merge 3 commits into
mainfrom
fix/openai-non-object-root

Conversation

@adesinah

@adesinah adesinah commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Python port of genkit-ai/genkit#7316 (JS), genkit-ai/genkit#7320 (Go) and genkit-ai/genkit-dart#766 (Dart). Refs genkit-ai/genkit#7238.

What

With OpenAI(), an output schema whose root is not an object (a list, enum or scalar) now works. The schema is sent as the value property of an object, and the reply is unwrapped before Genkit parses it, so output and the streamed chunks hold the caller's value.

response = await ai.generate(model='openai/gpt-4o-mini', prompt='Pick a number.', output_schema=int)
response.output  # 7

Object roots, DeepSeek, Mistral and other users of OpenAIModel (Vertex Model Garden) send the same request as before. A reply is unwrapped only when value is its one key. Anything else, such as a host that ignored the wrapper, comes back as sent.

Why

OpenAI's json_schema response format accepts only an object root. On main, any other root fails with:

Invalid schema for response_format 'output': schema must be a JSON Schema of 'type: "object"', got 'type: "array"'.

Local references (#, #/items/...) move into $defs when the schema is wrapped, as in JS and Go. Otherwise they would point at the wrapper, or fail the SDK's strict pass.

Testing

  • uv run pytest packages/genkit-openai. ruff and ty are clean, and pyrefly reports only the error already on main.
  • Live against gpt-4o-mini, streaming and not: array, str Enum, scalars (falsy values included) and type: ['object', 'null'] roots return the value. Object roots are unchanged.

Not in this PR

  • Core rejects list[Model] and list[str] as output_schema before the request is built. That is separate from this plugin.

@adesinah
adesinah requested a review from cabljac November 24, 2026 17:56

@cabljac cabljac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments/questions

Comment thread packages/genkit-openai/README.md Resolved
@cabljac
cabljac self-requested a review November 30, 2026 11:16
…Mistral

- The output wrapper moves '#' and '#/...' references into $defs and keeps
  $schema at the root, as JS and Go do.
- A reply is unwrapped only when value is its one key, as in Dart.
- The final streamed message is built from the raw text and unwrapped as a
  full reply is.
- Mistral requests are no longer wrapped.
- Restore the tool-schema helper names.
@adesinah
adesinah force-pushed the fix/openai-non-object-root branch from 303a365 to efc4bf0 Compare November 30, 2026 13:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants