Repository navigation
fix(agents): stop a fallback chain when onResponse aborts a failed Anthropic request - #3306
Conversation
… AbortError On a failed response the wire kept the transport's error and reported it over whatever the caller's onResponse threw, on both the gateway-failure path and pi-ai's vendor-error path. An AbortError from the hook became an ordinary error, so a fallback chain tried its next leg. The hook's error now wins. Six tests (gateway failure and vendor 429 on all three wires, and a fallback that must not run); the three Anthropic ones fail without the fix. From the #3223 review.
… stream's reconnects
🦋 Changeset detectedLatest commit: 5c5666f The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| // Only the newest request's failure is the stream's: a fallback that | ||
| // resends after one is answered on its own. | ||
| transportError = undefined; | ||
| hookError = undefined; |
There was a problem hiding this comment.
🔍 Anthropic retries after a hook abort
With maxRetries enabled, the Anthropic SDK treats an AbortError thrown inside fetch as a retryable timeout. The next attempt clears hookError, so it can return a successful stream despite the abort. The regression tests disable retries.
Was this helpful? React with 👍 or 👎 to provide feedback.
| failStream( | ||
| outer, | ||
| message, | ||
| hookError !== undefined ? hookError.error : transportError, |
There was a problem hiding this comment.
🔍 Missing diagnostics for rejected responses
When onResponse throws an ordinary Error on a failed dispatch, failStream gets only the hook error. The cloudflare-error diagnostic loses the dispatch status, code, and log ID.
Was this helpful? React with 👍 or 👎 to provide feedback.
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
|
✅ agents import sizes: no significant changes ( |
If your
onResponsethrows anAbortErroron a failed Anthropic response (a 429, say), the stream ended as an ordinaryerrorinstead ofaborted, so a fallback chain went on to call its next model. The Anthropic wire kept the provider's error and reported that instead of what the hook threw. Now the hook's error is reported, as the chat-completions and Responses wires already do. This came up in the Devin review of #3223.Summary
streamAnthropic fetch (per request) gateway failure → onResponse(raw) vendor error → pi-ai calls onResponse + a throw is kept as hookError settle stream - failStream(transportError) + failStream(hookError ?? transportError)The docs also said
onResponsefires for "every response", but a stream that drops and resumes reconnects without calling it again.models-pi-ai.mdnow says it reports each request once.Evidence
New tests in
gateway.test.ts: a hook that throwsAbortErroron a gateway 402 and on a vendor 429, on each of the three wires, plusai(claude, { fallback: [workersAI] })with a hook that aborts on the 429.stopReasonis"error", and the fallback calls Workers AI).pnpm run check.Merge Danger
Door: two-way
Blast Radius: small
Only Anthropic wire requests whose
onResponsethrows are affected. A hook that throws any other error still ends the stream as anerror, now with the hook's message instead of the provider's.