Skip to content

fix(provider): throw NoContentGeneratedError when a response has no choices - #500

Open
shoemoney wants to merge 1 commit into
stripe:mainfrom
shoemoney:fix/empty-choices-guard
Open

fix(provider): throw NoContentGeneratedError when a response has no choices#500
shoemoney wants to merge 1 commit into
stripe:mainfrom
shoemoney:fix/empty-choices-guard

Conversation

@shoemoney

Copy link
Copy Markdown

openAIResponseSchema allows an empty choices array, so an HTTP 200 response of {"choices": [], "usage": {...}} crashes doGenerate with an unhandled TypeError at response.choices[0].message in both StripeLanguageModel (V2) and StripeLanguageModelV3. The dereference sits outside the try/catch, which only wraps postJsonToApi, so callers get a raw TypeError instead of a typed SDK error.

This adds an explicit guard after response.choices[0] in both models that throws NoContentGeneratedError from @ai-sdk/provider, the SDK's standard error for a response with no generated content, with a clear message.

Tests: one regression test per model asserting a 200 response with empty choices rejects with NoContentGeneratedError. Both fail with TypeError before the fix and pass after. Full suite: 331 passing.

openAIResponseSchema allows an empty choices array, so a 200 response
with no choices crashed doGenerate with an unhandled TypeError when
dereferencing response.choices[0].message. Throw NoContentGeneratedError
with a clear message instead, in both the V2 and V3 language models.

Adds a regression test per model: a 200 response with empty choices now
produces NoContentGeneratedError rather than a TypeError.
@cla-assistant

cla-assistant Bot commented Aug 23, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant