Skip to content

fix: add optional chaining for finishReason to prevent crashes with gateway#13665

Open
guoyangzhen wants to merge 1 commit intovercel:mainfrom
guoyangzhen:fix/finishReason-null-check
Open

fix: add optional chaining for finishReason to prevent crashes with gateway#13665
guoyangzhen wants to merge 1 commit intovercel:mainfrom
guoyangzhen:fix/finishReason-null-check

Conversation

@guoyangzhen
Copy link

Problem

When using generateText() or generateObject() with Google image generation models through the Vercel AI Gateway, the SDK crashes with:

The gateway may return responses without finishReason for certain model types (e.g. image generation). The SDK accesses result.finishReason.unified without null-checking.

Fix

Add optional chaining (.?.unified) at all 8 locations where finishReason.unified or finishReason.raw is accessed in the core generate-text and generate-object packages. This prevents the crash while preserving normal behavior when finishReason is present.

Files Changed

  • packages/ai/src/generate-object/stream-object.ts (2 locations)
  • packages/ai/src/generate-object/generate-object.ts (4 locations)
  • packages/ai/src/generate-text/generate-text.ts (1 location)
  • packages/ai/src/generate-text/create-stream-text-part-transform.ts (1 location)

@tigent tigent bot added ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. 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 20, 2026
Copy link
Contributor

@vercel vercel bot left a comment

Choose a reason for hiding this comment

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

Additional Suggestion:

Missing optional chaining for finishReason in run-tools-transformation.ts causes streamText to crash when the gateway returns no finishReason.

Fix on Vercel

OnToolCallFinishEvent,
OnToolCallStartEvent,
} from './callback-events';
} from './core-events';
Copy link
Contributor

Choose a reason for hiding this comment

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

Multiple TypeScript compilation errors in generate-text.ts: wrong import path, non-existent generic parameters, and reference to non-existent property.

Fix on Vercel

: InferSchema<SCHEMA>,
>(
options: Omit<CallSettings, 'stopSequences'> &
options: Omit<CallSettings<any>, 'stopSequences'> &
Copy link
Contributor

Choose a reason for hiding this comment

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

CallSettings and TimeoutConfiguration are used as generic types (CallSettings<any>, CallSettings<TOOLS>, TimeoutConfiguration<TOOLS>) but they are not generic, causing TypeScript compilation errors (TS2315).

Fix on Vercel

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

Labels

ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. 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.

1 participant