fix(openai-compatible): inject JSON schema instruction when structuredOutputs disabled#12608
Open
giulio-leone wants to merge 2 commits intovercel:mainfrom
Open
fix(openai-compatible): inject JSON schema instruction when structuredOutputs disabled#12608giulio-leone wants to merge 2 commits intovercel:mainfrom
giulio-leone wants to merge 2 commits intovercel:mainfrom
Conversation
…dOutputs disabled (vercel#12491) When the OpenAI-compatible provider has structuredOutputs disabled (the default), it falls back from json_schema to json_object response format, which silently drops the schema. The model is told to produce JSON but has no knowledge of what schema to produce, leading to validation failures. This fix injects the JSON schema as a system instruction into the prompt messages (using injectJsonInstructionIntoMessages from provider-utils) when the provider cannot enforce the schema via response_format. This gives the model explicit guidance about the expected output structure, matching the reliability of providers that support structured outputs natively. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
a848c87 to
91897c3
Compare
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.
Fixes #12491
Problem
generateText + Output.object() had ~15% failure rate vs generateObject's 100% reliability with OpenAI-compatible providers.
Root Cause
When supportsStructuredOutputs is false (default for openai-compatible), the provider sends response_format: json_object but silently drops the schema - giving the model no knowledge of what JSON structure to produce.
Fix
In openai-compatible-chat-language-model.ts, when falling back to json_object mode, inject the JSON schema as a system instruction using injectJsonInstructionIntoMessages from @ai-sdk/provider-utils. This matches patterns used by other providers (e.g., Mistral).