@ai-sdk/amazon-bedrock 4.0.167 → 4.0.169
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 +14 -0
- package/dist/anthropic/index.js +14 -8
- package/dist/anthropic/index.js.map +1 -1
- package/dist/anthropic/index.mjs +14 -8
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +61 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +61 -24
- package/dist/index.mjs.map +1 -1
- package/dist/mantle/index.js +1 -1
- package/dist/mantle/index.mjs +1 -1
- package/docs/08-amazon-bedrock.mdx +45 -0
- package/package.json +2 -2
- package/src/bedrock-anthropic-model-support.ts +17 -7
- package/src/bedrock-chat-language-model.ts +49 -5
- package/src/bedrock-chat-options.ts +8 -0
package/dist/mantle/index.js
CHANGED
|
@@ -35,7 +35,7 @@ var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
|
35
35
|
var import_aws4fetch = require("aws4fetch");
|
|
36
36
|
|
|
37
37
|
// src/version.ts
|
|
38
|
-
var VERSION = true ? "4.0.
|
|
38
|
+
var VERSION = true ? "4.0.169" : "0.0.0-test";
|
|
39
39
|
|
|
40
40
|
// src/bedrock-sigv4-fetch.ts
|
|
41
41
|
function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
|
package/dist/mantle/index.mjs
CHANGED
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
import { AwsV4Signer } from "aws4fetch";
|
|
24
24
|
|
|
25
25
|
// src/version.ts
|
|
26
|
-
var VERSION = true ? "4.0.
|
|
26
|
+
var VERSION = true ? "4.0.169" : "0.0.0-test";
|
|
27
27
|
|
|
28
28
|
// src/bedrock-sigv4-fetch.ts
|
|
29
29
|
function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
|
|
@@ -256,6 +256,51 @@ const { text } = await generateText({
|
|
|
256
256
|
Amazon Bedrock language models can also be used in the `streamText` function
|
|
257
257
|
(see [AI SDK Core](/docs/ai-sdk-core)).
|
|
258
258
|
|
|
259
|
+
### Structured Outputs
|
|
260
|
+
|
|
261
|
+
For Anthropic models, you can use the `structuredOutputMode` provider option to
|
|
262
|
+
choose how Amazon Bedrock generates structured outputs:
|
|
263
|
+
|
|
264
|
+
- `"outputFormat"`: Use Bedrock's native `output_config.format` parameter.
|
|
265
|
+
- `"jsonTool"`: Use a special `json` tool with required tool choice.
|
|
266
|
+
- `"auto"`: Use native structured output when supported and otherwise fall
|
|
267
|
+
back to the JSON tool. This is the default.
|
|
268
|
+
|
|
269
|
+
In `"auto"` mode, Claude Sonnet 4.6 and Claude Haiku 4.5 use the JSON tool
|
|
270
|
+
fallback because native structured output can be unreliable across workloads
|
|
271
|
+
and Bedrock accounts. You can set `"outputFormat"` to opt into native structured
|
|
272
|
+
output when it works for your account and workload, or use `"jsonTool"` to force
|
|
273
|
+
the fallback for any model:
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
import {
|
|
277
|
+
bedrock,
|
|
278
|
+
type AmazonBedrockLanguageModelOptions,
|
|
279
|
+
} from '@ai-sdk/amazon-bedrock';
|
|
280
|
+
import { generateText, Output } from 'ai';
|
|
281
|
+
import { z } from 'zod';
|
|
282
|
+
|
|
283
|
+
const { output } = await generateText({
|
|
284
|
+
model: bedrock('eu.anthropic.claude-sonnet-4-6'),
|
|
285
|
+
output: Output.object({
|
|
286
|
+
schema: z.object({
|
|
287
|
+
summary: z.string(),
|
|
288
|
+
findings: z.array(z.string()),
|
|
289
|
+
}),
|
|
290
|
+
}),
|
|
291
|
+
prompt: 'Summarize the inspection report.',
|
|
292
|
+
providerOptions: {
|
|
293
|
+
bedrock: {
|
|
294
|
+
structuredOutputMode: 'jsonTool',
|
|
295
|
+
} satisfies AmazonBedrockLanguageModelOptions,
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
For parity with the Anthropic provider,
|
|
301
|
+
`providerOptions.anthropic.structuredOutputMode` is also honored for Anthropic
|
|
302
|
+
models on Bedrock. When both are provided, the `bedrock` value takes precedence.
|
|
303
|
+
|
|
259
304
|
### File Inputs
|
|
260
305
|
|
|
261
306
|
<Note type="warning">
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/amazon-bedrock",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.169",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@smithy/eventstream-codec": "^4.0.1",
|
|
45
45
|
"@smithy/util-utf8": "^4.0.0",
|
|
46
46
|
"aws4fetch": "^1.0.20",
|
|
47
|
-
"@ai-sdk/anthropic": "3.0.
|
|
47
|
+
"@ai-sdk/anthropic": "3.0.116",
|
|
48
48
|
"@ai-sdk/openai": "3.0.106",
|
|
49
49
|
"@ai-sdk/provider": "3.0.15",
|
|
50
50
|
"@ai-sdk/provider-utils": "4.0.50"
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
export function supportsStrictTools(modelId: string): boolean {
|
|
2
|
-
return !
|
|
2
|
+
return !matchesModel(modelId, MODELS_WITHOUT_STRICT_TOOL_SUPPORT);
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export function supportsNativeStructuredOutput(modelId: string): boolean {
|
|
6
|
-
return !
|
|
6
|
+
return !matchesModel(
|
|
7
|
+
modelId,
|
|
8
|
+
MODELS_WITHOUT_RELIABLE_NATIVE_STRUCTURED_OUTPUT,
|
|
9
|
+
);
|
|
7
10
|
}
|
|
8
11
|
|
|
9
12
|
// Bedrock validates against its own copy of the Messages schema, which rejects
|
|
10
13
|
// `output_config.format` and tool `strict` for the newest Claude models
|
|
11
|
-
const
|
|
14
|
+
const MODELS_WITHOUT_STRICT_TOOL_SUPPORT = [
|
|
12
15
|
'claude-opus-4-7',
|
|
13
16
|
'claude-opus-4-8',
|
|
14
17
|
'claude-opus-5',
|
|
@@ -16,8 +19,15 @@ const MODELS_REJECTING_NEWER_SCHEMA_FIELDS = [
|
|
|
16
19
|
'claude-sonnet-5',
|
|
17
20
|
];
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
// Native structured output is unreliable for additional models even though
|
|
23
|
+
// their strict tool support remains available. Sonnet 4.6 can fail to adhere
|
|
24
|
+
// to complex schemas, while Haiku 4.5 support varies between Bedrock accounts.
|
|
25
|
+
const MODELS_WITHOUT_RELIABLE_NATIVE_STRUCTURED_OUTPUT = [
|
|
26
|
+
...MODELS_WITHOUT_STRICT_TOOL_SUPPORT,
|
|
27
|
+
'claude-sonnet-4-6',
|
|
28
|
+
'claude-haiku-4-5',
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
function matchesModel(modelId: string, models: string[]): boolean {
|
|
32
|
+
return models.some(model => modelId.includes(model));
|
|
23
33
|
}
|
|
@@ -61,8 +61,13 @@ type BedrockChatConfig = {
|
|
|
61
61
|
generateId: () => string;
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
+
function isJsonResponseToolName(name: string): boolean {
|
|
65
|
+
return name === 'json' || name === 'json<|channel|>commentary';
|
|
66
|
+
}
|
|
67
|
+
|
|
64
68
|
const anthropicProviderOptions = z.object({
|
|
65
69
|
disableParallelToolUse: z.boolean().optional(),
|
|
70
|
+
structuredOutputMode: z.enum(['outputFormat', 'jsonTool', 'auto']).optional(),
|
|
66
71
|
});
|
|
67
72
|
|
|
68
73
|
export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
@@ -177,14 +182,52 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
177
182
|
const { supportsStructuredOutput: modelSupportsStructuredOutput } =
|
|
178
183
|
getModelCapabilities(this.modelId);
|
|
179
184
|
|
|
185
|
+
const structuredOutputMode =
|
|
186
|
+
bedrockOptions.structuredOutputMode ??
|
|
187
|
+
anthropicOptions?.structuredOutputMode ??
|
|
188
|
+
'auto';
|
|
189
|
+
|
|
190
|
+
if (structuredOutputMode === 'jsonTool') {
|
|
191
|
+
const additionalModelRequestFields = {
|
|
192
|
+
...bedrockOptions.additionalModelRequestFields,
|
|
193
|
+
};
|
|
194
|
+
const outputConfig = additionalModelRequestFields.output_config;
|
|
195
|
+
|
|
196
|
+
if (
|
|
197
|
+
outputConfig != null &&
|
|
198
|
+
typeof outputConfig === 'object' &&
|
|
199
|
+
!Array.isArray(outputConfig)
|
|
200
|
+
) {
|
|
201
|
+
const outputConfigWithoutFormat = { ...outputConfig };
|
|
202
|
+
delete outputConfigWithoutFormat.format;
|
|
203
|
+
|
|
204
|
+
if (Object.keys(outputConfigWithoutFormat).length > 0) {
|
|
205
|
+
additionalModelRequestFields.output_config =
|
|
206
|
+
outputConfigWithoutFormat;
|
|
207
|
+
} else {
|
|
208
|
+
delete additionalModelRequestFields.output_config;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
bedrockOptions.additionalModelRequestFields =
|
|
212
|
+
additionalModelRequestFields;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const modelSupportsNativeStructuredOutput =
|
|
217
|
+
supportsNativeStructuredOutput(this.modelId) &&
|
|
218
|
+
(modelSupportsStructuredOutput || isThinkingEnabled);
|
|
219
|
+
|
|
180
220
|
const useNativeStructuredOutput =
|
|
181
221
|
isAnthropicModel &&
|
|
182
|
-
supportsNativeStructuredOutput(this.modelId) &&
|
|
183
|
-
(modelSupportsStructuredOutput || isThinkingEnabled) &&
|
|
184
222
|
responseFormat?.type === 'json' &&
|
|
185
|
-
responseFormat.schema != null
|
|
223
|
+
responseFormat.schema != null &&
|
|
224
|
+
(structuredOutputMode === 'outputFormat' ||
|
|
225
|
+
(structuredOutputMode === 'auto' &&
|
|
226
|
+
modelSupportsNativeStructuredOutput));
|
|
186
227
|
|
|
187
228
|
const useJsonInstructionForStructuredOutput =
|
|
229
|
+
!useNativeStructuredOutput &&
|
|
230
|
+
structuredOutputMode !== 'jsonTool' &&
|
|
188
231
|
isAnthropicModel &&
|
|
189
232
|
!supportsStrictTools(this.modelId) &&
|
|
190
233
|
responseFormat?.type === 'json' &&
|
|
@@ -438,6 +481,7 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
438
481
|
reasoningConfig: _,
|
|
439
482
|
additionalModelRequestFields: __,
|
|
440
483
|
serviceTier: ___,
|
|
484
|
+
structuredOutputMode: ____,
|
|
441
485
|
...filteredBedrockOptions
|
|
442
486
|
} = providerOptions?.bedrock || {};
|
|
443
487
|
|
|
@@ -572,7 +616,7 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
572
616
|
// tool calls
|
|
573
617
|
if (part.toolUse) {
|
|
574
618
|
const isJsonResponseTool =
|
|
575
|
-
usesJsonResponseTool && part.toolUse.name
|
|
619
|
+
usesJsonResponseTool && isJsonResponseToolName(part.toolUse.name);
|
|
576
620
|
|
|
577
621
|
if (isJsonResponseTool) {
|
|
578
622
|
isJsonResponseFromTool = true;
|
|
@@ -1030,7 +1074,7 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
1030
1074
|
const toolUse = contentBlockStart.start.toolUse;
|
|
1031
1075
|
const blockIndex = contentBlockStart.contentBlockIndex!;
|
|
1032
1076
|
const isJsonResponseTool =
|
|
1033
|
-
usesJsonResponseTool && toolUse.name
|
|
1077
|
+
usesJsonResponseTool && isJsonResponseToolName(toolUse.name);
|
|
1034
1078
|
|
|
1035
1079
|
const normalizedToolCallId = normalizeToolCallId(
|
|
1036
1080
|
toolUse.toolUseId!,
|
|
@@ -107,6 +107,14 @@ export type BedrockFilePartProviderOptions = z.infer<
|
|
|
107
107
|
>;
|
|
108
108
|
|
|
109
109
|
export const amazonBedrockLanguageModelOptions = z.object({
|
|
110
|
+
/**
|
|
111
|
+
* Determines how structured outputs are generated for Anthropic models.
|
|
112
|
+
*
|
|
113
|
+
* - `outputFormat`: Use the native `output_config.format` parameter.
|
|
114
|
+
* - `jsonTool`: Use a special 'json' tool to specify the structured output format.
|
|
115
|
+
* - `auto`: Use `outputFormat` when supported, otherwise use `jsonTool` (default).
|
|
116
|
+
*/
|
|
117
|
+
structuredOutputMode: z.enum(['outputFormat', 'jsonTool', 'auto']).optional(),
|
|
110
118
|
/**
|
|
111
119
|
* Additional inference parameters that the model supports,
|
|
112
120
|
* beyond the base set of inference parameters that Converse
|