fix(gateway): guard against missing finishReason in doGenerate response#13712
Open
s-zx wants to merge 1 commit intovercel:mainfrom
Open
fix(gateway): guard against missing finishReason in doGenerate response#13712s-zx wants to merge 1 commit intovercel:mainfrom
s-zx wants to merge 1 commit intovercel:mainfrom
Conversation
When using the gateway with Google image generation models, the
deserialized response body may omit the finishReason field. Spreading
the raw responseBody directly then causes generate-text.ts to crash
with "Cannot read properties of undefined (reading 'unified')".
Add a nullish-coalescing fallback so that a missing finishReason
defaults to { unified: 'other', raw: undefined }, matching the
behaviour expected by downstream consumers.
Fixes vercel#13626
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.
Summary
When using the Vercel AI gateway with Google image-generation models, the deserialized response body may omit the
finishReasonfield entirely. The existingdoGenerateimplementation spreadsresponseBodydirectly into its return value, so whenfinishReasonis absent the downstream code ingenerate-text.tscrashes with:(line 984:
currentModelResponse.finishReason.unified)Root cause
GatewayLanguageModel.doGenerateperforms a simple object spread of the raw gateway response:Google image-generation model responses served through the gateway omit
finishReason, leaving the fieldundefinedafter the spread.Fix
Add a nullish-coalescing fallback in
packages/gateway/src/gateway-language-model.tsso that a missingfinishReasondefaults to{ unified: 'other', raw: undefined }:This matches the behaviour expected by
generate-text.tsand every other provider in the SDK, while remaining a no-op for gateway responses that already supply afinishReason.Fixes #13626