ai icon indicating copy to clipboard operation
ai copied to clipboard

Groq non-streaming crashes with raw TypeError on empty choices

Open soroush5 opened this issue 2 months ago • 5 comments

Hey! Found that the Groq provider's non-streaming doGenerate crashes with a raw TypeError when the API returns empty choices, instead of a typed AI SDK error like the streaming path produces. Happy to PR a fix if that helps.

soroush5 avatar Sep 19 '26 17:09 soroush5

Identify issue type

Identified as Bug
Confidence: High

Rationale

This describes a concrete inconsistency and unexpected crash in existing provider behavior, so it is a bug rather than a request for new functionality.

Evidence

  • Groq's non-streaming doGenerate reportedly crashes when the API returns an empty choices array.
  • The failure surfaces as a raw TypeError, while the streaming path produces a typed AI SDK error.

ai-sdk-factory[bot] avatar Sep 19 '26 17:09 ai-sdk-factory[bot]

Fix is open as #21184

soroush5 avatar Sep 19 '26 17:09 soroush5

Bug Reproduction - AI SDK 7

Bug was reproduced.

Branch: main PR: successful reproduction pull request

Details

  • Groq non-streaming doGenerate dereferences choices[0].message when a successful response contains choices: [], producing raw TypeError: Cannot read properties of undefined (reading 'message') instead of the expected InvalidResponseDataError.
  • examples/ai-functions/src/reproduction/groq-empty-choices-type-error.ts reproduces the user-visible failure and exits 1 only for the reported raw TypeError.
  • packages/groq/src/__fixtures__/groq-empty-choices.json and packages/groq/src/groq-chat-language-model.test.ts add a regression fixture and assertion requiring the typed error; the assertion fails on main with the reported TypeError.
  • A live Groq request using openai/gpt-oss-20b succeeded with one choice; the conditional empty-choices response was therefore replayed through the provider's fetch boundary.
  • The reported streaming comparison was not confirmed for an equivalent empty-choices SSE chunk: it completed with empty text rather than producing a typed error.

Relevant Documentation

  • https://console.groq.com/docs/api-reference
  • https://console.groq.com/docs/errors

ai-sdk-factory[bot] avatar Sep 19 '26 17:09 ai-sdk-factory[bot]

Bugfix - AI SDK 7

Created bugfix and opened PR: #21186

Root Cause

The response schema allowed empty choices arrays, but doGenerate dereferenced choices[0].message without verifying that a choice existed; the immutable reproduction confirmed this exact raw TypeError.

Summary

Added an empty-choice guard that throws InvalidResponseDataError, removed reproduction-only artifacts, and added a patch changeset for @ai-sdk/groq.

Testing

Added a regression test requiring InvalidResponseDataError with the expected message for an empty choices response.

ai-sdk-factory[bot] avatar Sep 19 '26 17:09 ai-sdk-factory[bot]

:rocket: Published in:

Package Version
ai 7.0.112 github npm
@ai-sdk/alibaba 2.0.53 github npm
@ai-sdk/amazon-bedrock 5.0.92 github npm
@ai-sdk/angular 3.0.112 github npm
@ai-sdk/anthropic 4.0.61 github npm
@ai-sdk/anthropic-aws 2.0.53 github npm
@ai-sdk/assemblyai 3.0.46 github npm
@ai-sdk/azure 4.0.77 github npm
@ai-sdk/baseten 2.1.32 github npm
@ai-sdk/black-forest-labs 2.0.47 github npm
@ai-sdk/bytedance 2.0.49 github npm
@ai-sdk/cartesia 3.0.41 github npm
@ai-sdk/cerebras 3.0.54 github npm
@ai-sdk/code-mode 1.0.69 github npm
@ai-sdk/cohere 4.0.47 github npm
@ai-sdk/deepgram 3.1.17 github npm
@ai-sdk/deepinfra 3.0.54 github npm
@ai-sdk/deepseek 3.0.51 github npm
@ai-sdk/devtools 1.0.23 github npm
@ai-sdk/elevenlabs 3.0.47 github npm
@ai-sdk/fal 3.0.47 github npm
@ai-sdk/fireworks 3.0.57 github npm
@ai-sdk/fish-audio 3.0.24 github npm
@ai-sdk/gateway 4.0.90 github npm
@ai-sdk/gladia 3.0.46 github npm
@ai-sdk/gmicloud 3.0.25 github npm
@ai-sdk/google 4.0.78 github npm
@ai-sdk/google-vertex 5.0.91 github npm
@ai-sdk/groq 4.0.47 github npm
@ai-sdk/harness 1.0.122 github npm
@ai-sdk/harness-acp 1.0.60 github npm
@ai-sdk/harness-claude-code 1.0.126 github npm
@ai-sdk/harness-cline 1.0.49 github npm
@ai-sdk/harness-codex 1.0.124 github npm
@ai-sdk/harness-cursor 1.0.35 github npm
@ai-sdk/harness-deepagents 1.0.122 github npm
@ai-sdk/harness-fx 1.0.35 github npm
@ai-sdk/harness-github-copilot 1.0.17 github npm
@ai-sdk/harness-grok-build 1.0.59 github npm
@ai-sdk/harness-opencode 1.0.124 github npm
@ai-sdk/harness-pi 1.0.124 github npm
@ai-sdk/huggingface 2.0.54 github npm
@ai-sdk/hume 3.0.46 github npm
@ai-sdk/klingai 4.0.48 github npm
@ai-sdk/langchain 3.0.112 github npm
@ai-sdk/llamaindex 3.0.112 github npm
@ai-sdk/luma 3.0.47 github npm
@ai-sdk/mcp 2.0.56 github npm
@ai-sdk/minimax 3.0.38 github npm
@ai-sdk/mistral 4.0.49 github npm
@ai-sdk/moonshotai 3.0.55 github npm
@ai-sdk/open-responses 2.0.50 github npm
@ai-sdk/openai 4.0.73 github npm
@ai-sdk/openai-compatible 3.0.54 github npm
@ai-sdk/otel 1.0.112 github npm
@ai-sdk/perplexity 4.0.49 github npm
@ai-sdk/policy-opa 1.0.112 github npm
@ai-sdk/prodia 2.0.47 github npm
@ai-sdk/provider 4.0.18 github npm
@ai-sdk/provider-utils 5.0.46 github npm
@ai-sdk/quiverai 2.0.46 github npm
@ai-sdk/react 4.0.115 github npm
@ai-sdk/replicate 3.0.47 github npm
@ai-sdk/revai 3.0.46 github npm
@ai-sdk/rsc 3.0.112 github npm
@ai-sdk/sandbox-just-bash 1.0.122 github npm
@ai-sdk/sandbox-vercel 1.0.122 github npm
@ai-sdk/svelte 5.0.112 github npm
@ai-sdk/togetherai 3.0.55 github npm
@ai-sdk/tui 1.0.113 github npm
@ai-sdk/typesafe-ai 3.0.5 github npm
@ai-sdk/valibot 3.0.46 github npm
@ai-sdk/voyage 2.0.46 github npm
@ai-sdk/vue 4.0.112 github npm
@ai-sdk/workflow 2.0.43 github npm
@ai-sdk/workflow-harness 1.0.122 github npm
@ai-sdk/xai 5.0.6 github npm
@ai-sdk/zai 3.0.16 github npm

github-actions[bot] avatar Sep 23 '26 08:09 github-actions[bot]