@ai-sdk/anthropic 2.0.80 → 2.0.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,6 +41,12 @@ import {
41
41
  zodSchema as zodSchema2
42
42
  } from "@ai-sdk/provider-utils";
43
43
  import { z as z2 } from "zod/v4";
44
+ var anthropicStopDetailsSchema = z2.object({
45
+ type: z2.string(),
46
+ category: z2.string().nullish(),
47
+ explanation: z2.string().nullish(),
48
+ recommended_model: z2.string().nullish()
49
+ });
44
50
  var anthropicMessagesResponseSchema = lazySchema2(
45
51
  () => zodSchema2(
46
52
  z2.object({
@@ -228,11 +234,18 @@ var anthropicMessagesResponseSchema = lazySchema2(
228
234
  old_start: z2.number().nullable()
229
235
  })
230
236
  ])
237
+ }),
238
+ // Server-side fallback marker. Parsed so the response validates, but
239
+ // dropped from the content output (the AI SDK has no model-hop
240
+ // primitive). The hop remains observable via usage.iterations.
241
+ z2.object({
242
+ type: z2.literal("fallback")
231
243
  })
232
244
  ])
233
245
  ),
234
246
  stop_reason: z2.string().nullish(),
235
247
  stop_sequence: z2.string().nullish(),
248
+ stop_details: anthropicStopDetailsSchema.nullish(),
236
249
  usage: z2.looseObject({
237
250
  input_tokens: z2.number(),
238
251
  output_tokens: z2.number(),
@@ -240,9 +253,17 @@ var anthropicMessagesResponseSchema = lazySchema2(
240
253
  cache_read_input_tokens: z2.number().nullish(),
241
254
  iterations: z2.array(
242
255
  z2.object({
243
- type: z2.union([z2.literal("compaction"), z2.literal("message")]),
256
+ type: z2.union([
257
+ z2.literal("compaction"),
258
+ z2.literal("message"),
259
+ z2.literal("advisor_message"),
260
+ z2.literal("fallback_message")
261
+ ]),
262
+ model: z2.string().nullish(),
244
263
  input_tokens: z2.number(),
245
- output_tokens: z2.number()
264
+ output_tokens: z2.number(),
265
+ cache_creation_input_tokens: z2.number().nullish(),
266
+ cache_read_input_tokens: z2.number().nullish()
246
267
  })
247
268
  ).nullish()
248
269
  }),
@@ -448,6 +469,11 @@ var anthropicMessagesChunkSchema = lazySchema2(
448
469
  old_start: z2.number().nullable()
449
470
  })
450
471
  ])
472
+ }),
473
+ // Server-side fallback marker; dropped from content output (see the
474
+ // response schema). The hop remains observable via usage.iterations.
475
+ z2.object({
476
+ type: z2.literal("fallback")
451
477
  })
452
478
  ])
453
479
  }),
