Skip to content

fix(gateway): guard against missing finishReason in doGenerate response#13712

Open
s-zx wants to merge 1 commit intovercel:mainfrom
s-zx:fix/13626-finishreason-null-guard
Open

fix(gateway): guard against missing finishReason in doGenerate response#13712
s-zx wants to merge 1 commit intovercel:mainfrom
s-zx:fix/13626-finishreason-null-guard

Conversation

@s-zx
Copy link

@s-zx s-zx commented Mar 21, 2026

Summary

When using the Vercel AI gateway with Google image-generation models, the deserialized response body may omit the finishReason field entirely. The existing doGenerate implementation spreads responseBody directly into its return value, so when finishReason is absent the downstream code in generate-text.ts crashes with:

TypeError: Cannot read properties of undefined (reading 'unified')

(line 984: currentModelResponse.finishReason.unified)

Root cause

GatewayLanguageModel.doGenerate performs a simple object spread of the raw gateway response:

return {
  ...responseBody,
  request: { body: args },
  response: { headers: responseHeaders, body: rawResponse },
  warnings,
};

Google image-generation model responses served through the gateway omit finishReason, leaving the field undefined after the spread.

Fix

Add a nullish-coalescing fallback in packages/gateway/src/gateway-language-model.ts so that a missing finishReason defaults to { unified: 'other', raw: undefined }:

return {
  ...responseBody,
  finishReason: responseBody.finishReason ?? {
    unified: 'other',
    raw: undefined,
  },
  request: { body: args },
  response: { headers: responseHeaders, body: rawResponse },
  warnings,
};

This matches the behaviour expected by generate-text.ts and every other provider in the SDK, while remaining a no-op for gateway responses that already supply a finishReason.

Fixes #13626

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
@tigent tigent bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/gateway Issues related to the @ai-sdk/gateway provider provider/google Issues related to the @ai-sdk/google provider labels Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/gateway Issues related to the @ai-sdk/gateway provider provider/google Issues related to the @ai-sdk/google provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generateText crashes with "Cannot read properties of undefined (reading 'unified')" when using gateway with Google image generation models

2 participants