Skip to content

fix: prevent Anthropic streams from merging overlapping message generations - #18402

Merged
gr2m merged 2 commits into
mainfrom
bugfix-18331-20260804073338085325
Aug 5, 2026
Merged

fix: prevent Anthropic streams from merging overlapping message generations#18402
gr2m merged 2 commits into
mainfrom
bugfix-18331-20260804073338085325

Conversation

@ai-sdk-factory

@ai-sdk-factory ai-sdk-factory Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Background

Spliced Anthropic streams silently merged separate generations, corrupting signed thinking history and causing subsequent tool-loop requests to fail.

Root Cause

The Anthropic stream transform did not track whether a message was open or its ID; reproduction showed a second message_start overwrote metadata while existing content state remained, merging both generations.

Summary

Added message lifecycle tracking that ignores same-ID duplicate starts, emits InvalidResponseDataError for a different message while one is open, and suppresses remaining invalid stream events.

Testing

Added Node and Edge regression coverage for spliced generations and benign duplicate message_start events, while preserving sequential programmatic tool-calling streams.

End-to-end Validation

  • pnpm -C packages/anthropic build followed by the reproduction script: emitted one InvalidResponseDataError, no second-message metadata or finish event, and only one metadata event for the same-ID duplicate.

Related Issues

Fixes #18331

Closes #18397

ai-sdk-factory and others added 2 commits August 4, 2026 07:34
Co-authored-by: Lars Grammel <205036+lgrammel@users.noreply.github.com>
Co-authored-by: ro-hansolo <87645076+ro-hansolo@users.noreply.github.com>
Co-authored-by: Lars Grammel <205036+lgrammel@users.noreply.github.com>
@ai-sdk-factory

ai-sdk-factory Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Bugfix review

Outcome: approved

Reproduction replay

Status: no-longer-reproduces

The exact original reproduction completed successfully and the original bug signal did not appear.

Fixes issue

Status: fully-addresses

The parser now ignores a same-ID duplicate start, emits InvalidResponseDataError for a different ID while a message is open, and suppresses subsequent events so no second-message finish or tool call is produced. This follows Anthropic's documented message_start-to-message_stop lifecycle. (platform.claude.com)

Side effects

Risk: low

Valid sequential message sequences remain accepted because lifecycle state resets at message_stop, while only overlapping malformed generations change behavior. Existing multi-message programmatic tool-calling tests pass.

Performance

Risk: low

The implementation adds only three scalar state variables and a constant-time check per stream chunk, with no new buffering, collections, or unbounded retention.

Backwards compatibility

Risk: low

Existing stored histories and persisted formats are unchanged; the new error path prevents an invalid overlapping stream from being finalized into corrupted history. This protects the requirement that thinking blocks be replayed complete and unmodified. (platform.claude.com)

Breaking changes

Risk: none

No public APIs, types, exports, accepted request inputs, configuration, defaults, or persisted formats change. Only malformed overlapping streams and benign duplicate metadata behavior receive the intended corrected error/output semantics.

Architecture

Risk: low

The lifecycle validation is localized to the Anthropic stream parser and uses InvalidResponseDataError through the declared @ai-sdk/provider dependency; no package boundary, dependency direction, export, or cross-package src access is introduced.

Change scope

Status: minimal

Every changed file supports the fix: localized parser state, two regression fixtures, lifecycle tests, and the required patch changeset. No unrelated production changes or speculative abstractions are present.

Security

Risk: low

The change adds no external input execution, credential handling, network behavior, parsing primitive, or new dependency; failing closed on an invalid stream reduces corrupted-history risk.

Testing

Status: appropriate

Tests cover the reported different-ID splice and same-ID duplicate, asserting error type, metadata IDs, suppression of the second tool call, and absence of finish. Existing sequential programmatic tool-calling coverage also passes in Node and Edge suites.

Verification

Inspected the complete merge-base diff and all changed files; verified package architecture and exports; package Node and Edge suites each passed all 476 tests, workspace type checking passed, the Anthropic package build passed, and diff validation found no formatting errors.

Relevant Documentation

