@ai-sdk/anthropic 4.0.92 → 4.0.94

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.
@@ -63,6 +63,330 @@ var AnthropicFiles = class {
63
63
  }
64
64
  };
65
65
  //#endregion
66
+ //#region src/anthropic-model-catalog.ts
67
+ /**
68
+ * Options accepted under `providerOptions.anthropic`. The `satisfies` check
69
+ * keeps this list in sync with the option schema.
70
+ */
71
+ const anthropicProviderOptions = { language: {
72
+ sendReasoning: { description: "Whether to send reasoning to the model." },
73
+ structuredOutputMode: { description: "How structured outputs are generated." },
74
+ thinking: { description: "Extended thinking configuration." },
75
+ disableParallelToolUse: { description: "Use at most one tool per response." },
76
+ cacheControl: { description: "Prompt cache control settings." },
77
+ metadata: { description: "Request metadata, such as a user ID." },
78
+ mcpServers: { description: "MCP servers to use in the request." },
79
+ container: { description: "Container and Agent Skills configuration." },
80
+ toolStreaming: { description: "Fine-grained streaming of tool inputs and outputs." },
81
+ effort: { description: "Effort spent on thinking, text, and tool calls." },
82
+ taskBudget: { description: "Advisory token budget for an agentic task." },
83
+ speed: { description: "Fast mode for faster output token speeds." },
84
+ serviceTier: { description: "Service tier for the request." },
85
+ inferenceGeo: { description: "Where model inference runs." },
86
+ fallbacks: { description: "Server-side fallback models." },
87
+ anthropicBeta: { description: "Additional beta features to enable." },
88
+ safeguards: { description: "Server-side safeguards to run." },
89
+ compaction: { description: "On-demand conversation summary." },
90
+ contextManagement: { description: "Server-side context management." }
91
+ } };
92
+ const legacyClaude = defineRuntime({});
93
+ const currentClaude = defineRuntime({
94
+ maxOutputTokens: 128e3,
95
+ nativeStructuredOutput: true,
96
+ adaptiveThinking: true
97
+ });
98
+ const currentClaudeWithoutSampling = defineRuntime({
99
+ ...currentClaude,
100
+ samplingParameters: false,
101
+ xhighEffort: true
102
+ });
103
+ function defineRuntime(overrides) {
104
+ return {
105
+ maxOutputTokens: 4096,
106
+ nativeStructuredOutput: false,
107
+ adaptiveThinking: false,
108
+ samplingParameters: true,
109
+ xhighEffort: false,
110
+ thinkingCanBeDisabledAboveHighEffort: true,
111
+ thinkingCanBeDisabled: true,
112
+ budgetThinking: true,
113
+ forcedToolUse: true,
114
+ betweenToolsThinking: false,
115
+ knownModel: true,
116
+ ...overrides
117
+ };
118
+ }
119
+ const runtimeRules = [
120
+ {
121
+ matches: (modelId) => modelId.includes("claude-sonnet-5-5"),
122
+ definition: defineRuntime({
123
+ ...currentClaudeWithoutSampling,
124
+ thinkingCanBeDisabledAboveHighEffort: false,
125
+ thinkingCanBeDisabled: false,
126
+ budgetThinking: false,
127
+ forcedToolUse: false,
128
+ betweenToolsThinking: true
129
+ })
130
+ },
131
+ {
132
+ matches: (modelId) => modelId.includes("claude-opus-5-5"),
133
+ definition: defineRuntime({
134
+ ...currentClaudeWithoutSampling,
135
+ thinkingCanBeDisabledAboveHighEffort: false,
136
+ thinkingCanBeDisabled: false,
137
+ budgetThinking: false,
138
+ forcedToolUse: false
139
+ })
140
+ },
141
+ {
142
+ matches: (modelId) => modelId.includes("claude-haiku-5-5"),
143
+ definition: defineRuntime({
144
+ ...currentClaudeWithoutSampling,
145
+ thinkingCanBeDisabledAboveHighEffort: false,
146
+ budgetThinking: false
147
+ })
148
+ },
149
+ {
150
+ matches: (modelId) => modelId.includes("claude-opus-5"),
151
+ definition: defineRuntime({
152
+ ...currentClaudeWithoutSampling,
153
+ thinkingCanBeDisabledAboveHighEffort: false
154
+ })
155
+ },
156
+ {
157
+ matches: (modelId) => modelId.includes("claude-fable-5-1"),
158
+ definition: defineRuntime({
159
+ ...currentClaudeWithoutSampling,
160
+ thinkingCanBeDisabled: false,
161
+ budgetThinking: false,
162
+ forcedToolUse: false
163
+ })
164
+ },
165
+ {
166
+ matches: (modelId) => modelId.includes("claude-fable-5"),
167
+ definition: defineRuntime({
168
+ ...currentClaudeWithoutSampling,
169
+ thinkingCanBeDisabled: false,
170
+ budgetThinking: false
171
+ })
172
+ },
173
+ {
174
+ matches: (modelId) => modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7") || modelId.includes("claude-sonnet-5"),
175
+ definition: currentClaudeWithoutSampling
176
+ },
177
+ {
178
+ matches: (modelId) => modelId.includes("claude-sonnet-4-6") || modelId.includes("claude-opus-4-6"),
179
+ definition: currentClaude
180
+ },
181
+ {
182
+ matches: (modelId) => modelId.includes("claude-sonnet-4-5") || modelId.includes("claude-opus-4-5") || modelId.includes("claude-haiku-4-5"),
183
+ definition: defineRuntime({
184
+ maxOutputTokens: 64e3,
185
+ nativeStructuredOutput: true
186
+ })
187
+ },
188
+ {
189
+ matches: (modelId) => modelId.includes("claude-opus-4-1"),
190
+ definition: defineRuntime({
191
+ maxOutputTokens: 32e3,
192
+ nativeStructuredOutput: true
193
+ })
194
+ },
195
+ {
196
+ matches: (modelId) => /claude-sonnet-4(?:-|@)/.test(modelId),
197
+ definition: defineRuntime({ maxOutputTokens: 64e3 })
198
+ },
199
+ {
200
+ matches: (modelId) => /claude-opus-4(?:-|@)/.test(modelId),
201
+ definition: defineRuntime({ maxOutputTokens: 32e3 })
202
+ },
203
+ {
204
+ matches: (modelId) => modelId.includes("claude-3-haiku"),
205
+ definition: legacyClaude
206
+ },
207
+ {
208
+ matches: (modelId) => /claude-(?:instant(?:-|$)|v?2(?=$|[-.:])|3(?=$|[-.]))/.test(modelId),
209
+ definition: defineRuntime({ knownModel: false })
210
+ }
211
+ ];
212
+ const anthropicModelCatalog = Object.fromEntries(Object.entries({
213
+ "claude-opus-5-5": {
214
+ toolSearch: true,
215
+ compaction: true,
216
+ effort: true
217
+ },
218
+ "claude-opus-5": {
219
+ toolSearch: true,
220
+ compaction: true,
221
+ effort: true
222
+ },
223
+ "claude-sonnet-5-5": {
224
+ toolSearch: true,
225
+ compaction: true,
226
+ effort: true
227
+ },
228
+ "claude-sonnet-5": {
229
+ toolSearch: true,
230
+ compaction: true,
231
+ effort: true
232
+ },
233
+ "claude-haiku-5-5": {
234
+ toolSearch: true,
235
+ compaction: true,
236
+ effort: true
237
+ },
238
+ "claude-fable-5-1": {
239
+ toolSearch: true,
240
+ compaction: true,
241
+ effort: true
242
+ },
243
+ "claude-fable-5": {
244
+ toolSearch: true,
245
+ compaction: true,
246
+ effort: true
247
+ },
248
+ "claude-opus-4-8": {
249
+ toolSearch: true,
250
+ compaction: true,
251
+ effort: true
252
+ },
253
+ "claude-opus-4-7": {
254
+ toolSearch: true,
255
+ compaction: true,
256
+ effort: true
257
+ },
258
+ "claude-opus-4-6": {
259
+ toolSearch: true,
260
+ compaction: true,
261
+ effort: true
262
+ },
263
+ "claude-sonnet-4-6": {
264
+ toolSearch: true,
265
+ compaction: true,
266
+ effort: true
267
+ },
268
+ "claude-opus-4-5": {
269
+ toolSearch: true,
270
+ compaction: false,
271
+ effort: true
272
+ },
273
+ "claude-haiku-4-5": {
274
+ toolSearch: true,
275
+ compaction: false,
276
+ effort: false
277
+ },
278
+ "claude-sonnet-4-5": {
279
+ toolSearch: true,
280
+ compaction: false,
281
+ effort: false
282
+ },
283
+ "claude-opus-4-1": {
284
+ toolSearch: false,
285
+ compaction: false,
286
+ effort: false
287
+ },
288
+ "claude-opus-4-0": {
289
+ toolSearch: false,
290
+ compaction: false,
291
+ effort: false
292
+ },
293
+ "claude-sonnet-4-0": {
294
+ toolSearch: false,
295
+ compaction: false,
296
+ effort: false
297
+ }
298
+ }).map(([modelId, capabilities]) => [modelId, createAnthropicLanguageModelEntry(modelId, capabilities)]));
299
+ function getAnthropicLanguageModelEntry(modelId) {
300
+ return Object.prototype.hasOwnProperty.call(anthropicModelCatalog, modelId) ? anthropicModelCatalog[modelId] : createAnthropicLanguageModelEntry(modelId);
301
+ }
302
+ function createAnthropicLanguageModelEntry(modelId, documented) {
303
+ const runtime = resolveRuntimeDefinition(modelId);
304
+ const declared = documented != null;
305
+ const samplingSupport = runtime.samplingParameters ? true : { condition: "Only supported when thinking is disabled." };
306
+ const reasoningSupport = runtime.adaptiveThinking || runtime.budgetThinking;
307
+ return {
308
+ capabilities: { language: {
309
+ inputs: {
310
+ image: declared ? true : "unknown",
311
+ audio: declared ? false : "unknown"
312
+ },
313
+ objectGeneration: declared ? true : "unknown",
314
+ tools: {
315
+ usage: declared ? true : "unknown",
316
+ streaming: declared ? true : "unknown",
317
+ forcedChoice: runtime.forcedToolUse,
318
+ computerUse: declared ? true : "unknown",
319
+ webSearch: declared ? true : "unknown",
320
+ toolSearch: documented?.toolSearch ?? "unknown"
321
+ },
322
+ reasoning: {
323
+ support: reasoningSupport,
324
+ efforts: runtime.xhighEffort ? [
325
+ "low",
326
+ "medium",
327
+ "high",
328
+ "xhigh",
329
+ "max"
330
+ ] : void 0,
331
+ adaptive: runtime.adaptiveThinking,
332
+ budget: runtime.budgetThinking,
333
+ canDisable: runtime.thinkingCanBeDisabled,
334
+ canDisableAboveHighEffort: runtime.thinkingCanBeDisabledAboveHighEffort,
335
+ supportsXhighEffort: runtime.xhighEffort,
336
+ betweenTools: runtime.betweenToolsThinking
337
+ },
338
+ sampling: {
339
+ temperature: samplingSupport,
340
+ topP: samplingSupport,
341
+ topK: samplingSupport,
342
+ logprobs: false
343
+ },
344
+ structuredOutput: { native: runtime.nativeStructuredOutput },
345
+ compaction: documented?.compaction ?? "unknown"
346
+ } },
347
+ providerOptions: { language: declareProviderOptions(anthropicProviderOptions.language, declared ? true : "unknown", {
348
+ thinking: reasoningSupport,
349
+ effort: documented?.effort ?? "unknown",
350
+ speed: declared ? modelId === "claude-opus-4-6" : "unknown",
351
+ taskBudget: modelId === "claude-opus-4-7" ? true : "unknown"
352
+ }) },
353
+ requestConfig: { language: {
354
+ maxOutputTokens: runtime.maxOutputTokens,
355
+ knownModel: runtime.knownModel
356
+ } }
357
+ };
358
+ }
359
+ /** Applies one default support value to every option, except the listed ones. */
360
+ function declareProviderOptions(definitions, support, exceptions) {
361
+ return Object.fromEntries(Object.keys(definitions).map((option) => [option, exceptions[option] ?? support]));
362
+ }
363
+ function resolveRuntimeDefinition(modelId) {
364
+ const rule = runtimeRules.find((rule) => rule.matches(modelId));
365
+ if (rule != null) return rule.definition;
366
+ return modelId.includes("claude-") ? defineRuntime({
367
+ ...currentClaudeWithoutSampling,
368
+ thinkingCanBeDisabledAboveHighEffort: false,
369
+ knownModel: false
370
+ }) : defineRuntime({ knownModel: false });
371
+ }
372
+ /** Compatibility projection for the provider's existing request preparation. */
373
+ function getAnthropicModelCapabilities(modelId) {
374
+ const { capabilities: { language }, requestConfig: { language: requestConfig } } = getAnthropicLanguageModelEntry(modelId);
375
+ return {
376
+ maxOutputTokens: requestConfig.maxOutputTokens,
377
+ supportsStructuredOutput: language.structuredOutput.native,
378
+ supportsAdaptiveThinking: language.reasoning.adaptive,
379
+ rejectsSamplingParameters: language.sampling.temperature !== true,
380
+ supportsXhighEffort: language.reasoning.supportsXhighEffort,
381
+ rejectsThinkingDisabledAboveHighEffort: !language.reasoning.canDisableAboveHighEffort,
382
+ rejectsThinkingDisabled: !language.reasoning.canDisable,
383
+ rejectsBudgetThinking: !language.reasoning.budget,
384
+ rejectsForcedToolUse: !language.tools.forcedChoice,
385
+ supportsBetweenToolsThinking: language.reasoning.betweenTools,
386
+ isKnownModel: requestConfig.knownModel
387
+ };
388
+ }
389
+ //#endregion
66
390
  //#region src/map-anthropic-citation.ts
