ai icon indicating copy to clipboard operation
ai copied to clipboard

DeepSeek non-streaming crashes with raw TypeError on empty choices

Open soroush5 opened this issue 2 months ago • 5 comments

Hey! Found that the DeepSeek provider's non-streaming doGenerate crashes with a raw TypeError when the API returns empty choices (happens with content filters), instead of a typed AI SDK error like the streaming path and the OpenAI provider produce. Happy to PR a fix if that helps.

soroush5 avatar Sep 18 '26 13:09 soroush5

Fix is open as #21139

soroush5 avatar Sep 18 '26 13:09 soroush5

Identify issue type

Identified as Bug
Confidence: High

Rationale

This describes a concrete error-handling failure in existing provider behavior, with non-streaming generation crashing unexpectedly instead of returning a typed SDK error.

Evidence

  • DeepSeek non-streaming doGenerate throws a raw TypeError when the API returns an empty choices array.
  • The reporter notes that comparable streaming and OpenAI provider paths return a typed AI SDK error instead.
  • A fix was opened in PR #21139.

ai-sdk-factory[bot] avatar Sep 18 '26 13:09 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 7

Bug was reproduced.

Branch: main PR: successful reproduction pull request

Details

  • examples/ai-functions/src/reproduction/deepseek-empty-choices.ts supplies the reported successful non-streaming response with choices: []; doGenerate crashes with a raw TypeError instead of the expected InvalidResponseDataError.
  • packages/deepseek/src/chat/deepseek-chat-language-model.ts dereferences responseBody.choices[0].message without checking whether the first choice exists.
  • The raw exception prevents callers from consistently identifying and handling invalid provider responses through typed AI SDK errors.
  • Direct DeepSeek content-filter probes returned either one refusal choice or HTTP 400, so the provider-side cause of the empty array was not independently observed; the reported AI SDK behavior for that response shape was reproduced.
  • The reproduction passed after temporarily adding the proposed missing-choice guard and rebuilding @ai-sdk/deepseek; the diagnostic product-code change was then reverted.

Relevant Documentation

  • https://api-docs.deepseek.com/api/create-chat-completion/

ai-sdk-factory[bot] avatar Sep 18 '26 14:09 ai-sdk-factory[bot]

Bugfix - AI SDK 7

Created bugfix and opened PR: #21144

Root Cause

The response schema permitted an empty choices array, but doGenerate unconditionally dereferenced choices[0].message. The immutable reproduction confirmed this produced the reported raw TypeError.

Summary

Added a missing-choice guard that throws InvalidResponseDataError with the raw response, added regression coverage, removed reproduction-only artifacts, and added a patch changeset for @ai-sdk/deepseek.

Testing

Added a doGenerate regression test verifying that an empty choices response is rejected with InvalidResponseDataError and the expected message.

ai-sdk-factory[bot] avatar Sep 18 '26 14:09 ai-sdk-factory[bot]

:rocket: Published in:

Package Version
ai 7.0.107 github npm
@ai-sdk/alibaba 2.0.51 github npm
@ai-sdk/amazon-bedrock 5.0.88 github npm
@ai-sdk/angular 3.0.107 github npm
@ai-sdk/anthropic 4.0.58 github npm
@ai-sdk/anthropic-aws 2.0.50 github npm
@ai-sdk/assemblyai 3.0.45 github npm
@ai-sdk/azure 4.0.75 github npm
@ai-sdk/baseten 2.1.31 github npm
@ai-sdk/black-forest-labs 2.0.46 github npm
@ai-sdk/bytedance 2.0.48 github npm
@ai-sdk/cartesia 3.0.40 github npm
@ai-sdk/cerebras 3.0.53 github npm
@ai-sdk/code-mode 1.0.64 github npm
@ai-sdk/cohere 4.0.46 github npm
@ai-sdk/deepgram 3.1.16 github npm
@ai-sdk/deepinfra 3.0.53 github npm
@ai-sdk/deepseek 3.0.49 github npm
@ai-sdk/elevenlabs 3.0.46 github npm
@ai-sdk/fal 3.0.46 github npm
@ai-sdk/fireworks 3.0.56 github npm
@ai-sdk/fish-audio 3.0.23 github npm
@ai-sdk/gateway 4.0.87 github npm
@ai-sdk/gladia 3.0.45 github npm
@ai-sdk/gmicloud 3.0.24 github npm
@ai-sdk/google 4.0.76 github npm
@ai-sdk/google-vertex 5.0.88 github npm
@ai-sdk/groq 4.0.46 github npm
@ai-sdk/harness 1.0.117 github npm
@ai-sdk/harness-acp 1.0.55 github npm
@ai-sdk/harness-claude-code 1.0.121 github npm
@ai-sdk/harness-cline 1.0.44 github npm
@ai-sdk/harness-codex 1.0.119 github npm
@ai-sdk/harness-cursor 1.0.30 github npm
@ai-sdk/harness-deepagents 1.0.117 github npm
@ai-sdk/harness-fx 1.0.30 github npm
@ai-sdk/harness-github-copilot 1.0.12 github npm
@ai-sdk/harness-grok-build 1.0.54 github npm
@ai-sdk/harness-opencode 1.0.119 github npm
@ai-sdk/harness-pi 1.0.119 github npm
@ai-sdk/huggingface 2.0.53 github npm
@ai-sdk/hume 3.0.45 github npm
@ai-sdk/klingai 4.0.47 github npm
@ai-sdk/langchain 3.0.107 github npm
@ai-sdk/llamaindex 3.0.107 github npm
@ai-sdk/luma 3.0.46 github npm
@ai-sdk/mcp 2.0.54 github npm
@ai-sdk/minimax 3.0.35 github npm
@ai-sdk/mistral 4.0.48 github npm
@ai-sdk/moonshotai 3.0.54 github npm
@ai-sdk/open-responses 2.0.49 github npm
@ai-sdk/openai 4.0.71 github npm
@ai-sdk/openai-compatible 3.0.53 github npm
@ai-sdk/otel 1.0.107 github npm
@ai-sdk/perplexity 4.0.48 github npm
@ai-sdk/policy-opa 1.0.107 github npm
@ai-sdk/prodia 2.0.46 github npm
@ai-sdk/provider-utils 5.0.45 github npm
@ai-sdk/quiverai 2.0.45 github npm
@ai-sdk/react 4.0.110 github npm
@ai-sdk/replicate 3.0.46 github npm
@ai-sdk/revai 3.0.45 github npm
@ai-sdk/rsc 3.0.107 github npm
@ai-sdk/sandbox-just-bash 1.0.117 github npm
@ai-sdk/sandbox-vercel 1.0.117 github npm
@ai-sdk/svelte 5.0.107 github npm
@ai-sdk/togetherai 3.0.54 github npm
@ai-sdk/tui 1.0.108 github npm
@ai-sdk/typesafe-ai 3.0.4 github npm
@ai-sdk/valibot 3.0.45 github npm
@ai-sdk/voyage 2.0.45 github npm
@ai-sdk/vue 4.0.107 github npm
@ai-sdk/workflow 2.0.38 github npm
@ai-sdk/workflow-harness 1.0.117 github npm
@ai-sdk/xai 5.0.4 github npm
@ai-sdk/zai 3.0.15 github npm

github-actions[bot] avatar Sep 18 '26 22:09 github-actions[bot]