feat(provider/google): support JSON Schema for structured outputs via useResponseJsonSchema - #18325
Open
onatozmenn wants to merge 1 commit into
Open
feat(provider/google): support JSON Schema for structured outputs via useResponseJsonSchema#18325onatozmenn wants to merge 1 commit into
useResponseJsonSchema#18325onatozmenn wants to merge 1 commit into
Conversation
Send structured output schemas via generationConfig.responseJsonSchema instead of the OpenAPI 3.0 subset, enabling unions, records and recursive schemas on Gemini 2.5+.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
Fixes #6494.
Gemini structured outputs currently go out as
generationConfig.responseSchema, which is a subset of the OpenAPI 3.0 spec rather than JSON Schema. That subset does not cover unions or records, soz.union/z.recordschemas either fail with errors likeresponse_schema.properties[occupation].type: must be specifiedor force users to disable structured outputs entirely (structuredOutputs: false), losing constrained decoding.Gemini 2.5 and later support submitting JSON Schema through
generationConfig.responseJsonSchema, which is mutually exclusive withresponseSchemaand requiresresponseMimeType(API reference, guide).Summary
Adds a
useResponseJsonSchemaprovider option to@ai-sdk/google:true, the response format schema is sent unchanged asgenerationConfig.responseJsonSchemainstead of being converted to the OpenAPI subset forgenerationConfig.responseSchemaresponseMimeTypekeeps being set as beforefalse, so existing requests are unchangedstructuredOutputs: falsestill wins and suppresses both fieldsThis unblocks unions, records and recursive schemas for object generation on Gemini 2.5+ without giving up structured outputs.
The schema is forwarded as-is (including
$schema), matching how the officialjs-genaiSDK routes JSON Schema payloads to this field.Contributor Credit
End-to-End Verification
Verified through the provider request-body tests, which assert the exact JSON sent to
:generateContent:useResponseJsonSchema: trueproducesgenerationConfig.responseJsonSchemawith the untouchedanyOfunion schema and noresponseSchemauseResponseJsonSchema: truecombined withstructuredOutputs: falsesends neither schema fieldpnpm --filter @ai-sdk/google exec vitest --config vitest.node.config.js --run src/google-language-model.test.ts→ 177 passed. Type check andoxlinton the touched files are clean.Checklist
pnpm changesetin the project root)Future Work
The Gemini API exposes the same escape hatch for tool inputs via
FunctionDeclaration.parametersJsonSchema. Wiring that up would let tool input schemas use unions and records too; it was left out here to keep this change focused on structured outputs.