{{ message }}
Repository navigation
fix(workers-ai-provider): flatten Anthropic system array on the run path - #672
Closed
isaacrowntree wants to merge 1 commit into
Closed
isaacrowntree wants to merge 1 commit into
isaacrowntree wants to merge 1 commit into
Conversation
The unified-billing run path (env.AI.run) validates an Anthropic Messages body's `system` as a string, but @ai-sdk/anthropic always sends an array of text blocks. Every request with a system prompt failed with "7003: Invalid value at system: Invalid input: expected string, received array". Join text-only blocks into a string on the run path; the gateway path is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HX9h9yQ3A7rkgDtZzotEPx
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
Every Anthropic request on the unified-billing run path (
env.AI.run) fails as soon as it has a system prompt:@ai-sdk/anthropicalways serializessystemas an array of text blocks ([{ type: "text", text }]), which Anthropic's own API accepts. The run path validatessystemas a string only. SincerunWireFormat: "anthropic"sends the AI SDK's native body throughmakeRunFetchverbatim, the request is rejected.This breaks Think's string-model routing for every Claude model (
getModel() { return "anthropic/…" }), because Think always sends a system prompt.Repro (Worker with an
AIbinding,workers-ai-provider@4.0.0):Fix
In
makeRunFetch, when the run wire format isanthropic, join a text-onlysystemarray into a single string before callingbinding.run. Bodies with non-text blocks are left alone. The gateway path (gw.run, provider-native endpoints) is unchanged, because it accepts arrays.Block-level
cache_controlcan't be expressed in a string, so it is dropped on this path. Those requests were rejected outright before, so this is strictly an improvement. If the run path later accepts arrays, this can be removed.Testing
cache_control), gateway path leaves the array untouched, and helper edge cases (string, absent, non-text blocks).vitest: 22 files / 460 tests pass;tsc --noEmitclean.anthropic/claude-sonnet-5.5andanthropic/claude-sonnet-5, with a system prompt and a tool, now succeed via bothgenerateTextandstreamText(all previously failed with 7003).Changeset included (
workers-ai-provider: patch).🤖 Generated with Claude Code
https://claude.ai/code/session_01HX9h9yQ3A7rkgDtZzotEPx