Skip to content

fix(anthropic): throw error in doStream when the first stream chunk is an error#10653

Merged
lgrammel merged 9 commits intomainfrom
lg/CMCXFuwT
Nov 27, 2025
Merged

fix(anthropic): throw error in doStream when the first stream chunk is an error#10653
lgrammel merged 9 commits intomainfrom
lg/CMCXFuwT

Conversation

@lgrammel
Copy link
Collaborator

@lgrammel lgrammel commented Nov 27, 2025

Background

The Anthropic API sends a 200 responses for streaming requests when overloaded, and includes error chunks in the stream. This breaks the AI SDK retry mechanism which relies on HTTP status codes and errors. While this cannot be solved in general, because the expected state is unclear when such an error happens mid-stream, it can be solved when the first chunk is the error chunk (which may be the majority of cases).

Summary

  • move DelayedPromise into provider utils
  • throw error from Anthropic language model doStream if first chunk is an error

Manual Verification

Cannot verify overload error end to end (Anthropic API would need to be overloaded).

  • Run examples/ai-core/src/stream-text/anthropic.ts to ensure streaming still works

Future Work

  • remove special case in case Anthropic switches to sending the correct http status codes

Related Issues

Fixes #2453

@lgrammel lgrammel changed the title Lg/cmcx fuw t fix(anthropic): throw error in doStream when the first stream chunk is an error Nov 27, 2025
@lgrammel lgrammel marked this pull request as ready for review November 27, 2025 11:16
@lgrammel lgrammel merged commit 03849b0 into main Nov 27, 2025
21 checks passed
@lgrammel lgrammel deleted the lg/CMCXFuwT branch November 27, 2025 11:25
vercel-ai-sdk bot pushed a commit that referenced this pull request Nov 27, 2025
@vercel-ai-sdk vercel-ai-sdk bot removed the backport label Nov 27, 2025
@vercel-ai-sdk
Copy link
Contributor

vercel-ai-sdk bot commented Nov 27, 2025

⚠️ Backport to release-v5.0 created but has conflicts: #10655

lgrammel added a commit that referenced this pull request Nov 27, 2025
…am chunk is an error (#10655)

This is a backport of #10653 to the release-v5.0 branch.

---------

Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ungraceful failure error when the anthropic api is overloaded

1 participant