@ai-sdk/anthropic 4.0.51 → 4.0.53

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/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 4.0.53
4
+
5
+ ### Patch Changes
6
+
7
+ - 5ec21a6: fix: reject unsupported batch request types
8
+ - 7469a3b: feat: support image generation requests in batches
9
+ - e4292e7: feat(anthropic): expand preserved thinking support to cover `prefix_mismatch_behavior: 'error'`
10
+ - Updated dependencies [5ec21a6]
11
+ - Updated dependencies [7469a3b]
12
+ - Updated dependencies [813bb36]
13
+ - Updated dependencies [c43e4b7]
14
+ - @ai-sdk/provider@4.0.14
15
+ - @ai-sdk/provider-utils@5.0.40
16
+
17
+ ## 4.0.52
18
+
19
+ ### Patch Changes
20
+
21
+ - 5a7e647: feat(anthropic): add batch cancellation and listing
22
+ - Updated dependencies [9942196]
23
+ - @ai-sdk/provider@4.0.13
24
+ - @ai-sdk/provider-utils@5.0.39
25
+
3
26
  ## 4.0.51
4
27
 
5
28
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -46,6 +46,17 @@ type AnthropicUsageIteration = {
46
46
  interface AnthropicMessageMetadata {
47
47
  usage: JSONObject;
48
48
  stopSequence: string | null;
49
+ /**
50
+ * Input blocks that Anthropic transformed before inference. This is
51
+ * populated when thinking binding controls drop a mismatched thinking block.
52
+ * Values are intentionally open-ended so new transformation types and
53
+ * reasons remain forward compatible.
54
+ */
55
+ inputTransformations?: Array<{
56
+ type: string;
57
+ path: string;
58
+ reason: string;
59
+ }>;
49
60
  /**
50
61
  * Details about why the request stopped. Present only when the API returns
51
62
  * a `refusal` stop reason together with a `stop_details` object (a
@@ -220,7 +231,10 @@ declare const anthropicLanguageModelOptions: z.ZodObject<{
220
231
  updates: "updates";
221
232
  }>>;
222
233
  blockBinding: z.ZodOptional<z.ZodObject<{
223
- prefixMismatchBehavior: z.ZodLiteral<"drop_block">;
234
+ prefixMismatchBehavior: z.ZodEnum<{
235
+ error: "error";
236
+ drop_block: "drop_block";
237
+ }>;
224
238
  }, z.core.$strip>>;
225
239
  }, z.core.$strip>, z.ZodObject<{
226
240
  type: z.ZodLiteral<"enabled">;
@@ -230,7 +244,10 @@ declare const anthropicLanguageModelOptions: z.ZodObject<{
230
244
  }, z.core.$strip>]>, z.ZodObject<{
231
245
  type: z.ZodOptional<z.ZodNever>;
232
246
  blockBinding: z.ZodObject<{
233
- prefixMismatchBehavior: z.ZodLiteral<"drop_block">;
247
+ prefixMismatchBehavior: z.ZodEnum<{
248
+ error: "error";
249
+ drop_block: "drop_block";
250
+ }>;
234
251
  }, z.core.$strip>;
235
252
  }, z.core.$strip>]>>;
236
253
  disableParallelToolUse: z.ZodOptional<z.ZodBoolean>;
package/dist/index.js CHANGED
@@ -228,6 +228,11 @@ var anthropicMcpToolResultContentSchema = z3.union([
228
228
  ])
229
229
  )
230
230
  ]);
231
+ var anthropicInputTransformationSchema = z3.object({
232
+ type: z3.string(),
233
+ path: z3.string(),
234
+ reason: z3.string()
235
+ });
231
236
  var anthropicResponseSchema = lazySchema3(
232
237
  () => zodSchema3(
233
238
  z3.object({
@@ -482,6 +487,7 @@ var anthropicResponseSchema = lazySchema3(
482
487
  stop_reason: z3.string().nullish(),
483
488
  stop_sequence: z3.string().nullish(),
484
489
  stop_details: anthropicStopDetailsSchema.nullish(),
490
+ input_transformations: z3.array(anthropicInputTransformationSchema).nullish(),
485
491
  usage: z3.looseObject({
486
492
  input_tokens: z3.number(),
487
493
  output_tokens: z3.number(),
@@ -566,6 +572,7 @@ var anthropicChunkSchema = lazySchema3(
566
572
  ])
567
573
  ).nullish(),
568
574
  stop_reason: z3.string().nullish(),
575
+ input_transformations: z3.array(anthropicInputTransformationSchema).nullish(),
569
576
  container: z3.object({
570
577
  expires_at: z3.string(),
571
578
  id: z3.string()
@@ -903,6 +910,7 @@ var anthropicChunkSchema = lazySchema3(
903
910
  })
904
911
  ).nullish()
905
912
  }),
913
+ input_transformations: z3.array(anthropicInputTransformationSchema).nullish(),
906
914
  context_management: z3.object({
907
915
  applied_edits: z3.array(
908
916
  z3.union([
@@ -1051,7 +1059,7 @@ var anthropicLanguageModelOptions = z4.object({
1051
1059
  * prefix. Requires the `thinking-binding-controls-2026-08-01` beta.
1052
1060
  */
1053
1061
  blockBinding: z4.object({
1054
- prefixMismatchBehavior: z4.literal("drop_block")
1062
+ prefixMismatchBehavior: z4.enum(["error", "drop_block"])
1055
1063
  }).optional()
1056
1064
  }),
