@ai-sdk/anthropic 2.0.79 → 2.0.81

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.
package/dist/index.mjs CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  } from "@ai-sdk/provider-utils";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = true ? "2.0.79" : "0.0.0-test";
14
+ var VERSION = true ? "2.0.81" : "0.0.0-test";
15
15
 
16
16
  // src/anthropic-messages-language-model.ts
17
17
  import {
@@ -56,6 +56,12 @@ import {
56
56
  zodSchema as zodSchema2
57
57
  } from "@ai-sdk/provider-utils";
58
58
  import { z as z2 } from "zod/v4";
59
+ var anthropicStopDetailsSchema = z2.object({
60
+ type: z2.string(),
61
+ category: z2.string().nullish(),
62
+ explanation: z2.string().nullish(),
63
+ recommended_model: z2.string().nullish()
64
+ });
59
65
  var anthropicMessagesResponseSchema = lazySchema2(
60
66
  () => zodSchema2(
61
67
  z2.object({
@@ -243,11 +249,18 @@ var anthropicMessagesResponseSchema = lazySchema2(
243
249
  old_start: z2.number().nullable()
244
250
  })
245
251
  ])
252
+ }),
253
+ // Server-side fallback marker. Parsed so the response validates, but
254
+ // dropped from the content output (the AI SDK has no model-hop
255
+ // primitive). The hop remains observable via usage.iterations.
256
+ z2.object({
257
+ type: z2.literal("fallback")
246
258
  })
247
259
  ])
248
260
  ),
249
261
  stop_reason: z2.string().nullish(),
250
262
  stop_sequence: z2.string().nullish(),
263
+ stop_details: anthropicStopDetailsSchema.nullish(),
251
264
  usage: z2.looseObject({
252
265
  input_tokens: z2.number(),
253
266
  output_tokens: z2.number(),
@@ -255,9 +268,17 @@ var anthropicMessagesResponseSchema = lazySchema2(
255
268
  cache_read_input_tokens: z2.number().nullish(),
256
269
  iterations: z2.array(
257
270
  z2.object({
258
- type: z2.union([z2.literal("compaction"), z2.literal("message")]),
271
+ type: z2.union([
272
+ z2.literal("compaction"),
273
+ z2.literal("message"),
274
+ z2.literal("advisor_message"),
275
+ z2.literal("fallback_message")
276
+ ]),
277
+ model: z2.string().nullish(),
259
278
  input_tokens: z2.number(),
260
- output_tokens: z2.number()
279
+ output_tokens: z2.number(),
280
+ cache_creation_input_tokens: z2.number().nullish(),
281
+ cache_read_input_tokens: z2.number().nullish()
261
282
  })
262
283
  ).nullish()
263
284
  }),
@@ -463,6 +484,11 @@ var anthropicMessagesChunkSchema = lazySchema2(
463
484
  old_start: z2.number().nullable()
464
485
  })
465
486
  ])
487
+ }),
488
+ // Server-side fallback marker; dropped from content output (see the
489
+ // response schema). The hop remains observable via usage.iterations.
490
+ z2.object({
491
+ type: z2.literal("fallback")
466
492
  })
467
493
  ])
468
494
  }),