67
391
  function mapAnthropicCitation(citation, documents) {
68
392
  if (citation.type === "web_search_result_location" || citation.type === "search_result_location") {
@@ -3375,8 +3699,10 @@ function sanitizeDefinition(definition) {
3375
3699
  }
3376
3700
  function sanitizeSchema(schema) {
3377
3701
  const result = {};
3378
- const schemaWithDefs = schema;
3379
- if (schema.$ref != null) return { $ref: schema.$ref };
3702
+ if (schema.$ref != null) return {
3703
+ $ref: schema.$ref,
3704
+ ...sanitizeSchemaDefinitions(schema)
3705
+ };
3380
3706
  if (schema.$schema != null) result.$schema = schema.$schema;
3381
3707
  if (schema.$id != null) result.$id = schema.$id;
3382
3708
  if (schema.title != null) result.title = schema.title;
@@ -3388,11 +3714,7 @@ function sanitizeSchema(schema) {
3388
3714
  if (schema.anyOf != null) result.anyOf = schema.anyOf.map(sanitizeDefinition);
3389
3715
  else if (schema.oneOf != null) result.anyOf = schema.oneOf.map(sanitizeDefinition);
3390
3716
  if (schema.allOf != null) result.allOf = schema.allOf.map(sanitizeDefinition);
3391
- if (schema.definitions != null) result.definitions = Object.fromEntries(Object.entries(schema.definitions).map(([name, definition]) => [name, sanitizeDefinition(definition)]));
3392
- if (schemaWithDefs.$defs != null) {
3393
- const resultWithDefs = result;
3394
- resultWithDefs.$defs = Object.fromEntries(Object.entries(schemaWithDefs.$defs).map(([name, definition]) => [name, sanitizeDefinition(definition)]));
3395
- }
3717
+ Object.assign(result, sanitizeSchemaDefinitions(schema));
3396
3718
  if (schema.type === "object" || schema.properties != null) {
3397
3719
  if (schema.properties != null) result.properties = Object.fromEntries(Object.entries(schema.properties).map(([name, definition]) => [name, sanitizeDefinition(definition)]));
3398
3720
  result.additionalProperties = false;
@@ -3404,6 +3726,14 @@ function sanitizeSchema(schema) {
3404
3726
  if (constraintDescription != null) result.description = result.description == null ? constraintDescription : `${result.description}\n${constraintDescription}`;
3405
3727
  return result;
3406
3728
  }
3729
+ function sanitizeSchemaDefinitions(schema) {
3730
+ const { $defs } = schema;
3731
+ const sanitizeAll = (definitions) => Object.fromEntries(Object.entries(definitions).map(([name, definition]) => [name, sanitizeDefinition(definition)]));
3732
+ return {
3733
+ ...schema.definitions != null && { definitions: sanitizeAll(schema.definitions) },
3734
+ ...$defs != null && { $defs: sanitizeAll($defs) }
3735
+ };
3736
+ }
3407
3737
  function getConstraintDescription(schema) {
3408
3738
  const descriptions = DESCRIPTION_CONSTRAINT_KEYS.flatMap((key) => {
3409
3739
  const value = schema[key];
@@ -3421,6 +3751,155 @@ function formatConstraintValue(value) {
3421
3751
  return JSON.stringify(value);
3422
3752
  }
3423
3753
  //#endregion
3754
+ //#region src/has-circular-schema-reference.ts
3755
+ const SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
3756
+ "dependentSchemas",
3757
+ "patternProperties",
3758
+ "properties"
3759
+ ]);
3760
+ const DATA_KEYWORDS = /* @__PURE__ */ new Set([
3761
+ "const",
3762
+ "default",
3763
+ "enum",
3764
+ "examples"
3765
+ ]);
3766
+ /**
3767
+ * Whether a JSON schema contains a cycle of local `$ref`s, e.g. a tree node
3768
+ * whose children reference the node itself.
3769
+ *
3770
+ * Anthropic's native structured outputs reject such schemas, while the JSON
3771
+ * response tool accepts them.
3772
+ */
3773
+ function hasCircularSchemaReference(schema) {
3774
+ const visiting = /* @__PURE__ */ new Set();
3775
+ const visited = /* @__PURE__ */ new Set();
3776
+ const visit = (pointer) => {
3777
+ if (visiting.has(pointer)) return true;
3778
+ if (visited.has(pointer)) return false;
3779
+ const target = resolveSchemaPointer(schema, pointer);
3780
+ if (target === void 0) return false;
3781
+ visiting.add(pointer);
3782
+ const isCircular = collectLocalRefs(target).some(visit);
3783
+ visiting.delete(pointer);
3784
+ visited.add(pointer);
3785
+ return isCircular;
3786
+ };
3787
+ return visit("#");
3788
+ }
3789
+ function collectLocalRefs(node, refs = []) {
3790
+ if (Array.isArray(node)) {
3791
+ for (const item of node) collectLocalRefs(item, refs);
3792
+ return refs;
3793
+ }
3794
+ if (!isRecord(node)) return refs;
3795
+ for (const [key, value] of Object.entries(node)) if (key === "$ref") {
3796
+ if (typeof value === "string" && value.startsWith("#")) refs.push(value);
3797
+ } else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) for (const subschema of Object.values(value)) collectLocalRefs(subschema, refs);
3798
+ else if (key !== "$defs" && key !== "definitions" && !DATA_KEYWORDS.has(key)) collectLocalRefs(value, refs);
3799
+ return refs;
3800
+ }
3801
+ function resolveSchemaPointer(schema, pointer) {
3802
+ if (pointer === "#") return schema;
3803
+ if (!pointer.startsWith("#/")) return;
3804
+ let decodedPointer;
3805
+ try {
3806
+ decodedPointer = decodeURIComponent(pointer.slice(2));
3807
+ } catch {
3808
+ return;
3809
+ }
3810
+ let current = schema;
3811
+ for (const rawSegment of decodedPointer.split("/")) {
3812
+ const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
3813
+ if (Array.isArray(current)) current = current[Number(segment)];
3814
+ else if (isRecord(current)) current = current[segment];
3815
+ else return;
3816
+ }
3817
+ return current;
3818
+ }
3819
+ //#endregion
3820
+ //#region src/supports-native-structured-output-schema.ts
3821
+ const MAX_OPTIONAL_PROPERTIES = 10;
3822
+ const SUBSCHEMA_KEYWORDS = [
3823
+ "additionalProperties",
3824
+ "contains",
3825
+ "else",
3826
+ "if",
3827
+ "items",
3828
+ "not",
3829
+ "then"
3830
+ ];
3831
+ const SUBSCHEMA_LIST_KEYWORDS = [
3832
+ "allOf",
3833
+ "anyOf",
3834
+ "oneOf",
3835
+ "prefixItems"
3836
+ ];
3837
+ const SUBSCHEMA_MAP_KEYWORDS = [
3838
+ "dependentSchemas",
3839
+ "patternProperties",
3840
+ "properties"
3841
+ ];
3842
+ /**
3843
+ * Whether Anthropic's native structured outputs (`output_config.format`) can
3844
+ * represent a schema without rejecting it or losing data.
3845
+ *
3846
+ * Rejected: self-referencing schemas, and schemas with too many optional
3847
+ * properties. Lossy: open objects (maps, free-form objects), which native
3848
+ * structured outputs close with `additionalProperties: false`, so the model
3849
+ * can only return `{}` for them.
3850
+ */
3851
+ function supportsNativeStructuredOutputSchema(schema) {
3852
+ if (hasCircularSchemaReference(schema)) return false;
3853
+ const { optionalProperties, hasOpenObject } = getSchemaStats(schema, schema, /* @__PURE__ */ new Map());
3854
+ return !hasOpenObject && optionalProperties <= MAX_OPTIONAL_PROPERTIES;
3855
+ }
3856
+ function getSchemaStats(node, root, refStats) {
3857
+ const stats = {
3858
+ optionalProperties: 0,
3859
+ hasOpenObject: false
3860
+ };
3861
+ if (!isRecord(node)) return stats;
3862
+ const add = (child) => {
3863
+ const childStats = getSchemaStats(child, root, refStats);
3864
+ stats.optionalProperties += childStats.optionalProperties;
3865
+ stats.hasOpenObject ||= childStats.hasOpenObject;
3866
+ };
3867
+ if (typeof node.$ref === "string" && node.$ref.startsWith("#")) {
3868
+ let resolved = refStats.get(node.$ref);
3869
+ if (resolved == null) {
3870
+ resolved = getSchemaStats(resolveSchemaPointer(root, node.$ref), root, refStats);
3871
+ refStats.set(node.$ref, resolved);
3872
+ }
3873
+ stats.optionalProperties += resolved.optionalProperties;
3874
+ stats.hasOpenObject ||= resolved.hasOpenObject;
3875
+ }
3876
+ if (isOpenObject(node)) stats.hasOpenObject = true;
3877
+ if (isRecord(node.properties)) {
3878
+ const required = new Set(Array.isArray(node.required) ? node.required : []);
3879
+ stats.optionalProperties += Object.keys(node.properties).filter((name) => !required.has(name)).length;
3880
+ }
3881
+ for (const keyword of SUBSCHEMA_KEYWORDS) {
3882
+ const value = node[keyword];
3883
+ if (Array.isArray(value)) value.forEach(add);
3884
+ else add(value);
3885
+ }
3886
+ for (const keyword of SUBSCHEMA_LIST_KEYWORDS) {
3887
+ const value = node[keyword];
3888
+ if (Array.isArray(value)) value.forEach(add);
3889
+ }
3890
+ for (const keyword of SUBSCHEMA_MAP_KEYWORDS) {
3891
+ const value = node[keyword];
3892
+ if (isRecord(value)) Object.values(value).forEach(add);
3893
+ }
3894
+ return stats;
3895
+ }
3896
+ function isOpenObject(node) {
3897
+ const { additionalProperties, patternProperties, properties, type } = node;
3898
+ if (additionalProperties === true || isRecord(additionalProperties)) return true;
3899
+ if (isRecord(patternProperties) && Object.keys(patternProperties).length > 0) return true;
3900
+ return (type === "object" || Array.isArray(type) && type.includes("object")) && properties == null && additionalProperties !== false;
3901
+ }
3902
+ //#endregion
3424
3903
  //#region src/anthropic-language-model.ts
3425
3904
  function createAnthropicStreamError(error) {
3426
3905
  const inferredMetadata = getAnthropicStreamErrorMetadata(error.type);
@@ -3539,6 +4018,28 @@ function getProviderOptionsName(provider) {
3539
4018
  const dotIndex = provider.indexOf(".");
3540
4019
  return dotIndex === -1 ? provider : provider.substring(0, dotIndex);
3541
4020
  }
4021
+ const TEXT_OR_THINKING_BLOCK_TYPES = /* @__PURE__ */ new Set([
4022
+ "redacted_thinking",
4023
+ "text",
4024
+ "thinking"
4025
+ ]);
4026
+ function isContinuationStopReason(stopReason) {
4027
+ return stopReason === "pause_turn" || stopReason === "tool_use";
4028
+ }
4029
+ /**
4030
+ * Text parts to return when the JSON response tool is in use: the text after
4031
+ * the last tool call or result, and only if the tool was never called.
4032
+ */
4033
+ function getJsonResponseToolFallbackText(content, stopReason) {
4034
+ const text = /* @__PURE__ */ new Set();
4035
+ if (isContinuationStopReason(stopReason)) return text;
4036
+ for (const part of content) {
4037
+ if (part.type === "tool_use" && part.name === "json") return /* @__PURE__ */ new Set();
4038
+ if (part.type === "text") text.add(part);
4039
+ else if (!TEXT_OR_THINKING_BLOCK_TYPES.has(part.type)) text.clear();
4040
+ }
4041
+ return text;
4042
+ }
3542
4043
  var AnthropicLanguageModel = class AnthropicLanguageModel {
3543
4044
  static [WORKFLOW_SERIALIZE](model) {
3544
4045
  return serializeModelOptions({
@@ -3615,7 +4116,7 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
3615
4116
  }) : null;
3616
4117
  const usedCustomProviderKey = customProviderOptions != null;
3617
4118
  const anthropicOptions = Object.assign({}, canonicalOptions ?? {}, customProviderOptions ?? {});
3618
- const { maxOutputTokens: maxOutputTokensForModel, supportsStructuredOutput: modelSupportsStructuredOutput, supportsAdaptiveThinking, rejectsSamplingParameters, supportsXhighEffort, rejectsThinkingDisabledAboveHighEffort, rejectsThinkingDisabled, rejectsBudgetThinking, rejectsForcedToolUse, supportsBetweenToolsThinking, isKnownModel } = getModelCapabilities(modelId);
4119
+ const { maxOutputTokens: maxOutputTokensForModel, supportsStructuredOutput: modelSupportsStructuredOutput, supportsAdaptiveThinking, rejectsSamplingParameters, supportsXhighEffort, rejectsThinkingDisabledAboveHighEffort, rejectsThinkingDisabled, rejectsBudgetThinking, rejectsForcedToolUse, supportsBetweenToolsThinking, isKnownModel } = getAnthropicModelCapabilities(modelId);
3619
4120
  if (!isKnownModel && maxOutputTokens == null) warnings.push({
3620
4121
  type: "compatibility",
3621
4122
  feature: "maxOutputTokens",
@@ -3652,7 +4153,7 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
3652
4153
  const supportsStrictTools = (config.supportsStrictTools ?? true) && modelSupportsStructuredOutput;
3653
4154
  const structureOutputMode = anthropicOptions?.structuredOutputMode ?? "auto";
3654
4155
  let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
3655
- if (!useStructuredOutput && rejectsForcedToolUse && supportsStructuredOutput && responseFormat?.type === "json" && responseFormat.schema != null) {
4156
+ if (!useStructuredOutput && rejectsForcedToolUse && modelSupportsStructuredOutput && responseFormat?.type === "json" && responseFormat.schema != null && supportsNativeStructuredOutputSchema(responseFormat.schema)) {
3656
4157
  warnings.push({
3657
4158
  type: "unsupported",
3658
4159
  feature: "providerOptions.anthropic.structuredOutputMode",
@@ -4059,9 +4560,10 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
4059
4560
  const serverToolCalls = {};
4060
4561
  let isJsonResponseFromTool = false;
4061
4562
  const hasWebSearchResults = response.content.some((part) => part.type === "web_search_tool_result" && Array.isArray(part.content) && part.content.length > 0);
4563
+ const jsonResponseToolFallbackText = usesJsonResponseTool ? getJsonResponseToolFallbackText(response.content, response.stop_reason) : void 0;
4062
4564
  for (const part of response.content) switch (part.type) {
4063
4565
  case "text":
4064
- if (!usesJsonResponseTool) {
4566
+ if (jsonResponseToolFallbackText == null || jsonResponseToolFallbackText.has(part)) {
4065
4567
  const citations = part.citations ?? [];
4066
4568
  content.push({
4067
4569
  type: "text",
@@ -4499,6 +5001,16 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
4499
5001
  let safeguardResults;
4500
5002
  let container = null;
4501
5003
  let isJsonResponseFromTool = false;
5004
+ const heldBackText = [];
5005
+ const heldBackTextByIndex = /* @__PURE__ */ new Map();
5006
+ const createTextEnd = (id, citations) => ({
5007
+ type: "text-end",
5008
+ id,
5009
+ ...citations.length > 0 && {
5010
+ citations: citations.map((citation) => mapAnthropicCitation(citation, citationDocuments)),
5011
+ providerMetadata: { anthropic: { citations } }
5012
+ }
5013
+ });
4502
5014
  let hasWebSearchResults = false;
4503
5015
  const fallbackWebCitations = [];
4504
5016
  let isMessageOpen = false;
@@ -4545,9 +5057,19 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
4545
5057
  return;
4546
5058
  }
4547
5059
  blockType = contentBlockType;
5060
+ if (!TEXT_OR_THINKING_BLOCK_TYPES.has(contentBlockType)) heldBackText.length = 0;
4548
5061
  switch (contentBlockType) {
4549
5062
  case "text":
4550
- if (usesJsonResponseTool) return;
5063
+ if (usesJsonResponseTool) {
5064
+ const entry = {
5065
+ id: String(value.index),
5066
+ text: "",
5067
+ citations: []
5068
+ };
5069
+ heldBackText.push(entry);
5070
+ heldBackTextByIndex.set(value.index, entry);
5071
+ return;
5072
+ }
4551
5073
  contentBlocks[value.index] = {
4552
5074
  type: "text",
4553
5075
  citations: []
@@ -4933,14 +5455,7 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
4933
5455
  const contentBlock = contentBlocks[value.index];
4934
5456
  switch (contentBlock.type) {
4935
5457
  case "text":
4936
- controller.enqueue({
4937
- type: "text-end",
4938
- id: String(value.index),
4939
- ...contentBlock.citations.length > 0 && {
4940
- citations: contentBlock.citations.map((citation) => mapAnthropicCitation(citation, citationDocuments)),
4941
- providerMetadata: { anthropic: { citations: contentBlock.citations } }
4942
- }
4943
- });
5458
+ controller.enqueue(createTextEnd(String(value.index), contentBlock.citations));
4944
5459
  break;
4945
5460
  case "reasoning":
4946
5461
  controller.enqueue({
@@ -5000,7 +5515,11 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
5000
5515
  const deltaType = value.delta.type;
5001
5516
  switch (deltaType) {
5002
5517
  case "text_delta":
5003
- if (usesJsonResponseTool) return;
5518
+ if (usesJsonResponseTool) {
5519
+ const entry = heldBackTextByIndex.get(value.index);
5520
+ if (entry != null) entry.text += value.delta.text;
5521
+ return;
5522
+ }
5004
5523
  controller.enqueue({
5005
5524
  type: "text-delta",
5006
5525
  id: String(value.index),
@@ -5061,6 +5580,7 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
5061
5580
  const citation = value.delta.citation;
5062
5581
  const contentBlock = contentBlocks[value.index];
5063
5582
  if (contentBlock?.type === "text") contentBlock.citations.push(citation);
5583
+ heldBackTextByIndex.get(value.index)?.citations.push(citation);
5064
5584
  if (citation.type === "web_search_result_location") fallbackWebCitations.push(citation);
5065
5585
  else {
5066
5586
  const source = createCitationSource(citation, citationDocuments, generateId);
@@ -5086,6 +5606,7 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
5086
5606
  }
5087
5607
  isMessageOpen = true;
5088
5608
  activeMessageId = value.message.id;
5609
+ heldBackTextByIndex.clear();
5089
5610
  usage.input_tokens = value.message.usage.input_tokens;
5090
5611
  usage.cache_read_input_tokens = value.message.usage.cache_read_input_tokens ?? 0;
5091
5612
  usage.cache_creation_input_tokens = value.message.usage.cache_creation_input_tokens ?? 0;
@@ -5206,6 +5727,20 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
5206
5727
  if (source) controller.enqueue(source);
5207
5728
  }
5208
5729
  fallbackWebCitations.length = 0;
5730
+ if (!isJsonResponseFromTool && !isContinuationStopReason(finishReason.raw)) for (const { id, text, citations } of heldBackText) {
5731
+ if (text.length === 0) continue;
5732
+ controller.enqueue({
5733
+ type: "text-start",
5734
+ id
5735
+ });
5736
+ controller.enqueue({
5737
+ type: "text-delta",
5738
+ id,
5739
+ delta: text
5740
+ });
5741
+ controller.enqueue(createTextEnd(id, citations));
5742
+ }
5743
+ heldBackText.length = 0;
5209
5744
  controller.enqueue({
5210
5745
  type: "finish",
5211
5746
  finishReason,
@@ -5305,222 +5840,6 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
5305
5840
  };
5306
5841
  }
5307
5842
  };
5308
- /**
5309
- * Returns the capabilities of a Claude model that are used for defaults and feature selection.
5310
- *
5311
- * @see https://docs.claude.com/en/docs/about-claude/models/overview#model-comparison-table
5312
- * @see https://platform.claude.com/docs/en/build-with-claude/structured-outputs
5313
- */
5314
- function getModelCapabilities(modelId) {
5315
- if (modelId.includes("claude-sonnet-5-5")) return {
5316
- maxOutputTokens: 128e3,
5317
- supportsStructuredOutput: true,
5318
- supportsAdaptiveThinking: true,
5319
- rejectsSamplingParameters: true,
5320
- supportsXhighEffort: true,
5321
- rejectsThinkingDisabledAboveHighEffort: true,
5322
- rejectsThinkingDisabled: true,
5323
- rejectsBudgetThinking: true,
5324
- rejectsForcedToolUse: true,
5325
- supportsBetweenToolsThinking: true,
5326
- isKnownModel: true
5327
- };
5328
- else if (modelId.includes("claude-opus-5-5")) return {
5329
- maxOutputTokens: 128e3,
5330
- supportsStructuredOutput: true,
5331
- supportsAdaptiveThinking: true,
5332
- rejectsSamplingParameters: true,
5333
- supportsXhighEffort: true,
5334
- rejectsThinkingDisabledAboveHighEffort: true,
5335
- rejectsThinkingDisabled: true,
5336
- rejectsBudgetThinking: true,
5337
- rejectsForcedToolUse: true,
5338
- supportsBetweenToolsThinking: false,
5339
- isKnownModel: true
5340
- };
5341
- else if (modelId.includes("claude-haiku-5-5")) return {
5342
- maxOutputTokens: 128e3,
5343
- supportsStructuredOutput: true,
5344
- supportsAdaptiveThinking: true,
5345
- rejectsSamplingParameters: true,
5346
- supportsXhighEffort: true,
5347
- rejectsThinkingDisabledAboveHighEffort: true,
5348
- rejectsThinkingDisabled: false,
5349
- rejectsBudgetThinking: true,
5350
- rejectsForcedToolUse: false,
5351
- supportsBetweenToolsThinking: false,
5352
- isKnownModel: true
5353
- };
5354
- else if (modelId.includes("claude-opus-5")) return {
5355
- maxOutputTokens: 128e3,
5356
- supportsStructuredOutput: true,
5357
- supportsAdaptiveThinking: true,
5358
- rejectsSamplingParameters: true,
5359
- supportsXhighEffort: true,
5360
- rejectsThinkingDisabledAboveHighEffort: true,
5361
- rejectsThinkingDisabled: false,
5362
- rejectsBudgetThinking: false,
5363
- rejectsForcedToolUse: false,
5364
- supportsBetweenToolsThinking: false,
5365
- isKnownModel: true
5366
- };
5367
- else if (modelId.includes("claude-fable-5-1")) return {
5368
- maxOutputTokens: 128e3,
5369
- supportsStructuredOutput: true,
5370
- supportsAdaptiveThinking: true,
5371
- rejectsSamplingParameters: true,
5372
- supportsXhighEffort: true,
5373
- rejectsThinkingDisabledAboveHighEffort: false,
5374
- rejectsThinkingDisabled: true,
5375
- rejectsBudgetThinking: true,
5376
- rejectsForcedToolUse: true,
5377
- supportsBetweenToolsThinking: false,
5378
- isKnownModel: true
5379
- };
5380
- else if (modelId.includes("claude-fable-5")) return {
5381
- maxOutputTokens: 128e3,
5382
- supportsStructuredOutput: true,
5383
- supportsAdaptiveThinking: true,
5384
- rejectsSamplingParameters: true,
5385
- supportsXhighEffort: true,
5386
- rejectsThinkingDisabledAboveHighEffort: false,
5387
- rejectsThinkingDisabled: true,
5388
- rejectsBudgetThinking: true,
5389
- rejectsForcedToolUse: false,
5390
- supportsBetweenToolsThinking: false,
5391
- isKnownModel: true
5392
- };
5393
- else if (modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7") || modelId.includes("claude-sonnet-5")) return {
5394
- maxOutputTokens: 128e3,
5395
- supportsStructuredOutput: true,
5396
- supportsAdaptiveThinking: true,
5397
- rejectsSamplingParameters: true,
5398
- supportsXhighEffort: true,
5399
- rejectsThinkingDisabledAboveHighEffort: false,
5400
- rejectsThinkingDisabled: false,
5401
- rejectsBudgetThinking: false,
5402
- rejectsForcedToolUse: false,
5403
- supportsBetweenToolsThinking: false,
5404
- isKnownModel: true
5405
- };
5406
- else if (modelId.includes("claude-sonnet-4-6") || modelId.includes("claude-opus-4-6")) return {
5407
- maxOutputTokens: 128e3,
5408
- supportsStructuredOutput: true,
5409
- supportsAdaptiveThinking: true,
5410
- rejectsSamplingParameters: false,
5411
- supportsXhighEffort: false,
5412
- rejectsThinkingDisabledAboveHighEffort: false,
5413
- rejectsThinkingDisabled: false,
5414
- rejectsBudgetThinking: false,
5415
- rejectsForcedToolUse: false,
5416
- supportsBetweenToolsThinking: false,
5417
- isKnownModel: true
5418
- };
5419
- else if (modelId.includes("claude-sonnet-4-5") || modelId.includes("claude-opus-4-5") || modelId.includes("claude-haiku-4-5")) return {
5420
- maxOutputTokens: 64e3,
5421
- supportsStructuredOutput: true,
5422
- supportsAdaptiveThinking: false,
5423
- rejectsSamplingParameters: false,
5424
- supportsXhighEffort: false,
5425
- rejectsThinkingDisabledAboveHighEffort: false,
5426
- rejectsThinkingDisabled: false,
5427
- rejectsBudgetThinking: false,
5428
- rejectsForcedToolUse: false,
5429
- supportsBetweenToolsThinking: false,
5430
- isKnownModel: true
5431
- };
5432
- else if (modelId.includes("claude-opus-4-1")) return {
5433
- maxOutputTokens: 32e3,
5434
- supportsStructuredOutput: true,
5435
- supportsAdaptiveThinking: false,
5436
- rejectsSamplingParameters: false,
5437
- supportsXhighEffort: false,
5438
- rejectsThinkingDisabledAboveHighEffort: false,
5439
- rejectsThinkingDisabled: false,
5440
- rejectsBudgetThinking: false,
5441
- rejectsForcedToolUse: false,
5442
- supportsBetweenToolsThinking: false,
5443
- isKnownModel: true
5444
- };
5445
- else if (/claude-sonnet-4(?:-|@)/.test(modelId)) return {
5446
- maxOutputTokens: 64e3,
5447
- supportsStructuredOutput: false,
5448
- supportsAdaptiveThinking: false,
5449
- rejectsSamplingParameters: false,
5450
- supportsXhighEffort: false,
5451
- rejectsThinkingDisabledAboveHighEffort: false,
5452
- rejectsThinkingDisabled: false,
5453
- rejectsBudgetThinking: false,
5454
- rejectsForcedToolUse: false,
5455
- supportsBetweenToolsThinking: false,
5456
- isKnownModel: true
5457
- };
5458
- else if (/claude-opus-4(?:-|@)/.test(modelId)) return {
5459
- maxOutputTokens: 32e3,
5460
- supportsStructuredOutput: false,
5461
- supportsAdaptiveThinking: false,
5462
- rejectsSamplingParameters: false,
5463
- supportsXhighEffort: false,
5464
- rejectsThinkingDisabledAboveHighEffort: false,
5465
- rejectsThinkingDisabled: false,
5466
- rejectsBudgetThinking: false,
5467
- rejectsForcedToolUse: false,
5468
- supportsBetweenToolsThinking: false,
5469
- isKnownModel: true
5470
- };
5471
- else if (modelId.includes("claude-3-haiku")) return {
5472
- maxOutputTokens: 4096,
5473
- supportsStructuredOutput: false,
5474
- supportsAdaptiveThinking: false,
5475
- rejectsSamplingParameters: false,
5476
- supportsXhighEffort: false,
5477
- rejectsThinkingDisabledAboveHighEffort: false,
5478
- rejectsThinkingDisabled: false,
5479
- rejectsBudgetThinking: false,
5480
- rejectsForcedToolUse: false,
5481
- supportsBetweenToolsThinking: false,
5482
- isKnownModel: true
5483
- };
5484
- else if (/claude-(?:instant(?:-|$)|v?2(?=$|[-.:])|3(?=$|[-.]))/.test(modelId)) return {
5485
- maxOutputTokens: 4096,
5486
- supportsStructuredOutput: false,
5487
- supportsAdaptiveThinking: false,
5488
- rejectsSamplingParameters: false,
5489
- supportsXhighEffort: false,
5490
- rejectsThinkingDisabledAboveHighEffort: false,
5491
- rejectsThinkingDisabled: false,
5492
- rejectsBudgetThinking: false,
5493
- rejectsForcedToolUse: false,
5494
- supportsBetweenToolsThinking: false,
5495
- isKnownModel: false
5496
- };
5497
- else if (modelId.includes("claude-")) return {
5498
- maxOutputTokens: 128e3,
5499
- supportsStructuredOutput: true,
5500
- supportsAdaptiveThinking: true,
5501
- rejectsSamplingParameters: true,
5502
- supportsXhighEffort: true,
5503
- rejectsThinkingDisabledAboveHighEffort: true,
5504
- rejectsThinkingDisabled: false,
5505
- rejectsBudgetThinking: false,
5506
- rejectsForcedToolUse: false,
5507
- supportsBetweenToolsThinking: false,
5508
- isKnownModel: false
5509
- };
5510
- else return {
5511
- maxOutputTokens: 4096,
5512
- supportsStructuredOutput: false,
5513
- supportsAdaptiveThinking: false,
5514
- rejectsSamplingParameters: false,
5515
- supportsXhighEffort: false,
5516
- rejectsThinkingDisabledAboveHighEffort: false,
5517
- rejectsThinkingDisabled: false,
5518
- rejectsBudgetThinking: false,
5519
- rejectsForcedToolUse: false,
5520
- supportsBetweenToolsThinking: false,
5521
- isKnownModel: false
5522
- };
5523
- }
5524
5843
  function hasDynamicFilteringWebToolWithoutCodeExecution(tools) {
5525
5844
  if (!tools) return false;
5526
5845
  let hasDynamicFilteringWebTool = false;
@@ -6293,6 +6612,6 @@ var AnthropicSkills = class {
6293
6612
  }
6294
6613
  };
6295
6614
  //#endregion
6296
- export { AnthropicFiles, AnthropicLanguageModel, AnthropicLanguageModel as AnthropicMessagesLanguageModel, AnthropicSkills, anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema };
6615
+ export { AnthropicFiles, AnthropicLanguageModel, AnthropicLanguageModel as AnthropicMessagesLanguageModel, AnthropicSkills, anthropicTools, getAnthropicModelCapabilities as getModelCapabilities, prepareTools, sanitizeJsonSchema };
6297
6616
 
6298
6617
  //# sourceMappingURL=index.js.map