@gr2m
gr2m merged commit 8b96941 into main Aug 5, 2026
64 checks passed
@gr2m
gr2m deleted the bugfix-18331-20260804073338085325 branch August 5, 2026 21:40
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

🚀 Published in:

Package Version
ai 7.0.54 github npm
@ai-sdk/alibaba 2.0.26 github npm
@ai-sdk/amazon-bedrock 5.0.45 github npm
@ai-sdk/angular 3.0.54 github npm
@ai-sdk/anthropic 4.0.32 github npm
@ai-sdk/anthropic-aws 2.0.24 github npm
@ai-sdk/assemblyai 3.0.22 github npm
@ai-sdk/azure 4.0.32 github npm
@ai-sdk/baseten 2.1.2 github npm
@ai-sdk/black-forest-labs 2.0.23 github npm
@ai-sdk/bytedance 2.0.24 github npm
@ai-sdk/cartesia 3.0.17 github npm
@ai-sdk/cerebras 3.0.24 github npm
@ai-sdk/code-mode 1.0.11 github npm
@ai-sdk/cohere 4.0.22 github npm
@ai-sdk/deepgram 3.0.22 github npm
@ai-sdk/deepinfra 3.0.24 github npm
@ai-sdk/deepseek 3.0.23 github npm
@ai-sdk/elevenlabs 3.0.23 github npm
@ai-sdk/fal 3.0.23 github npm
@ai-sdk/fireworks 3.0.26 github npm
@ai-sdk/fish-audio 3.0.0 github npm
@ai-sdk/gateway 4.0.42 github npm
@ai-sdk/gladia 3.0.22 github npm
@ai-sdk/google 4.0.35 github npm
@ai-sdk/google-vertex 5.0.43 github npm
@ai-sdk/groq 4.0.23 github npm
@ai-sdk/harness 1.0.60 github npm
@ai-sdk/harness-claude-code 1.0.61 github npm
@ai-sdk/harness-codex 1.0.62 github npm
@ai-sdk/harness-deepagents 1.0.59 github npm
@ai-sdk/harness-opencode 1.0.61 github npm
@ai-sdk/harness-pi 1.0.60 github npm
@ai-sdk/huggingface 2.0.24 github npm
@ai-sdk/hume 3.0.22 github npm
@ai-sdk/klingai 4.0.24 github npm
@ai-sdk/langchain 3.0.54 github npm
@ai-sdk/llamaindex 3.0.54 github npm
@ai-sdk/lmnt 3.0.22 github npm
@ai-sdk/luma 3.0.23 github npm
@ai-sdk/mcp 2.0.26 github npm
@ai-sdk/minimax 3.0.8 github npm
@ai-sdk/mistral 4.0.24 github npm
@ai-sdk/moonshotai 3.0.27 github npm
@ai-sdk/open-responses 2.0.22 github npm
@ai-sdk/openai 4.0.31 github npm
@ai-sdk/openai-compatible 3.0.24 github npm
@ai-sdk/otel 1.0.54 github npm
@ai-sdk/perplexity 4.0.24 github npm
@ai-sdk/policy-opa 1.0.54 github npm
@ai-sdk/prodia 2.0.23 github npm
@ai-sdk/provider-utils 5.0.22 github npm
@ai-sdk/quiverai 2.0.22 github npm
@ai-sdk/react 4.0.57 github npm
@ai-sdk/replicate 3.0.23 github npm
@ai-sdk/revai 3.0.22 github npm
@ai-sdk/rsc 3.0.54 github npm
@ai-sdk/sandbox-just-bash 1.0.60 github npm
@ai-sdk/sandbox-vercel 1.0.60 github npm
@ai-sdk/svelte 5.0.54 github npm
@ai-sdk/togetherai 3.0.25 github npm
@ai-sdk/tui 1.0.55 github npm
@ai-sdk/valibot 3.0.22 github npm
@ai-sdk/vercel 3.0.24 github npm
@ai-sdk/voyage 2.0.22 github npm
@ai-sdk/vue 4.0.54 github npm
@ai-sdk/workflow 1.0.54 github npm
@ai-sdk/workflow-harness 1.0.60 github npm
@ai-sdk/xai 4.0.29 github npm

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

Projects

None yet

2 participants