@@ -536,6 +562,7 @@ var anthropicMessagesChunkSchema = lazySchema2(
536
562
  delta: z2.object({
537
563
  stop_reason: z2.string().nullish(),
538
564
  stop_sequence: z2.string().nullish(),
565
+ stop_details: anthropicStopDetailsSchema.nullish(),
539
566
  container: z2.object({
540
567
  expires_at: z2.string(),
541
568
  id: z2.string(),
@@ -558,9 +585,17 @@ var anthropicMessagesChunkSchema = lazySchema2(
558
585
  cache_read_input_tokens: z2.number().nullish(),
559
586
  iterations: z2.array(
560
587
  z2.object({
561
- type: z2.union([z2.literal("compaction"), z2.literal("message")]),
588
+ type: z2.union([
589
+ z2.literal("compaction"),
590
+ z2.literal("message"),
591
+ z2.literal("advisor_message"),
592
+ z2.literal("fallback_message")
593
+ ]),
594
+ model: z2.string().nullish(),
562
595
  input_tokens: z2.number(),
563
- output_tokens: z2.number()
596
+ output_tokens: z2.number(),
597
+ cache_creation_input_tokens: z2.number().nullish(),
598
+ cache_read_input_tokens: z2.number().nullish()
564
599
  })
565
600
  ).nullish()
566
601
  }),
@@ -735,6 +770,31 @@ var anthropicProviderOptions = z3.object({
735
770
  * See https://platform.claude.com/docs/en/build-with-claude/data-residency
736
771
  */
737
772
  inferenceGeo: z3.enum(["us", "global"]).optional(),
773
+ /**
774
+ * Server-side fallback chain.
775
+ *
776
+ * When the primary model's safety classifiers block a turn, the API
777
+ * automatically retries it on the next model in the chain, server-side. A
778
+ * `content-filter` finish reason means the entire chain refused.
779
+ *
780
+ * Each entry is merged into the request as a direct request to that entry's
781
+ * model, so it must be formatted accordingly: `model` is required, and an
782
+ * entry may additionally override `max_tokens`, `thinking`, `output_config`,
783
+ * and `speed` for that attempt only (`speed` additionally requires the speed
784
+ * beta). The value is passed through to the API as-is.
785
+ *
786
+ * The required `server-side-fallback-2026-06-01` beta is added automatically
787
+ * when this option is set.
788
+ */
789
+ fallbacks: z3.array(
790
+ z3.object({
791
+ model: z3.string(),
792
+ max_tokens: z3.number().int().optional(),
793
+ thinking: z3.record(z3.string(), z3.unknown()).optional(),
794
+ output_config: z3.record(z3.string(), z3.unknown()).optional(),
795
+ speed: z3.enum(["fast", "standard"]).optional()
796
+ })
797
+ ).optional(),
738
798
  /**
739
799
  * Context management configuration for automatic context window management.
740
800
  * Enables features like automatic compaction and clearing of tool uses/thinking blocks.
@@ -1433,19 +1493,20 @@ async function convertToAnthropicMessagesPrompt({
1433
1493
  const type = block.type;
1434
1494
  switch (type) {
1435
1495
  case "system": {
1436
- if (system != null) {
1437
- throw new UnsupportedFunctionalityError2({
1438
- functionality: "Multiple system messages that are separated by user/assistant messages"
1439
- });
1440
- }
1441
- system = block.messages.map(({ content, providerOptions }) => ({
1496
+ const content = block.messages.map(({ content: content2, providerOptions }) => ({
1442
1497
  type: "text",
1443
- text: content,
1498
+ text: content2,
1444
1499
  cache_control: validator.getCacheControl(providerOptions, {
1445
1500
  type: "system message",
1446
1501
  canCache: true
1447
1502
  })
1448
1503
  }));
1504
+ if (system == null) {
1505
+ system = content;
1506
+ } else {
1507
+ messages.push({ role: "system", content });
1508
+ betas.add("mid-conversation-system-2026-04-07");
1509
+ }
1449
1510
  break;
1450
1511
  }
1451
1512
  case "user": {
@@ -2322,6 +2383,9 @@ var AnthropicMessagesLanguageModel = class {
2322
2383
  ...(anthropicOptions == null ? void 0 : anthropicOptions.inferenceGeo) && {
2323
2384
  inference_geo: anthropicOptions.inferenceGeo
2324
2385
  },
2386
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0 && {
2387
+ fallbacks: anthropicOptions.fallbacks
2388
+ },
2325
2389
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
2326
2390
  cache_control: anthropicOptions.cacheControl
2327
2391
  },
@@ -2464,6 +2528,9 @@ var AnthropicMessagesLanguageModel = class {
2464
2528
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
2465
2529
  betas.add("fast-mode-2026-02-01");
2466
2530
  }
2531
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0) {
2532
+ betas.add("server-side-fallback-2026-06-01");
2533
+ }
2467
2534
  if (useStructuredOutput) {
2468
2535
  betas.add("structured-outputs-2025-11-13");
2469
2536
  }
@@ -2563,7 +2630,7 @@ var AnthropicMessagesLanguageModel = class {
2563
2630
  });
2564
2631
  }
2565
2632
  async doGenerate(options) {
2566
- var _a, _b, _c, _d, _e, _f, _g, _h;
2633
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2567
2634
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2568
2635
  ...options,
2569
2636
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
@@ -2803,11 +2870,20 @@ var AnthropicMessagesLanguageModel = class {
2803
2870
  });
2804
2871
  break;
2805
2872
  }
2873
+ // Server-side fallback marker: the AI SDK has no content primitive for
2874
+ // a model hop, so drop it. The hop is still observable via
2875
+ // usage.iterations.
2876
+ case "fallback": {
2877
+ break;
2878
+ }
2806
2879
  }
2807
2880
  }
2808
2881
  let inputTokens;
2809
2882
  let outputTokens;
2810
- if (response.usage.iterations && response.usage.iterations.length > 0) {
2883
+ const servedByFallback = (_b = response.usage.iterations) == null ? void 0 : _b.some(
2884
+ (iter) => iter.type === "fallback_message"
2885
+ );
2886
+ if (response.usage.iterations && response.usage.iterations.length > 0 && !servedByFallback) {
2811
2887
  const totals = response.usage.iterations.reduce(
2812
2888
  (acc, iter) => ({
2813
2889
  input: acc.input + iter.input_tokens,
@@ -2821,6 +2897,7 @@ var AnthropicMessagesLanguageModel = class {
2821
2897
  inputTokens = response.usage.input_tokens;
2822
2898
  outputTokens = response.usage.output_tokens;
2823
2899
  }
2900
+ const stopDetails = mapAnthropicStopDetails(response.stop_details);
2824
2901
  return {
2825
2902
  content,
2826
2903
  finishReason: mapAnthropicStopReason({
@@ -2831,12 +2908,12 @@ var AnthropicMessagesLanguageModel = class {
2831
2908
  inputTokens,
2832
2909
  outputTokens,
2833
2910
  totalTokens: inputTokens + outputTokens,
2834
- cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
2911
+ cachedInputTokens: (_c = response.usage.cache_read_input_tokens) != null ? _c : void 0
2835
2912
  },
2836
2913
  request: { body: args },
2837
2914
  response: {
2838
- id: (_c = response.id) != null ? _c : void 0,
2839
- modelId: (_d = response.model) != null ? _d : void 0,
2915
+ id: (_d = response.id) != null ? _d : void 0,
2916
+ modelId: (_e = response.model) != null ? _e : void 0,
2840
2917
  headers: responseHeaders,
2841
2918
  body: rawResponse
2842
2919
  },
@@ -2844,21 +2921,23 @@ var AnthropicMessagesLanguageModel = class {
2844
2921
  providerMetadata: {
2845
2922
  anthropic: {
2846
2923
  usage: response.usage,
2847
- cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2848
- stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2924
+ cacheCreationInputTokens: (_f = response.usage.cache_creation_input_tokens) != null ? _f : null,
2925
+ stopSequence: (_g = response.stop_sequence) != null ? _g : null,
2926
+ ...stopDetails != null ? { stopDetails } : {},
2849
2927
  iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
2850
2928
  type: iter.type,
2929
+ ...iter.model != null ? { model: iter.model } : {},
2851
2930
  inputTokens: iter.input_tokens,
2852
2931
  outputTokens: iter.output_tokens
2853
2932
  })) : null,
2854
2933
  container: response.container ? {
2855
2934
  expiresAt: response.container.expires_at,
2856
2935
  id: response.container.id,
2857
- skills: (_h = (_g = response.container.skills) == null ? void 0 : _g.map((skill) => ({
2936
+ skills: (_i = (_h = response.container.skills) == null ? void 0 : _h.map((skill) => ({
2858
2937
  type: skill.type,
2859
2938
  skillId: skill.skill_id,
2860
2939
  version: skill.version
2861
- }))) != null ? _h : null
2940
+ }))) != null ? _i : null
2862
2941
  } : null,
2863
2942
  contextManagement: response.context_management ? {
2864
2943
  appliedEdits: response.context_management.applied_edits.map(
@@ -2919,6 +2998,7 @@ var AnthropicMessagesLanguageModel = class {
2919
2998
  let rawUsage = void 0;
2920
2999
  let cacheCreationInputTokens = null;
2921
3000
  let stopSequence = null;
3001
+ let stopDetails = void 0;
2922
3002
  let container = null;
2923
3003
  let iterations = null;
2924
3004
  let contextManagement = null;
@@ -2930,7 +3010,7 @@ var AnthropicMessagesLanguageModel = class {
2930
3010
  controller.enqueue({ type: "stream-start", warnings });
2931
3011
  },
2932
3012
  transform(chunk, controller) {
2933
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
3013
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2934
3014
  if (options.includeRawChunks) {
2935
3015
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2936
3016
  }
@@ -2945,6 +3025,9 @@ var AnthropicMessagesLanguageModel = class {
2945
3025
  }
2946
3026
  case "content_block_start": {
2947
3027
  const contentBlockType = value.content_block.type;
3028
+ if (contentBlockType === "fallback") {
3029
+ return;
3030
+ }
2948
3031
  blockType = contentBlockType;
2949
3032
  switch (contentBlockType) {
2950
3033
  case "text": {
@@ -3337,7 +3420,10 @@ var AnthropicMessagesLanguageModel = class {
3337
3420
  if (value.usage.iterations != null) {
3338
3421
  iterations = value.usage.iterations;
3339
3422
  }
3340
- if (value.usage.iterations && value.usage.iterations.length > 0) {
3423
+ const servedByFallback = (_f = value.usage.iterations) == null ? void 0 : _f.some(
3424
+ (iter) => iter.type === "fallback_message"
3425
+ );
3426
+ if (value.usage.iterations && value.usage.iterations.length > 0 && !servedByFallback) {
3341
3427
  const totals = value.usage.iterations.reduce(
3342
3428
  (acc, iter) => ({
3343
3429
  input: acc.input + iter.input_tokens,
@@ -3353,20 +3439,21 @@ var AnthropicMessagesLanguageModel = class {
3353
3439
  }
3354
3440
  usage.outputTokens = value.usage.output_tokens;
3355
3441
  }
3356
- usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = usage.outputTokens) != null ? _g : 0);
3442
+ usage.totalTokens = ((_g = usage.inputTokens) != null ? _g : 0) + ((_h = usage.outputTokens) != null ? _h : 0);
3357
3443
  finishReason = mapAnthropicStopReason({
3358
3444
  finishReason: value.delta.stop_reason,
3359
3445
  isJsonResponseFromTool: usesJsonResponseTool
3360
3446
  });
3361
- stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
3447
+ stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
3448
+ stopSequence = (_i = value.delta.stop_sequence) != null ? _i : null;
3362
3449
  container = value.delta.container != null ? {
3363
3450
  expiresAt: value.delta.container.expires_at,
3364
3451
  id: value.delta.container.id,
3365
- skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
3452
+ skills: (_k = (_j = value.delta.container.skills) == null ? void 0 : _j.map((skill) => ({
3366
3453
  type: skill.type,
3367
3454
  skillId: skill.skill_id,
3368
3455
  version: skill.version
3369
- }))) != null ? _j : null
3456
+ }))) != null ? _k : null
3370
3457
  } : null;
3371
3458
  if (value.context_management != null) {
3372
3459
  contextManagement = {
@@ -3411,8 +3498,10 @@ var AnthropicMessagesLanguageModel = class {
3411
3498
  usage: rawUsage != null ? rawUsage : null,
3412
3499
  cacheCreationInputTokens,
3413
3500
  stopSequence,
3501
+ ...stopDetails != null ? { stopDetails } : {},
3414
3502
  iterations: iterations ? iterations.map((iter) => ({
3415
3503
  type: iter.type,
3504
+ ...iter.model != null ? { model: iter.model } : {},
3416
3505
  inputTokens: iter.input_tokens,
3417
3506
  outputTokens: iter.output_tokens
3418
3507
  })) : null,
@@ -3468,7 +3557,7 @@ var AnthropicMessagesLanguageModel = class {
3468
3557
  }
3469
3558
  };
3470
3559
  function getModelCapabilities(modelId) {
3471
- if (modelId.includes("claude-opus-4-7")) {
3560
+ if (modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7") || modelId.includes("claude-fable-5")) {
3472
3561
  return {
3473
3562
  maxOutputTokens: 128e3,
3474
3563
  supportsStructuredOutput: true,
@@ -3533,6 +3622,17 @@ function getModelCapabilities(modelId) {
3533
3622
  };
3534
3623
  }
3535
3624
  }
3625
+ function mapAnthropicStopDetails(stopDetails) {
3626
+ if (stopDetails == null) {
3627
+ return void 0;
3628
+ }
3629
+ return {
3630
+ type: stopDetails.type,
3631
+ ...stopDetails.category != null ? { category: stopDetails.category } : {},
3632
+ ...stopDetails.explanation != null ? { explanation: stopDetails.explanation } : {},
3633
+ ...stopDetails.recommended_model != null ? { recommendedModel: stopDetails.recommended_model } : {}
3634
+ };
3635
+ }
3536
3636
 
3537
3637
  // src/tool/bash_20241022.ts
3538
3638
  import {