@@ -521,6 +547,7 @@ var anthropicMessagesChunkSchema = lazySchema2(
521
547
  delta: z2.object({
522
548
  stop_reason: z2.string().nullish(),
523
549
  stop_sequence: z2.string().nullish(),
550
+ stop_details: anthropicStopDetailsSchema.nullish(),
524
551
  container: z2.object({
525
552
  expires_at: z2.string(),
526
553
  id: z2.string(),
@@ -543,9 +570,17 @@ var anthropicMessagesChunkSchema = lazySchema2(
543
570
  cache_read_input_tokens: z2.number().nullish(),
544
571
  iterations: z2.array(
545
572
  z2.object({
546
- type: z2.union([z2.literal("compaction"), z2.literal("message")]),
573
+ type: z2.union([
574
+ z2.literal("compaction"),
575
+ z2.literal("message"),
576
+ z2.literal("advisor_message"),
577
+ z2.literal("fallback_message")
578
+ ]),
579
+ model: z2.string().nullish(),
547
580
  input_tokens: z2.number(),
548
- output_tokens: z2.number()
581
+ output_tokens: z2.number(),
582
+ cache_creation_input_tokens: z2.number().nullish(),
583
+ cache_read_input_tokens: z2.number().nullish()
549
584
  })
550
585
  ).nullish()
551
586
  }),
@@ -720,6 +755,31 @@ var anthropicProviderOptions = z3.object({
720
755
  * See https://platform.claude.com/docs/en/build-with-claude/data-residency
721
756
  */
722
757
  inferenceGeo: z3.enum(["us", "global"]).optional(),
758
+ /**
759
+ * Server-side fallback chain.
760
+ *
761
+ * When the primary model's safety classifiers block a turn, the API
762
+ * automatically retries it on the next model in the chain, server-side. A
763
+ * `content-filter` finish reason means the entire chain refused.
764
+ *
765
+ * Each entry is merged into the request as a direct request to that entry's
766
+ * model, so it must be formatted accordingly: `model` is required, and an
767
+ * entry may additionally override `max_tokens`, `thinking`, `output_config`,
768
+ * and `speed` for that attempt only (`speed` additionally requires the speed
769
+ * beta). The value is passed through to the API as-is.
770
+ *
771
+ * The required `server-side-fallback-2026-06-01` beta is added automatically
772
+ * when this option is set.
773
+ */
774
+ fallbacks: z3.array(
775
+ z3.object({
776
+ model: z3.string(),
777
+ max_tokens: z3.number().int().optional(),
778
+ thinking: z3.record(z3.string(), z3.unknown()).optional(),
779
+ output_config: z3.record(z3.string(), z3.unknown()).optional(),
780
+ speed: z3.enum(["fast", "standard"]).optional()
781
+ })
782
+ ).optional(),
723
783
  /**
724
784
  * Context management configuration for automatic context window management.
725
785
  * Enables features like automatic compaction and clearing of tool uses/thinking blocks.
@@ -2308,6 +2368,9 @@ var AnthropicMessagesLanguageModel = class {
2308
2368
  ...(anthropicOptions == null ? void 0 : anthropicOptions.inferenceGeo) && {
2309
2369
  inference_geo: anthropicOptions.inferenceGeo
2310
2370
  },
2371
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0 && {
2372
+ fallbacks: anthropicOptions.fallbacks
2373
+ },
2311
2374
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
2312
2375
  cache_control: anthropicOptions.cacheControl
2313
2376
  },
@@ -2450,6 +2513,9 @@ var AnthropicMessagesLanguageModel = class {
2450
2513
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
2451
2514
  betas.add("fast-mode-2026-02-01");
2452
2515
  }
2516
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0) {
2517
+ betas.add("server-side-fallback-2026-06-01");
2518
+ }
2453
2519
  if (useStructuredOutput) {
2454
2520
  betas.add("structured-outputs-2025-11-13");
2455
2521
  }
@@ -2549,7 +2615,7 @@ var AnthropicMessagesLanguageModel = class {
2549
2615
  });
2550
2616
  }
2551
2617
  async doGenerate(options) {
2552
- var _a, _b, _c, _d, _e, _f, _g, _h;
2618
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2553
2619
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2554
2620
  ...options,
2555
2621
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
@@ -2789,11 +2855,20 @@ var AnthropicMessagesLanguageModel = class {
2789
2855
  });
2790
2856
  break;
2791
2857
  }
2858
+ // Server-side fallback marker: the AI SDK has no content primitive for
2859
+ // a model hop, so drop it. The hop is still observable via
2860
+ // usage.iterations.
2861
+ case "fallback": {
2862
+ break;
2863
+ }
2792
2864
  }
2793
2865
  }
2794
2866
  let inputTokens;
2795
2867
  let outputTokens;
2796
- if (response.usage.iterations && response.usage.iterations.length > 0) {
2868
+ const servedByFallback = (_b = response.usage.iterations) == null ? void 0 : _b.some(
2869
+ (iter) => iter.type === "fallback_message"
2870
+ );
2871
+ if (response.usage.iterations && response.usage.iterations.length > 0 && !servedByFallback) {
2797
2872
  const totals = response.usage.iterations.reduce(
2798
2873
  (acc, iter) => ({
2799
2874
  input: acc.input + iter.input_tokens,
@@ -2807,6 +2882,7 @@ var AnthropicMessagesLanguageModel = class {
2807
2882
  inputTokens = response.usage.input_tokens;
2808
2883
  outputTokens = response.usage.output_tokens;
2809
2884
  }
2885
+ const stopDetails = mapAnthropicStopDetails(response.stop_details);
2810
2886
  return {
2811
2887
  content,
2812
2888
  finishReason: mapAnthropicStopReason({
@@ -2817,12 +2893,12 @@ var AnthropicMessagesLanguageModel = class {
2817
2893
  inputTokens,
2818
2894
  outputTokens,
2819
2895
  totalTokens: inputTokens + outputTokens,
2820
- cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
2896
+ cachedInputTokens: (_c = response.usage.cache_read_input_tokens) != null ? _c : void 0
2821
2897
  },
2822
2898
  request: { body: args },
2823
2899
  response: {
2824
- id: (_c = response.id) != null ? _c : void 0,
2825
- modelId: (_d = response.model) != null ? _d : void 0,
2900
+ id: (_d = response.id) != null ? _d : void 0,
2901
+ modelId: (_e = response.model) != null ? _e : void 0,
2826
2902
  headers: responseHeaders,
2827
2903
  body: rawResponse
2828
2904
  },
@@ -2830,21 +2906,23 @@ var AnthropicMessagesLanguageModel = class {
2830
2906
  providerMetadata: {
2831
2907
  anthropic: {
2832
2908
  usage: response.usage,
2833
- cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2834
- stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2909
+ cacheCreationInputTokens: (_f = response.usage.cache_creation_input_tokens) != null ? _f : null,
2910
+ stopSequence: (_g = response.stop_sequence) != null ? _g : null,
2911
+ ...stopDetails != null ? { stopDetails } : {},
2835
2912
  iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
2836
2913
  type: iter.type,
2914
+ ...iter.model != null ? { model: iter.model } : {},
2837
2915
  inputTokens: iter.input_tokens,
2838
2916
  outputTokens: iter.output_tokens
2839
2917
  })) : null,
2840
2918
  container: response.container ? {
2841
2919
  expiresAt: response.container.expires_at,
2842
2920
  id: response.container.id,
2843
- skills: (_h = (_g = response.container.skills) == null ? void 0 : _g.map((skill) => ({
2921
+ skills: (_i = (_h = response.container.skills) == null ? void 0 : _h.map((skill) => ({
2844
2922
  type: skill.type,
2845
2923
  skillId: skill.skill_id,
2846
2924
  version: skill.version
2847
- }))) != null ? _h : null
2925
+ }))) != null ? _i : null
2848
2926
  } : null,
2849
2927
  contextManagement: response.context_management ? {
2850
2928
  appliedEdits: response.context_management.applied_edits.map(
@@ -2905,6 +2983,7 @@ var AnthropicMessagesLanguageModel = class {
2905
2983
  let rawUsage = void 0;
2906
2984
  let cacheCreationInputTokens = null;
2907
2985
  let stopSequence = null;
2986
+ let stopDetails = void 0;
2908
2987
  let container = null;
2909
2988
  let iterations = null;
2910
2989
  let contextManagement = null;
@@ -2916,7 +2995,7 @@ var AnthropicMessagesLanguageModel = class {
2916
2995
  controller.enqueue({ type: "stream-start", warnings });
2917
2996
  },
2918
2997
  transform(chunk, controller) {
2919
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
2998
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2920
2999
  if (options.includeRawChunks) {
2921
3000
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2922
3001
  }
@@ -2931,6 +3010,9 @@ var AnthropicMessagesLanguageModel = class {
2931
3010
  }
2932
3011
  case "content_block_start": {
2933
3012
  const contentBlockType = value.content_block.type;
3013
+ if (contentBlockType === "fallback") {
3014
+ return;
3015
+ }
2934
3016
  blockType = contentBlockType;
2935
3017
  switch (contentBlockType) {
2936
3018
  case "text": {
@@ -3323,7 +3405,10 @@ var AnthropicMessagesLanguageModel = class {
3323
3405
  if (value.usage.iterations != null) {
3324
3406
  iterations = value.usage.iterations;
3325
3407
  }
3326
- if (value.usage.iterations && value.usage.iterations.length > 0) {
3408
+ const servedByFallback = (_f = value.usage.iterations) == null ? void 0 : _f.some(
3409
+ (iter) => iter.type === "fallback_message"
3410
+ );
3411
+ if (value.usage.iterations && value.usage.iterations.length > 0 && !servedByFallback) {
3327
3412
  const totals = value.usage.iterations.reduce(
3328
3413
  (acc, iter) => ({
3329
3414
  input: acc.input + iter.input_tokens,
@@ -3339,20 +3424,21 @@ var AnthropicMessagesLanguageModel = class {
3339
3424
  }
3340
3425
  usage.outputTokens = value.usage.output_tokens;
3341
3426
  }
3342
- usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = usage.outputTokens) != null ? _g : 0);
3427
+ usage.totalTokens = ((_g = usage.inputTokens) != null ? _g : 0) + ((_h = usage.outputTokens) != null ? _h : 0);
3343
3428
  finishReason = mapAnthropicStopReason({
3344
3429
  finishReason: value.delta.stop_reason,
3345
3430
  isJsonResponseFromTool: usesJsonResponseTool
3346
3431
  });
3347
- stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
3432
+ stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
3433
+ stopSequence = (_i = value.delta.stop_sequence) != null ? _i : null;
3348
3434
  container = value.delta.container != null ? {
3349
3435
  expiresAt: value.delta.container.expires_at,
3350
3436
  id: value.delta.container.id,
3351
- skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
3437
+ skills: (_k = (_j = value.delta.container.skills) == null ? void 0 : _j.map((skill) => ({
3352
3438
  type: skill.type,
3353
3439
  skillId: skill.skill_id,
3354
3440
  version: skill.version
3355
- }))) != null ? _j : null
3441
+ }))) != null ? _k : null
3356
3442
  } : null;
3357
3443
  if (value.context_management != null) {
3358
3444
  contextManagement = {
@@ -3397,8 +3483,10 @@ var AnthropicMessagesLanguageModel = class {
3397
3483
  usage: rawUsage != null ? rawUsage : null,
3398
3484
  cacheCreationInputTokens,
3399
3485
  stopSequence,
3486
+ ...stopDetails != null ? { stopDetails } : {},
3400
3487
  iterations: iterations ? iterations.map((iter) => ({
3401
3488
  type: iter.type,
3489
+ ...iter.model != null ? { model: iter.model } : {},
3402
3490
  inputTokens: iter.input_tokens,
3403
3491
  outputTokens: iter.output_tokens
3404
3492
  })) : null,
@@ -3454,7 +3542,7 @@ var AnthropicMessagesLanguageModel = class {
3454
3542
  }
3455
3543
  };
3456
3544
  function getModelCapabilities(modelId) {
3457
- if (modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7")) {
3545
+ if (modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7") || modelId.includes("claude-fable-5")) {
3458
3546
  return {
3459
3547
  maxOutputTokens: 128e3,
3460
3548
  supportsStructuredOutput: true,
@@ -3519,6 +3607,17 @@ function getModelCapabilities(modelId) {
3519
3607
  };
3520
3608
  }
3521
3609
  }
3610
+ function mapAnthropicStopDetails(stopDetails) {
3611
+ if (stopDetails == null) {
3612
+ return void 0;
3613
+ }
3614
+ return {
3615
+ type: stopDetails.type,
3616
+ ...stopDetails.category != null ? { category: stopDetails.category } : {},
3617
+ ...stopDetails.explanation != null ? { explanation: stopDetails.explanation } : {},
3618
+ ...stopDetails.recommended_model != null ? { recommendedModel: stopDetails.recommended_model } : {}
3619
+ };
3620
+ }
3522
3621
 
3523
3622
  // src/tool/bash_20241022.ts
3524
3623
  import {