1057
1065
  z4.object({
@@ -1066,7 +1074,7 @@ var anthropicLanguageModelOptions = z4.object({
1066
1074
  z4.object({
1067
1075
  type: z4.never().optional(),
1068
1076
  blockBinding: z4.object({
1069
- prefixMismatchBehavior: z4.literal("drop_block")
1077
+ prefixMismatchBehavior: z4.enum(["error", "drop_block"])
1070
1078
  })
1071
1079
  })
1072
1080
  ]).optional(),
@@ -4822,6 +4830,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4822
4830
  usage: response.usage,
4823
4831
  stopSequence: (_a2 = response.stop_sequence) != null ? _a2 : null,
4824
4832
  ...stopDetails != null ? { stopDetails } : {},
4833
+ ...response.input_transformations != null ? { inputTransformations: response.input_transformations } : {},
4825
4834
  iterations: response.usage.iterations ? response.usage.iterations.map(
4826
4835
  (iter) => ({
4827
4836
  type: iter.type,
@@ -4909,6 +4918,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4909
4918
  let rawUsage = void 0;
4910
4919
  let stopSequence = null;
4911
4920
  let stopDetails = void 0;
4921
+ let inputTransformations;
4912
4922
  let container = null;
4913
4923
  let isJsonResponseFromTool = false;
4914
4924
  let isMessageOpen = false;
@@ -5578,6 +5588,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5578
5588
  rawUsage = {
5579
5589
  ...value.message.usage
5580
5590
  };
5591
+ if (value.message.input_transformations != null) {
5592
+ inputTransformations = value.message.input_transformations;
5593
+ }
5581
5594
  if (value.message.container != null) {
5582
5595
  container = {
5583
5596
  expiresAt: value.message.container.expires_at,
@@ -5677,6 +5690,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5677
5690
  value.context_management
5678
5691
  );
5679
5692
  }
5693
+ if (value.input_transformations != null) {
5694
+ inputTransformations = value.input_transformations;
5695
+ }
5680
5696
  rawUsage = {
5681
5697
  ...rawUsage,
5682
5698
  ...value.usage
@@ -5690,6 +5706,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5690
5706
  usage: rawUsage != null ? rawUsage : null,
5691
5707
  stopSequence,
5692
5708
  ...stopDetails != null ? { stopDetails } : {},
5709
+ ...inputTransformations != null ? { inputTransformations } : {},
5693
5710
  iterations: usage.iterations ? usage.iterations.map(
5694
5711
  (iter) => ({
5695
5712
  type: iter.type,
@@ -6008,25 +6025,33 @@ var anthropicBatchRequestIdPattern = /^[A-Za-z0-9_-]{1,64}$/;
6008
6025
  var anthropicBatchProviderOptionsSchema = anthropicLanguageModelOptions.pick({
6009
6026
  anthropicBeta: true
6010
6027
  });
6028
+ var anthropicBatchResponseZodSchema = () => z15.object({
6029
+ id: z15.string(),
6030
+ type: z15.literal("message_batch"),
6031
+ processing_status: z15.string(),
6032
+ request_counts: z15.object({
6033
+ processing: z15.number(),
6034
+ succeeded: z15.number(),
6035
+ errored: z15.number(),
6036
+ canceled: z15.number(),
6037
+ expired: z15.number()
6038
+ }),
6039
+ created_at: z15.string(),
6040
+ expires_at: z15.string(),
6041
+ archived_at: z15.string().nullish(),
6042
+ cancel_initiated_at: z15.string().nullish(),
6043
+ ended_at: z15.string().nullish(),
6044
+ results_url: z15.string().nullish()
6045
+ });
6011
6046
  var anthropicBatchResponseSchema = lazySchema14(
6047
+ () => zodSchema14(anthropicBatchResponseZodSchema())
6048
+ );
6049
+ var anthropicBatchListResponseSchema = lazySchema14(
6012
6050
  () => zodSchema14(
6013
6051
  z15.object({
6014
- id: z15.string(),
6015
- type: z15.literal("message_batch"),
6016
- processing_status: z15.string(),
6017
- request_counts: z15.object({
6018
- processing: z15.number(),
6019
- succeeded: z15.number(),
6020
- errored: z15.number(),
6021
- canceled: z15.number(),
6022
- expired: z15.number()
6023
- }),
6024
- created_at: z15.string(),
6025
- expires_at: z15.string(),
6026
- archived_at: z15.string().nullish(),
6027
- cancel_initiated_at: z15.string().nullish(),
6028
- ended_at: z15.string().nullish(),
6029
- results_url: z15.string().nullish()
6052
+ data: z15.array(anthropicBatchResponseZodSchema()),
6053
+ has_more: z15.boolean(),
6054
+ last_id: z15.string().nullish()
6030
6055
  })
6031
6056
  )
6032
6057
  );
@@ -6080,6 +6105,17 @@ var anthropicBatchResultLineSchema = lazySchema14(
6080
6105
  })
6081
6106
  )
6082
6107
  );
6108
+ function assertTextBatchRequests(requests) {
6109
+ for (const request of requests) {
6110
+ const requestType = request.type;
6111
+ if (requestType !== "text") {
6112
+ throw new UnsupportedFunctionalityError3({
6113
+ functionality: `batch request type: ${requestType}`,
6114
+ message: `The Anthropic Message Batches API does not support batch requests with type "${requestType}".`
6115
+ });
6116
+ }
6117
+ }
6118
+ }
6083
6119
  var AnthropicBatch = class {
6084
6120
  constructor(options) {
6085
6121
  this.options = options;
@@ -6097,6 +6133,7 @@ var AnthropicBatch = class {
6097
6133
  webhookUrl
6098
6134
  }) {
6099
6135
  var _a, _b, _c, _d;
6136
+ assertTextBatchRequests(requests);
6100
6137
  validateRequestIds(requests);
6101
6138
  const explicitBatchBetas = new Set(
6102
6139
  await getAnthropicBatchProviderBetas({
@@ -6187,6 +6224,47 @@ var AnthropicBatch = class {
6187
6224
  async doGetBatchStatus(options) {
6188
6225
  return convertAnthropicBatchStatus(await this.retrieveBatch(options));
6189
6226
  }
6227
+ async doCancelBatch(options) {
6228
+ await postJsonToApi2({
6229
+ url: this.getBatchUrl(`/${encodeURIComponent(options.batchId)}/cancel`),
6230
+ headers: await this.getBatchHeaders(options.headers),
6231
+ body: {},
6232
+ failedResponseHandler: anthropicFailedResponseHandler,
6233
+ successfulResponseHandler: createJsonResponseHandler3(
6234
+ anthropicBatchResponseSchema
6235
+ ),
6236
+ abortSignal: options.abortSignal,
6237
+ fetch: this.options.config.fetch
6238
+ });
6239
+ return {};
6240
+ }
6241
+ async doListBatches(options) {
6242
+ const url = new URL(this.getBatchUrl(""));
6243
+ if (options.limit != null) {
6244
+ url.searchParams.set("limit", String(options.limit));
6245
+ }
6246
+ if (options.cursor != null) {
6247
+ url.searchParams.set("after_id", options.cursor);
6248
+ }
6249
+ const { value: page } = await getFromApi({
6250
+ url: url.toString(),
6251
+ validateUrl: false,
6252
+ headers: await this.getBatchHeaders(options.headers),
6253
+ failedResponseHandler: anthropicFailedResponseHandler,
6254
+ successfulResponseHandler: createJsonResponseHandler3(
6255
+ anthropicBatchListResponseSchema
6256
+ ),
6257
+ abortSignal: options.abortSignal,
6258
+ fetch: this.options.config.fetch
6259
+ });
6260
+ return {
6261
+ batches: page.data.map((batch) => ({
6262
+ batchId: batch.id,
6263
+ ...convertAnthropicBatchStatus(batch)
6264
+ })),
6265
+ ...page.has_more && page.last_id != null ? { nextCursor: page.last_id } : {}
6266
+ };
6267
+ }
6190
6268
  async doGetBatchResults(options) {
6191
6269
  const batch = await this.retrieveBatch(options);
6192
6270
  if (convertAnthropicBatchStatus(batch).status === "pending") {
@@ -6771,6 +6849,7 @@ function convertAnthropicMessageMetadata(response) {
6771
6849
  usage: response.usage,
6772
6850
  stopSequence: (_a = response.stop_sequence) != null ? _a : null,
6773
6851
  ...stopDetails != null ? { stopDetails } : {},
6852
+ ...response.input_transformations != null ? { inputTransformations: response.input_transformations } : {},
6774
6853
  iterations: response.usage.iterations ? response.usage.iterations.map(
6775
6854
  (iteration) => ({
6776
6855
  type: iteration.type,
@@ -7573,7 +7652,7 @@ var AnthropicSkills = class {
7573
7652
  };
7574
7653
 
7575
7654
  // src/version.ts
7576
- var VERSION = true ? "4.0.51" : "0.0.0-test";
7655
+ var VERSION = true ? "4.0.53" : "0.0.0-test";
7577
7656
 
7578
7657
  // src/anthropic-provider.ts
7579
7658
  var ANTHROPIC_API_URL = "https://api.anthropic.com";