Skip to content

fix(ai-anthropic): decode replies a fallback model took over - #8971

Merged
tim-smart merged 6 commits into
Effect-TS:mainfrom
m9tdev:ai-anthropic-fallback
Oct 9, 2026
Merged

tim-smart merged 6 commits into
Effect-TS:mainfrom
m9tdev:ai-anthropic-fallback

Conversation

@m9tdev

@m9tdev m9tdev commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

With server-side fallback, the Messages API can hand a refused reply to a fallback model. @effect/ai-anthropic failed to decode those responses in both generateText and streamText.

  • Add the fallback content block and fallback_message usage iterations to the response schemas.
  • Emit each fallback block as a response-metadata part, with the fallback model in modelId and the block in metadata.anthropic.fallback.
  • Keep the model field on usage iterations. It is required on fallback_message iterations and optional on message iterations, because older responses and gateways may omit it. A codegen.yml patch keeps it optional on regeneration.
  • Add two regression tests and a patch changeset.

The schemas follow the last published upstream spec (2026-09-02). In that spec the fallback trigger is required and its type is refusal.

Scope and limitations

Request-side fallbacks configuration is not supported. History replay across a fallback boundary is not supported either. Assistant history drops the fallback block but keeps the content that came before it, so multi-turn conversations that went through a fallback are not safe to replay.

Upstream spec discovery is still broken: .stats.yml no longer has openapi_spec_url. Fixing discovery and doing a full regeneration are separate work.

Validation

  • test/AnthropicLanguageModel.test.ts: 46 pass. Both new tests fail without the decoding changes.
  • Package typecheck, oxlint, dprint check, and pnpm jsdocs --check pass.
  • test/AnthropicClient.test.ts still fails to load because ./internal/errors is not exported. It fails the same way on main.

Closes EFF-1902

With server-side fallback (the `fallbacks` request field), the API can
hand a refused reply to a fallback model. It then returns a `fallback`
content block (from, to, trigger) and reports usage with
`fallback_message` iterations and a `model` on each iteration. None of
these were in the generated schemas, so such replies failed to decode.

- Generated.ts (by hand, mirroring @anthropic-ai/sdk beta/messages):
  BetaResponseFallbackBlock (in BetaContentBlock and
  content_block_start), BetaFallbackMessageIterationUsage, optional
  `model` on BetaMessageIterationUsage.
- The fallback block becomes a response-metadata part naming the
  fallback model, with the block in metadata.anthropic.fallback
  (streamed and non-streamed).
@changeset-bot

changeset-bot Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5db9e93

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 32 packages
Name Type
@effect/ai-anthropic Patch
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/openapi-generator Patch

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

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 38.83 KB 38.83 KB 0.00 KB (0.00%)
basic.ts 7.06 KB 7.06 KB 0.00 KB (0.00%)
batching.ts 10.14 KB 10.14 KB 0.00 KB (0.00%)
brand.ts 6.73 KB 6.73 KB 0.00 KB (0.00%)
cache.ts 10.90 KB 10.90 KB 0.00 KB (0.00%)
config.ts 22.31 KB 22.31 KB 0.00 KB (0.00%)
differ.ts 21.34 KB 21.34 KB 0.00 KB (0.00%)
http-client.ts 22.69 KB 22.69 KB 0.00 KB (0.00%)
http-router.ts 34.12 KB 34.12 KB 0.00 KB (0.00%)
logger.ts 11.23 KB 11.23 KB 0.00 KB (0.00%)
metric.ts 9.03 KB 9.03 KB 0.00 KB (0.00%)
optic.ts 6.96 KB 6.96 KB 0.00 KB (0.00%)
pubsub.ts 15.21 KB 15.21 KB 0.00 KB (0.00%)
queue.ts 12.14 KB 12.14 KB 0.00 KB (0.00%)
schedule.ts 11.25 KB 11.25 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 13.77 KB 13.77 KB 0.00 KB (0.00%)
schema-binary.ts 40.02 KB 40.02 KB 0.00 KB (0.00%)
schema-class.ts 21.11 KB 21.11 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 32.15 KB 32.15 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.32 KB 27.32 KB 0.00 KB (0.00%)
schema-string-transformation.ts 14.52 KB 14.52 KB 0.00 KB (0.00%)
schema-string.ts 12.03 KB 12.03 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.09 KB 16.09 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.36 KB 38.36 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 25.31 KB 25.31 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.27 KB 20.27 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.48 KB 20.48 KB 0.00 KB (0.00%)
schema-toFormatter.ts 20.58 KB 20.58 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 25.05 KB 25.05 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 20.57 KB 20.57 KB 0.00 KB (0.00%)
schema.ts 20.32 KB 20.32 KB 0.00 KB (0.00%)
stm.ts 13.27 KB 13.27 KB 0.00 KB (0.00%)
stream.ts 9.99 KB 9.99 KB 0.00 KB (0.00%)

@tim-smart
tim-smart merged commit 1db5671 into Effect-TS:main Oct 9, 2026
15 of 18 checks passed

This branch had an error being deployed

1 failed deployment
fork — 5db9e932 Deployed Oct 9, 2026 by tim-smart via approval-gate #18705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.0 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants