@ai-sdk/amazon-bedrock
Version:
3,478 lines • 128 kB
JavaScript
import { anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema, supportsNativeStructuredOutputSchema } from "@ai-sdk/anthropic/internal";
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, asSchema, combineHeaders, convertToBase64, convertUint8ArrayToBase64, createJsonErrorResponseHandler, createJsonResponseHandler, createNullLanguageModelUsage, createProviderStreamError, extractResponseHeaders, generateId, getRuntimeEnvironmentUserAgent, getTopLevelMediaType, injectJsonInstructionIntoMessages, isCustomReasoning, isFullMediaType, isValidHostnamePart, lazySchema, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, normalizeHeaders, parseProviderOptions, postJsonToApi, resolve, resolveFullMediaType, resolveProviderOptions, safeParseJSON, safeValidateTypes, secureJsonParse, serializeModelOptions, stringifyWithBigIntAndCycles, stripFileExtension, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
import { z } from "zod/v4";
import { EmptyResponseBodyError, InvalidArgumentError, LoadAPIKeyError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
import { EventStreamCodec } from "@smithy/eventstream-codec";
import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
import { AwsV4Signer } from "aws4fetch";
//#region src/amazon-bedrock-model-catalog.ts
const strictToolsAndNativeOutput = {
tools: { strict: true },
structuredOutput: { native: true }
};
const strictToolsOnly = {
tools: { strict: true },
structuredOutput: { native: false }
};
const noStrictToolsOrNativeOutput = {
tools: { strict: false },
structuredOutput: { native: false }
};
const claudeStructuredOutputCapabilities = {
"claude-opus-4": strictToolsAndNativeOutput,
"claude-sonnet-4": strictToolsAndNativeOutput,
"claude-opus-4-1": strictToolsAndNativeOutput,
"claude-opus-4-5": strictToolsAndNativeOutput,
"claude-sonnet-4-5": strictToolsAndNativeOutput,
"claude-haiku-4-5": strictToolsOnly,
"claude-opus-4-6": strictToolsAndNativeOutput,
"claude-sonnet-4-6": strictToolsOnly,
"claude-opus-4-7": noStrictToolsOrNativeOutput,
"claude-opus-4-8": noStrictToolsOrNativeOutput,
"claude-opus-5": strictToolsAndNativeOutput,
"claude-sonnet-5": strictToolsAndNativeOutput,
"claude-opus-5-5": strictToolsAndNativeOutput,
"claude-sonnet-5-5": strictToolsAndNativeOutput,
"claude-haiku-5-5": strictToolsAndNativeOutput,
"claude-fable-5": noStrictToolsOrNativeOutput,
"claude-fable-5-1": noStrictToolsOrNativeOutput,
"claude-fable-5-2": noStrictToolsOrNativeOutput
};
function resolveClaudeStructuredOutputCapabilities(modelId) {
if (!modelId.includes("claude-") || /claude-(?:instant|v?2|3)(?=[-.:]|$)/.test(modelId)) return strictToolsAndNativeOutput;
const canonicalModelId = /claude-[a-z]+-4-\d{1,2}(?=[-.:]|$)/.exec(modelId)?.[0] ?? /(claude-[a-z]+-\d+(?:-\d{1,2})?)(?:-\d{8})?(?:-v\d+)?(?=[:.]|$)/.exec(modelId)?.[1];
return canonicalModelId != null && Object.prototype.hasOwnProperty.call(claudeStructuredOutputCapabilities, canonicalModelId) ? claudeStructuredOutputCapabilities[canonicalModelId] : noStrictToolsOrNativeOutput;
}
const documentedLanguageModels = {
"amazon.titan-tg1-large": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"amazon.titan-text-express-v1": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"amazon.titan-text-lite-v1": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"us.amazon.nova-premier-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.amazon.nova-pro-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.amazon.nova-lite-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.amazon.nova-micro-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-haiku-4-5-20251001-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-sonnet-4-20250514-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-sonnet-4-5-20250929-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-opus-4-20250514-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-opus-4-1-20250805-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-3-5-sonnet-20241022-v2:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-3-5-sonnet-20240620-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-3-opus-20240229-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-3-sonnet-20240229-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-3-haiku-20240307-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-sonnet-4-20250514-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-sonnet-4-5-20250929-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-opus-4-20250514-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-opus-4-1-20250805-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-3-5-sonnet-20241022-v2:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-3-5-sonnet-20240620-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-3-sonnet-20240229-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-3-opus-20240229-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"us.anthropic.claude-3-haiku-20240307-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"anthropic.claude-v2": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"anthropic.claude-v2:1": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"anthropic.claude-instant-v1": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"cohere.command-text-v14": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"cohere.command-light-text-v14": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"cohere.command-r-v1:0": {
image: false,
objectGeneration: false,
tools: true,
streaming: false
},
"cohere.command-r-plus-v1:0": {
image: false,
objectGeneration: false,
tools: true,
streaming: false
},
"us.deepseek.r1-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"meta.llama3-8b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-70b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-1-8b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-1-70b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-1-405b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-2-1b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-2-3b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-2-11b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"meta.llama3-2-90b-instruct-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"us.meta.llama3-2-1b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama3-2-3b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama3-2-11b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama3-2-90b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama3-1-8b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama3-1-70b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama3-3-70b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama4-scout-17b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"us.meta.llama4-maverick-17b-instruct-v1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"mistral.mistral-7b-instruct-v0:2": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"mistral.mixtral-8x7b-instruct-v0:1": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"mistral.mistral-large-2402-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"mistral.mistral-small-2402-v1:0": {
image: false,
objectGeneration: false,
tools: false,
streaming: false
},
"us.mistral.pixtral-large-2502-v1:0": {
image: true,
objectGeneration: true,
tools: true,
streaming: true
},
"openai.gpt-oss-120b-1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
},
"openai.gpt-oss-20b-1:0": {
image: false,
objectGeneration: true,
tools: true,
streaming: true
}
};
function createLanguageEntry(modelId) {
const documented = documentedLanguageModels[modelId];
const anthropic = getModelCapabilities(modelId);
const claude = modelId.includes("claude-");
const structuredOutputCapabilities = resolveClaudeStructuredOutputCapabilities(modelId);
const openaiModelId = /^(?:[^.]+\.)?(openai\..+)$/.exec(modelId)?.[1];
const nova2Reasoning = modelId.includes("amazon.nova-2-lite-v1:0");
const sampling = anthropic.rejectsSamplingParameters ? false : true;
return {
capabilities: { language: {
inputs: {
image: documented?.image ?? "unknown",
audio: "unknown"
},
objectGeneration: documented?.objectGeneration ?? "unknown",
tools: {
usage: documented?.tools ?? "unknown",
streaming: documented?.streaming ?? "unknown",
strict: structuredOutputCapabilities.tools.strict
},
reasoning: { support: openaiModelId != null || nova2Reasoning ? true : "unknown" },
sampling: {
temperature: sampling,
topP: sampling,
logprobs: false
},
structuredOutput: structuredOutputCapabilities.structuredOutput
} },
providerOptions: { language: {
structuredOutputMode: { condition: "Applies to Anthropic models; auto preserves each model’s native-output policy." },
additionalModelRequestFields: true,
reasoningConfig: { condition: "Supported reasoning configuration depends on the underlying model." },
anthropicBeta: { condition: "Requires an Anthropic model and a supported beta." },
serviceTier: { condition: "Requires a model and region supporting the selected tier." },
requestMetadata: true
} },
requestConfig: { language: {
anthropic: modelId.includes("anthropic"),
applicationInferenceProfile: modelId.includes(":application-inference-profile/"),
claude,
mistral: modelId.includes("mistral."),
openai: openaiModelId != null,
openaiGptOss: openaiModelId?.startsWith("openai.gpt-oss-") ?? false,
openaiGptOssReasoning: modelId.includes("openai.gpt-oss-"),
nova2Reasoning,
mantleRoute: /^(?:openai\.gpt-(?!oss-)|google\.gemma-4|xai\.)/.test(modelId) ? "openai/v1" : "v1"
} }
};
}
function getAmazonBedrockLanguageModelEntry(modelId) {
const entry = Object.prototype.hasOwnProperty.call(amazonBedrockModelCatalog, modelId) ? amazonBedrockModelCatalog[modelId] : void 0;
return entry?.capabilities.language != null ? entry : createLanguageEntry(modelId);
}
function getAmazonBedrockEmbeddingModelFamily(modelId) {
return modelId.includes("amazon.nova-") && modelId.includes("embed") ? "nova" : modelId.includes("cohere.embed-") ? "cohere" : "titan";
}
function embeddingEntry(modelId, defaultDimensions, customDimensions) {
const family = getAmazonBedrockEmbeddingModelFamily(modelId);
return {
capabilities: { embedding: {
defaultDimensions,
customDimensions
} },
providerOptions: { embedding: {
dimensions: family === "titan" && customDimensions,
normalize: family === "titan" && customDimensions,
embeddingDimension: family === "nova",
embeddingPurpose: family === "nova",
inputType: family === "cohere",
truncate: family !== "titan",
outputDimension: family === "cohere" && customDimensions
} }
};
}
const amazonBedrockImageModelCatalog = { "amazon.nova-canvas-v1:0": {
capabilities: { image: {
sizes: { description: "320–4096 pixels per side, divisible by 16; aspect ratio 1:4 to 4:1; fewer than 4,194,304 pixels." },
fileInput: true,
maskInput: true,
maxImagesPerCall: 5
} },
providerOptions: { image: {
quality: true,
negativeText: true,
cfgScale: true,
style: true,
taskType: true,
maskPrompt: true,
outPaintingMode: true,
similarityStrength: true
} }
} };
function getAmazonBedrockImageModelEntry(modelId) {
return Object.prototype.hasOwnProperty.call(amazonBedrockImageModelCatalog, modelId) ? amazonBedrockImageModelCatalog[modelId] : { capabilities: { image: {
sizes: { description: "Model-dependent." },
fileInput: "unknown",
maskInput: "unknown",
maxImagesPerCall: void 0
} } };
}
const rerankingEntry = {
capabilities: { reranking: {
documents: {
text: true,
object: true
},
topN: true
} },
providerOptions: { reranking: {
nextToken: true,
additionalModelRequestFields: true
} }
};
const languageModelIds = [
"amazon.titan-tg1-large",
"amazon.titan-text-express-v1",
"anthropic.claude-v2",
"anthropic.claude-v2:1",
"anthropic.claude-instant-v1",
"anthropic.claude-sonnet-5",
"anthropic.claude-sonnet-5-5",
"anthropic.claude-haiku-5-5",
"anthropic.claude-fable-5",
"anthropic.claude-fable-5-1",
"anthropic.claude-fable-5-2",
"anthropic.claude-opus-5",
"anthropic.claude-opus-5-5",
"anthropic.claude-opus-4-8",
"anthropic.claude-opus-4-7",
"anthropic.claude-opus-4-6-v1",
"anthropic.claude-sonnet-4-6-v1",
"anthropic.claude-opus-4-5-20251101-v1:0",
"anthropic.claude-haiku-4-5-20251001-v1:0",
"anthropic.claude-sonnet-4-5-20250929-v1:0",
"anthropic.claude-sonnet-4-20250514-v1:0",
"anthropic.claude-opus-4-20250514-v1:0",
"anthropic.claude-opus-4-1-20250805-v1:0",
"anthropic.claude-3-7-sonnet-20250219-v1:0",
"anthropic.claude-3-5-sonnet-20240620-v1:0",
"anthropic.claude-3-5-sonnet-20241022-v2:0",
"anthropic.claude-3-5-haiku-20241022-v1:0",
"anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-haiku-20240307-v1:0",
"anthropic.claude-3-opus-20240229-v1:0",
"cohere.command-text-v14",
"cohere.command-light-text-v14",
"cohere.command-r-v1:0",
"cohere.command-r-plus-v1:0",
"meta.llama3-70b-instruct-v1:0",
"meta.llama3-8b-instruct-v1:0",
"meta.llama3-1-405b-instruct-v1:0",
"meta.llama3-1-70b-instruct-v1:0",
"meta.llama3-1-8b-instruct-v1:0",
"meta.llama3-2-11b-instruct-v1:0",
"meta.llama3-2-1b-instruct-v1:0",
"meta.llama3-2-3b-instruct-v1:0",
"meta.llama3-2-90b-instruct-v1:0",
"mistral.mistral-7b-instruct-v0:2",
"mistral.mixtral-8x7b-instruct-v0:1",
"mistral.mistral-large-2402-v1:0",
"mistral.mistral-small-2402-v1:0",
"openai.gpt-oss-120b-1:0",
"openai.gpt-oss-20b-1:0",
"amazon.titan-text-lite-v1",
"us.amazon.nova-premier-v1:0",
"us.amazon.nova-pro-v1:0",
"us.amazon.nova-micro-v1:0",
"us.amazon.nova-lite-v1:0",
"us.anthropic.claude-3-sonnet-20240229-v1:0",
"us.anthropic.claude-3-opus-20240229-v1:0",
"us.anthropic.claude-3-haiku-20240307-v1:0",
"us.anthropic.claude-3-5-sonnet-20240620-v1:0",
"us.anthropic.claude-3-5-haiku-20241022-v1:0",
"us.anthropic.claude-3-5-sonnet-20241022-v2:0",
"us.anthropic.claude-3-7-sonnet-20250219-v1:0",
"us.anthropic.claude-sonnet-5",
"us.anthropic.claude-sonnet-5-5",
"us.anthropic.claude-haiku-5-5",
"us.anthropic.claude-fable-5",
"us.anthropic.claude-fable-5-1",
"us.anthropic.claude-fable-5-2",
"us.anthropic.claude-opus-5",
"us.anthropic.claude-opus-5-5",
"us.anthropic.claude-opus-4-8",
"us.anthropic.claude-opus-4-7",
"us.anthropic.claude-opus-4-6-v1",
"us.anthropic.claude-sonnet-4-6-v1",
"us.anthropic.claude-opus-4-5-20251101-v1:0",
"us.anthropic.claude-sonnet-4-5-20250929-v1:0",
"us.anthropic.claude-sonnet-4-20250514-v1:0",
"us.anthropic.claude-opus-4-20250514-v1:0",
"us.anthropic.claude-opus-4-1-20250805-v1:0",
"us.anthropic.claude-haiku-4-5-20251001-v1:0",
"global.anthropic.claude-fable-5-1",
"global.anthropic.claude-fable-5-2",
"us.meta.llama3-2-11b-instruct-v1:0",
"us.meta.llama3-2-3b-instruct-v1:0",
"us.meta.llama3-2-90b-instruct-v1:0",
"us.meta.llama3-2-1b-instruct-v1:0",
"us.meta.llama3-1-8b-instruct-v1:0",
"us.meta.llama3-1-70b-instruct-v1:0",
"us.meta.llama3-3-70b-instruct-v1:0",
"us.deepseek.r1-v1:0",
"us.mistral.pixtral-large-2502-v1:0",
"us.meta.llama4-scout-17b-instruct-v1:0",
"us.meta.llama4-maverick-17b-instruct-v1:0",
"amazon.nova-2-lite-v1:0"
];
const amazonBedrockEmbeddingModelEntries = {
"amazon.titan-embed-text-v1": embeddingEntry("amazon.titan-embed-text-v1", 1536, false),
"amazon.titan-embed-text-v2:0": embeddingEntry("amazon.titan-embed-text-v2:0", 1024, true),
"amazon.nova-embed-text-v2:0": embeddingEntry("amazon.nova-embed-text-v2:0", 1024, true),
"cohere.embed-english-v3": embeddingEntry("cohere.embed-english-v3", 1024, false),
"cohere.embed-multilingual-v3": embeddingEntry("cohere.embed-multilingual-v3", 1024, false),
"cohere.embed-v4:0": embeddingEntry("cohere.embed-v4:0", 1536, true)
};
const amazonBedrockRerankingModelEntries = {
"amazon.rerank-v1:0": rerankingEntry,
"cohere.rerank-v3-5:0": rerankingEntry
};
const amazonBedrockModelCatalog = {
...Object.fromEntries(languageModelIds.map((id) => [id, createLanguageEntry(id)])),
...amazonBedrockEmbeddingModelEntries,
...amazonBedrockRerankingModelEntries,
...amazonBedrockImageModelCatalog
};
Object.fromEntries(Object.entries({
"us.anthropic.claude-fable-5-1": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-fable-5-2": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-sonnet-5-5": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-sonnet-5": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-haiku-5-5": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-fable-5": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-opus-4-8": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-opus-4-7": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-opus-4-6-v1": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-opus-4-5-20251101-v1:0": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-sonnet-4-5-20250929-v1:0": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-opus-4-20250514-v1:0": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-sonnet-4-20250514-v1:0": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-opus-4-1-20250805-v1:0": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-haiku-4-5-20251001-v1:0": {
computerUse: true,
reasoning: true
},
"us.anthropic.claude-3-5-sonnet-20241022-v2:0": {
computerUse: false,
reasoning: false
},
"anthropic.claude-3-5-haiku-20241022-v1:0": void 0,
"anthropic.claude-3-5-sonnet-20240620-v1:0": void 0,
"anthropic.claude-3-5-sonnet-20241022-v2:0": void 0,
"anthropic.claude-3-7-sonnet-20250219-v1:0": void 0,
"anthropic.claude-3-haiku-20240307-v1:0": void 0,
"anthropic.claude-3-opus-20240229-v1:0": void 0,
"anthropic.claude-3-sonnet-20240229-v1:0": void 0,
"anthropic.claude-fable-5": void 0,
"anthropic.claude-fable-5-1": void 0,
"anthropic.claude-fable-5-2": void 0,
"anthropic.claude-haiku-4-5-20251001-v1:0": void 0,
"anthropic.claude-haiku-5-5": void 0,
"anthropic.claude-opus-4-1-20250805-v1:0": void 0,
"anthropic.claude-opus-4-20250514-v1:0": void 0,
"anthropic.claude-opus-4-5-20251101-v1:0": void 0,
"anthropic.claude-opus-4-6-v1": void 0,
"anthropic.claude-opus-4-7": void 0,
"anthropic.claude-opus-4-8": void 0,
"anthropic.claude-opus-5": void 0,
"anthropic.claude-opus-5-5": void 0,
"anthropic.claude-sonnet-4-20250514-v1:0": void 0,
"anthropic.claude-sonnet-4-5-20250929-v1:0": void 0,
"anthropic.claude-sonnet-4-6-v1": void 0,
"anthropic.claude-sonnet-5": void 0,
"anthropic.claude-sonnet-5-5": void 0,
"global.anthropic.claude-fable-5-1": void 0,
"global.anthropic.claude-fable-5-2": void 0,
"us.anthropic.claude-3-5-haiku-20241022-v1:0": void 0,
"us.anthropic.claude-3-5-sonnet-20240620-v1:0": void 0,
"us.anthropic.claude-3-7-sonnet-20250219-v1:0": void 0,
"us.anthropic.claude-3-haiku-20240307-v1:0": void 0,
"us.anthropic.claude-3-opus-20240229-v1:0": void 0,
"us.anthropic.claude-3-sonnet-20240229-v1:0": void 0,
"us.anthropic.claude-opus-5": void 0,
"us.anthropic.claude-opus-5-5": void 0,
"us.anthropic.claude-sonnet-4-6-v1": void 0
}).flatMap(([id, support]) => {
if (support == null) return [];
const base = getAmazonBedrockLanguageModelEntry(id).capabilities.language;
return [[id, { capabilities: { language: {
...base,
inputs: {
image: true,
audio: false
},
objectGeneration: true,
tools: {
...base.tools,
usage: true,
streaming: true,
computerUse: support.computerUse
},
reasoning: { support: support.reasoning }
} } }]];
}));
Object.fromEntries(Object.entries({
"openai.gpt-oss-20b": {
chatCompletions: true,
responses: true
},
"openai.gpt-oss-120b": {
chatCompletions: true,
responses: true
},
"openai.gpt-oss-safeguard-20b": {
chatCompletions: true,
responses: false
},
"openai.gpt-oss-safeguard-120b": {
chatCompletions: true,
responses: false
}
}).map(([id, endpoints]) => {
return [id, { capabilities: { language: {
...getAmazonBedrockLanguageModelEntry(id).capabilities.language,
endpoints
} } }];
}));
//#endregion
//#region src/amazon-bedrock-api-types.ts
const BEDROCK_STOP_REASONS = [
"stop",
"stop_sequence",
"end_turn",
"length",
"max_tokens",
"content-filter",
"content_filtered",
"guardrail_intervened",
"tool-calls",
"tool_use"
];
/**
* @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ImageBlock.html
*/
const BEDROCK_IMAGE_MIME_TYPES = {
"image/jpeg": "jpeg",
"image/png": "png",
"image/gif": "gif",
"image/webp": "webp"
};
/**
* @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html
*/
const BEDROCK_DOCUMENT_MIME_TYPES = {
"application/pdf": "pdf",
"text/csv": "csv",
"application/msword": "doc",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
"application/vnd.ms-excel": "xls",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
"text/html": "html",
"text/plain": "txt",
"text/markdown": "md"
};
/**
* @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_VideoBlock.html
*/
const BEDROCK_VIDEO_MIME_TYPES = {
"video/x-matroska": "mkv",
"video/quicktime": "mov",
"video/mp4": "mp4",
"video/webm": "webm",
"video/x-flv": "flv",
"video/mpeg": "mpeg",
"video/mpg": "mpg",
"video/wmv": "wmv",
"video/x-ms-wmv": "wmv",
"video/3gpp": "three_gp"
};
//#endregion
//#region src/amazon-bedrock-chat-language-model-options.ts
/**
* Bedrock file part provider options for document-specific features.
* These options apply to individual file parts (documents).
*/
const amazonBedrockFilePartProviderOptions = lazySchema(() => zodSchema(z.object({
/**
* Citation configuration for this document.
* When enabled, this document will generate citations in the response.
*/
citations: z.object({
/**
* Enable citations for this document
*/
enabled: z.boolean() }).optional() })));
/**
* Amazon Bedrock text part provider options for guardrail content.
* These options apply to individual text parts.
*/
const amazonBedrockTextPartProviderOptions = lazySchema(() => zodSchema(z.object({
guardContent: z.boolean().optional(),
guardContentQualifiers: z.array(z.enum([
"grounding_source",
"query",
"guard_content"
])).optional()
})));
/**
* Amazon Bedrock image part provider options for guardrail content.
* These options apply to individual image parts.
*/
const amazonBedrockImagePartProviderOptions = lazySchema(() => zodSchema(z.object({ guardContent: z.boolean().optional() })));
const amazonBedrockLanguageModelChatOptions = z.object({
/**
* Determines how structured outputs are generated for Anthropic models.
*
* - `outputFormat`: Use the native `output_config.format` parameter.
* - `jsonTool`: Use a special 'json' tool to specify the structured output format.
* - `auto`: Use `outputFormat` when supported, otherwise use `jsonTool` (default).
*/
structuredOutputMode: z.enum([
"outputFormat",
"jsonTool",
"auto"
]).optional(),
/**
* Additional inference parameters that the model supports,
* beyond the base set of inference parameters that Converse
* supports in the inferenceConfig field
*/
additionalModelRequestFields: z.record(z.string(), z.any()).optional(),
reasoningConfig: z.object({
type: z.union([
z.literal("enabled"),
z.literal("disabled"),
z.literal("adaptive")
]).optional(),
budgetTokens: z.number().optional(),
maxReasoningEffort: z.enum([
"low",
"medium",
"high",
"xhigh",
"max"
]).optional(),
display: z.enum(["omitted", "summarized"]).optional()
}).optional(),
/**
* Anthropic beta features to enable
*/
anthropicBeta: z.array(z.string()).optional(),
/**
* Service tier for the request.
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
*
* - 'reserved': Uses provisioned throughput capacity
* - 'priority': Prioritizes low-latency inference when capacity is available
* - 'default': Standard on-demand tier
* - 'flex': Lower-cost tier for flexible latency workloads
*/
serviceTier: z.enum([
"reserved",
"priority",
"default",
"flex"
]).optional(),
/**
* Key-value pairs to attach to the request for invocation log filtering and
* cost attribution.
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/cost-mgmt-request-metadata.html
*
* - Maximum 16 entries per request.
* - Keys and values: maximum 256 characters each.
*/
requestMetadata: z.record(z.string(), z.string()).optional()
});
//#endregion
//#region src/amazon-bedrock-anthropic-model-support.ts
function isAnthropicModel({ modelId, modelFamily, reasoningBudgetTokens }) {
const config = getAmazonBedrockLanguageModelEntry(modelId).requestConfig.language;
return modelFamily === "anthropic" || config.anthropic || config.applicationInferenceProfile && reasoningBudgetTokens != null;
}
function supportsStrictTools(modelId) {
return getAmazonBedrockLanguageModelEntry(modelId).capabilities.language.tools.strict;
}
function supportsNativeStructuredOutput(modelId) {
return getAmazonBedrockLanguageModelEntry(modelId).capabilities.language.structuredOutput.native;
}
//#endregion
//#region src/amazon-bedrock-error.ts
const AmazonBedrockErrorSchema = lazySchema(() => zodSchema(z.object({
message: z.string(),
type: z.string().nullish()
})));
const amazonBedrockFailedResponseHandler = createJsonErrorResponseHandler({
errorSchema: AmazonBedrockErrorSchema,
errorToMessage: (error) => error.type == null ? error.message : `${error.type}: ${error.message}`
});
//#endregion
//#region src/amazon-bedrock-event-stream-decoder.ts
function createAmazonBedrockEventStreamDecoder(body, processEvent) {
const codec = new EventStreamCodec(toUtf8, fromUtf8);
let buffer = /* @__PURE__ */ new Uint8Array(0);
const textDecoder = new TextDecoder();
return body.pipeThrough(new TransformStream({
async transform(chunk, controller) {
const newBuffer = new Uint8Array(buffer.length + chunk.length);
newBuffer.set(buffer);
newBuffer.set(chunk, buffer.length);
buffer = newBuffer;
while (buffer.length >= 4) {
const totalLength = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength).getUint32(0, false);
if (buffer.length < totalLength) break;
const subView = buffer.subarray(0, totalLength);
const decoded = codec.decode(subView);
buffer = buffer.slice(totalLength);
const messageType = decoded.headers[":message-type"]?.value;
const eventType = decoded.headers[":event-type"]?.value;
const exceptionType = decoded.headers[":exception-type"]?.value;
await processEvent({
messageType,
eventType,
exceptionType,
data: textDecoder.decode(decoded.body)
}, controller);
}
},
flush() {
if (buffer.length > 0) throw new Error(`Incomplete Amazon Bedrock event-stream frame: ${buffer.length} buffered bytes remain at end of stream.`);
}
}));
}
//#endregion
//#region src/amazon-bedrock-event-stream-response-handler.ts
const createAmazonBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response }) => {
const responseHeaders = extractResponseHeaders(response);
if (response.body == null) throw new EmptyResponseBodyError({});
return {
responseHeaders,
value: createAmazonBedrockEventStreamDecoder(response.body, async (event, controller) => {
const payloadType = event.messageType === "event" ? event.eventType : event.messageType === "exception" ? event.exceptionType : void 0;
if (payloadType == null) return;
const parsedDataResult = await safeParseJSON({ text: event.data });
if (!parsedDataResult.success) {
controller.enqueue(parsedDataResult);
return;
}
delete parsedDataResult.value.p;
const wrappedData = { [payloadType]: parsedDataResult.value };
const validatedWrappedData = await safeValidateTypes({
value: wrappedData,
schema: chunkSchema
});
if (!validatedWrappedData.success) controller.enqueue(validatedWrappedData);
else controller.enqueue({
success: true,
value: validatedWrappedData.value,
rawValue: wrappedData
});
})
};
};
//#endregion
//#region src/amazon-bedrock-stream-error.ts
function getAmazonBedrockStreamErrorMetadata(type) {
switch (type) {
case "internalServerException":
case "InternalServerException": return {
statusCode: 500,
isRetryable: true
};
case "modelStreamErrorException":
case "ModelStreamErrorException": return {
statusCode: 424,
isRetryable: true
};
case "serviceUnavailableException":
case "ServiceUnavailableException": return {
statusCode: 503,
isRetryable: true
};
case "throttlingException":
case "ThrottlingException": return {
statusCode: 429,
isRetryable: true
};
case "validationException":
case "ValidationException": return {
statusCode: 400,
isRetryable: false
};
default: return {};
}
}
//#endregion
//#region src/amazon-bedrock-prepare-tools.ts
async function prepareTools$1({ tools, toolChoice, modelId, modelFamily, reasoningBudgetTokens, disableParallelToolUse, rejectsForcedToolUse = false }) {
const toolWarnings = [];
const betas = /* @__PURE__ */ new Set();
if (tools == null || tools.length === 0) return {
toolConfig: {},
additionalTools: void 0,
betas,
toolWarnings
};
const supportedTools = tools.filter((tool) => {
if (tool.type === "provider" && (tool.id === "anthropic.web_search_20250305" || tool.id === "anthropic.web_search_20260318" || tool.id === "anthropic.web_fetch_20260318")) {
toolWarnings.push({
type: "unsupported",
feature: `${tool.id.slice(10)} tool`,
details: `The ${tool.id.slice(10)} tool is not supported on Amazon Bedrock.`
});
return false;
}
return true;
});
if (supportedTools.length === 0) return {
toolConfig: {},
additionalTools: void 0,
betas,
toolWarnings
};
const isAnthropicModel$2 = isAnthropicModel({
modelId,
modelFamily,
reasoningBudgetTokens
});
const providerTools = supportedTools.filter((t) => t.type === "provider");
const functionTools = supportedTools.filter((t) => t.type === "function");
const rejectsForcedToolChoice = isAnthropicModel$2 && rejectsForcedToolUse && (toolChoice?.type === "required" || toolChoice?.type === "tool");
const providerToolsForRequest = rejectsForcedToolChoice && toolChoice.type === "tool" ? providerTools.filter((tool) => tool.name === toolChoice.toolName) : providerTools;
const disableParallelToolUseForRequest = disableParallelToolUse ?? (isAnthropicModel$2 && toolChoice?.type === "tool" && !rejectsForcedToolChoice ? true : void 0);
let additionalTools = void 0;
const amazonBedrockTools = [];
const usingAnthropicTools = isAnthropicModel$2 && providerToolsForRequest.length > 0;
if (usingAnthropicTools) {
const { toolChoice: preparedAnthropicToolChoice, toolWarnings: anthropicToolWarnings, betas: anthropicBetas } = await prepareTools({
tools: providerTools,
toolChoice,
disableParallelToolUse: disableParallelToolUseForRequest,
supportsStructuredOutput: false,
supportsStrictTools: false,
rejectsForcedToolUse
});
toolWarnings.push(...anthropicToolWarnings);
anthropicBetas.forEach((beta) => betas.add(beta));
if (preparedAnthropicToolChoice) additionalTools = { tool_choice: preparedAnthropicToolChoice };
for (const tool of providerToolsForRequest) {
const toolFactory = Object.values(anthropicTools).find((factory) => {
return factory({}).id === tool.id;
});
if (toolFactory != null) {
const fullToolDefinition = toolFactory({});
amazonBedrockTools.push({ toolSpec: {
name: tool.name,
inputSchema: { json: await asSchema(fullToolDefinition.inputSchema).jsonSchema }
} });
} else toolWarnings.push({
type: "unsupported",
feature: `tool ${tool.id}`
});
}
} else for (const tool of providerToolsForRequest) toolWarnings.push({
type: "unsupported",
feature: `tool ${tool.id}`
});
let preparedToolChoice = toolChoice;
if (!usingAnthropicTools && rejectsForcedToolChoice) {
if (toolChoice.type === "tool") toolWarnings.push({
type: "unsupported",
feature: "toolChoice",
details: `toolChoice 'tool' is not supported by this model because it rejects forced tool use. Only the '${toolChoice.toolName}' tool is sent with 'auto' tool choice. Instruct the model to use the tool in the prompt and verify that a tool call was made.`
});
else toolWarnings.push({
type: "unsupported",
feature: "toolChoice",
details: "toolChoice 'required' is not supported by this model because it rejects forced tool use. Using 'auto' instead. Instruct the model to use a tool in the prompt and verify that a tool call was made."
});
preparedToolChoice = { type: "auto" };
}
const filteredFunctionTools = toolChoice?.type === "tool" && (!isAnthropicModel$2 || rejectsForcedToolChoice) ? functionTools.filter((t) => t.name === toolChoice.toolName) : functionTools;
const supportsStrictOnTools = supportsStrictTools(modelId);
for (const tool of filteredFunctionTools) {
const supportsStrictForTool = supportsStrictOnTools && (tool.strict !== true || isStrictToolSchemaCompatible(tool.inputSchema));
if (!supportsStrictOnTools && tool.strict === true) toolWarnings.push({
type: "unsupported",
feature: "strict",
details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this model on Amazon Bedrock. The strict property will be ignored.`
});
else if (tool.strict === true && !supportsStrictForTool) toolWarnings.push({
type: "unsupported",
feature: "strict",
details: `Tool '${tool.name}' has strict: true, but Amazon Bedrock requires every object in a strict tool schema to set additionalProperties: false. The strict property will be ignored.`
});
amazonBedrockTools.push({ toolSpec: {
name: tool.name,
...tool.description?.trim() !== "" ? { description: tool.description } : {},
...tool.strict != null && supportsStrictForTool ? { strict: tool.strict } : {},
inputSchema: { json: tool.inputSchema }
} });
}
if (isAnthropicModel$2 && !usingAnthropicTools && disableParallelToolUseForRequest && amazonBedrockTools.length > 0 && preparedToolChoice?.type !== "none") additionalTools = { tool_choice: preparedToolChoice?.type === "required" ? {
type: "any",
disable_parallel_tool_use: true
} : preparedToolChoice?.type === "tool" ? {
type: "tool",
name: preparedToolChoice.toolName,
disable_parallel_tool_use: true
} : {
type: "auto",
disable_parallel_tool_use: true
} };
let amazonBedrockToolChoice = void 0;
if (!usingAnthropicTools && additionalTools?.tool_choice == null && amazonBedrockTools.length > 0 && preparedToolChoice) {
const type = preparedToolChoice.type;
switch (type) {
case "auto":
amazonBedrockToolChoice = { auto: {} };
break;
case "required":
amazonBedrockToolChoice = { any: {} };
break;
case "none":
amazonBedrockTools.length = 0;
amazonBedrockToolChoice = void 0;
break;
case "tool":
amazonBedrockToolChoice = { tool: { name: preparedToolChoice.toolName } };
break;
default: throw new UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
}
}
return {
toolConfig: amazonBedrockTools.length > 0 ? {
tools: amazonBedrockTools,
toolChoice: amazonBedrockToolChoice
} : {},
additionalTools,
betas,
toolWarnings
};
}
function isStrictToolSchemaCompatible(schema) {
if (typeof schema === "boolean") return true;
const schemaWithDefs = schema;
if ((schema.type === "object" || Array.isArray(schema.type) && schema.type.includes("object")) && schema.additionalProperties !== false) return false;
const schemaMaps = [
schema.properties,
schema.patternProperties,
schema.definitions,
schemaWithDefs.$defs
];
for (const schemaMap of schemaMaps) if (schemaMap != null && Object.values(schemaMap).some((definition) => !isStrictToolSchemaCompatible(definition))) return false;
if (schema.dependencies != null && Object.values(schema.dependencies).some((dependency) => !Array.isArray(dependency) && !isStrictToolSchemaCompatible(dependency))) return false;
if ([
schema.propertyNames,
schema.contains,
schema.not,
schema.if,
schema.then,
schema.else
].some((nestedSchema) => nestedSchema != null && !isStrictToolSchemaCompatible(nestedSchema))) return false;
if (schema.items != null && (Array.isArray(schema.items) ? schema.items.some((item) => !isStrictToolSchemaCompatible(item)) : !isStrictToolSchemaCompatible(schema.items))) return false;
return [
schema.anyOf,
schema.allOf,
schema.oneOf
].every((alternatives) => alternatives == null || alternatives.every(isStrictToolSchemaCompatible));
}
//#endregion
//#region src/convert-amazon-bedrock-usage.ts
function convertAmazonBedrockUsage(usage) {
if (usage == null) return createNullLanguageModelUsage();
const inputTokens = usage.inputTokens;
const outputTokens = usage.outputTokens;
const cacheReadTokens = usage.cacheReadInputTokens ?? 0;
const cacheWriteTokens = usage.cacheWriteInputTokens ?? 0;
return {
inputTokens: {
total: inputTokens + cacheReadTokens + cacheWriteTokens,
noCache: inputTokens,
cacheRead: cacheReadTokens,
cacheWrite: cacheWriteTokens
},
outputTokens: {
total: outputTokens,
text: outputTokens,
reasoning: void 0
},
raw: usage
};
}
//#endregion
//#region src/amazon-bedrock-reasoning-metadata.ts
const amazonBedrockReasoningMetadataSchema = lazySchema(() => zodSchema(z.object({
signature: z.string().optional(),
redactedData: z.string().optional(),
redactedContent: z.string().optional()
})));
//#endregion
//#region src/normalize-tool-call-id.ts
/**
* Checks if the given model ID is a Mistral model.
* Mistral models on Bedrock are prefixed with 'mistral.' or region-prefixed like 'us.mistral.'.
*/
function isMistralModel(modelId) {
return getAmazonBedrockLanguageModelEntry(modelId).requestConfig.language.mistral;
}
/**
* Normalizes a tool call ID for Mistral models.
*
* Mistral models require tool call IDs to match the regex `^[a-zA-Z0-9]{9}$`:
* - Exactly 9 characters
* - Alphanumeric only (no underscores, hyphens, or other characters)
*
* Bedrock generates tool call IDs in formats like `tooluse_bpe71yCfRu2b5i-nKGDr5g`,
* which are incompatible with Mistral's requirements.
*
* This function hashes incompatible IDs into 9 alphanumeric characters.
*
* @param toolCallId - The original tool call ID from Bedrock
* @param isMistral - Whether the model is a Mistral model
* @returns The normalized tool call ID (9 alphanumeric chars) if Mistral, otherwise the original ID
*/
function normalizeToolCallId(toolCallId, isMistral) {
if (!isMistral) return toolCallId;
if (/^[a-zA-Z0-9]{9}$/.test(toolCallId)) return toolCallId;
return convertToBase62Hash(toolCallId);
}
const base62Characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const base62Length = BigInt(62);
const normalizedToolCallIdLength = 9;
const normalizedToolCallIdSpace = base62Length ** BigInt(normalizedToolCallIdLength);
const fnvOffsetBasis64 = BigInt("14695981039346656037");
const fnvPrime64 = BigInt("1099511628211");
const fnv64BitMask = BigInt("18446744073709551615");
function convertToBase62Hash(value) {
let hash = fnvOffsetBasis64;
for (let i = 0; i < value.length; i++) {
hash ^= BigInt(value.charCodeAt(i));
hash = hash * fnvPrime64 & fnv64BitMask;
}
let base62Value = hash % normalizedToolCallIdSpace;
let result = "";
for (let i = 0; i < normalizedToolCallIdLength; i++) {
result = base62Characters[Number(base62Value % base62Length)] + result;
base62Value /= base62Length;
}
return result;
}
//#endregion
//#region src/convert-to-amazon-bedrock-chat-messages.ts
function getCachePoint(providerMetadata) {
const cachePointConfig = providerMetadata?.amazonBedrock?.cachePoint ?? providerMetadata?.bedrock?.cachePoint;
if (!cachePointConfig) return;
return { cachePoint: cachePointConfig };
}
function pushCachePoint(content, providerMetadata) {
const cachePoint = getCachePoint(providerMetadata);
if (cachePoint) content.push(cachePoint);
}
function sanitizeToolName(toolName) {
return toolName.replace(/[^a-zA-Z0-9_-]/g, "") || "_";
}
function sanitizeDocumentName(filename) {
return stripFileExtension(filename).replace(/\s+/g, " ").replace(/[^a-zA-Z0-9 ()[\]-]/g, "").trim().slice(0, 200).trim();
}
function getAmazonBedrockMediaSource({ data, functionality }) {
switch (data.type) {
case "data": return { bytes: convertToBase64(data.data) };
case "url":
if (data.url.protocol !== "s3:") throw new UnsupportedFunctionalityError({ functionality });
return { s3Location: { uri: data.url.toString() } };
}
}
async function shouldEnableCitations(providerMetadata) {
return (await parseProviderOptions({
provider: "amazonBedrock",
providerOptions: providerMetadata,
schema: amazonBedrockFilePartProviderOptions
}) ?? await parseProviderOptions({
provider: "bedrock",
providerOptions: providerMetadata,
schema: amazonBedrockFilePartProviderOptions
}))?.citations?.enabled ?? false;
}
async function getTextPartGuardContentOptions(providerMetadata) {
return await parseProviderOptions({
provider: "amazonBedrock",
providerOptions: providerMetadata,
schema: amazonBedrockTextPartProviderOptions
}) ?? await parseProviderOptions({
provider: "bedrock",
providerOptions: providerMetadata,
schema: amazonBedrockTextPartProviderOptions
});
}
async function getImagePartGuardContentOptions(providerMetadata) {
return await parseProviderOptions({
provider: "amazonBedrock",
providerOptions: providerMetadata,
schema: amazonBedrockImagePartProviderOptions
}) ?? await parseProviderOptions({
provider: "bedrock",
providerOptions: providerMetadata,
schema: amazonBedrockImagePartProviderOptions
});
}
async function convertToAmazonBedrockChatMessages(prompt, isMistral = false) {
const blocks = groupIntoBlocks(prompt);
let system = [];
const messages = [];
let documentCounter = 0;
const generateDocumentName = () => `document-${++documentCounter}`;
const usedDocumentNames = /* @__PURE__ */ new Set();
const getDocumentName = (filename) => {
const baseName = filename && sanitizeDocumentName(filename) || generateDocumentName();
let name = baseName;
for (let suffix = 2; usedDocumentNames.has(name); suffix++) {
const suffixText = ` (${suffix})`;
name = `${baseName.slice(0, 200 - suffixText.length).trim()}${suffixText}`;
}
usedDocumentNames.add(name);
return name;
};
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
const isLastBlock = i === blocks.length - 1;
const type = block.type;
switch (type) {
case "system":
if (messages.length > 0) throw new UnsupportedFunctionalityError({ functionality: "Multiple system messages that are separated by user/assistant messages" });
for (const message of block.messages) {
system.push({ text: message.content });
const cachePoint = getCachePoint(message.providerOptions);
if (cachePoint) system.push(cachePoint);
}
break;
case "user": {
const amazonBedrockContent = [];
for (const message of block.messages) {
const { role, content, providerOptions } = message;
switch (role) {
case "user":
for (let j = 0; j < content.length; j++) {
const part = content[j];
switch (part.type) {
case "text": {
const textOptions = await getTextPartGuardContentOptions(part.providerOptions);
if (textOptions?.guardContent) amazonBedrockContent.push({ guardContent: { text: {
text: part.text,
qualifiers: textOptions.guardContentQualifiers
} } });
else amazonBedrockContent.push({ text: part.text });
break;
}
case "file": switch (part.data.type) {
case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
case "url": {
if (part.data.url.protocol !== "s3:") throw new UnsupportedFunctionalityError({ functionality: "File URL data" });
const fullMediaType = resolveFullMediaType({ part });
switch (getTopLevelMediaType(fullMediaType)) {
case "image":
amazonBedrockContent.push({ image: {
format: getAmazonBedrockImageFormat(fullMediaType),
source: getAmazonBedrockMediaSource({
data: part.data,
functionality: "File URL data"
})
} });
break;
case "video":
amazonBedrockContent.push({ video: {
format: getAmazonBedrockVideoFormat(fullMediaType),
source: getAmazonBedrockMediaSource({
data: part.data,
functionality: "File URL data"
})
} });
break;
default: throw new UnsupportedFunctionalityError({ functionality: "File URL data" });
}
break;
}
case "text": {
const textMediaType = isFullMediaType(part.mediaType) ? part.mediaType : "text/plain";
const enableCitations = await shouldEnableCitations(part.providerOptions);
amazonBedrockContent.push({ document: {
format: getAmazonBedrockDocumentFormat(textMediaType),
name: getDocumentName(part.filename),
source: { bytes: convertToBase64(new TextEncoder().encode(part.data.text)) },
...enableCitations && { citations: { enabled: true } }
} });
break;
}
case "data": {
const fullMediaType = resolveFullMediaType({ part });
switch (getTopLevelMediaType(fullMediaType)) {
case "image": {
const imageOptions = await getImagePartGuardContentOptions(part.providerOptions);
const imageBlock = { image: {
format: getAmazonBedrockImageFormat(fullMediaType),
source: getAmazonBedrockMediaSource({
data: part.data,
functionality: "File URL data"
})
} };
if (imageOptions?.guardContent) amazonBedrockContent.push({ guardContent: imageBlock });
else amazonBedrockContent.push(imageBlock);
break;
}
case "video":
amazonBedrockContent.push({ video: {
format: getAmazonBedrockVideoFormat(fullMediaType),
source: getAmazonBedrockMediaSource({
data: part.data,
functionality: "File URL data"
})
} });
break;
default: {
const enableCitations = await shouldEnableCitations(part.providerOptions);
amazonBedrockContent.push({ document: {
format: getAmazonBedrockDocumentFormat(fullMediaType),
name: getDocumentName(part.filename),
source: { bytes: convertToBase64(part.data.data) },
...enableCitations && { citations: { enabled: true } }
} });
}
}
break;
}
}
}
pushCachePoint(amazonBedrockContent, part.providerOptions);
}
break;
case "tool":
for (const part of content) {
if (part.type === "tool-approval-response") continue;
amazonBedrockContent.push({ toolResult: {
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
content: await convertToolResultOutput({
output: part.output,
getDocumentName
})
} });
pushCachePoint(amazonBedrockContent, part.providerOptions);
}
break;
default: throw new Error(`Unsupported role: ${role}`);
}
pushCachePoint(amazonBedrockContent, providerOptions);
}
appendToUserMessage(messages, amazonBedrockContent);
break;
}
case "assistant": {
let assistantContent = [];
let toolResultContent = [];
const flushAssistantContent = () => {
if (assistantContent.some((block) => !("cachePoint" in block))) messages.push({
role: "assistant",
content: assistantContent
});
assistantContent = [];
};
const flushToolResultContent = () => {
if (toolResultContent.length > 0) {
appendToUserMessage(messages, toolResultContent);
toolResultContent = [];
}
};
for (let j = 0; j < block.messages.length; j++) {
const message = block.messages[j];
const isLastMessage = j === block.messages.length - 1;
const content = message.content.filter((part) => part.type !== "text" || part.text.length > 0 || part.providerOptions == null || part.providerOptions.amazonBedrock != null || part.providerOptions.bedrock != null);
const hasReasoningBlocks = content.some((part) => part.type === "reasoning");
for (let k = 0; k < content.length; k++) {
const part = content[k];
const isLastContentPart = k === content.length - 1;
if (part.type !== "tool-result") flushToolResultContent();
switch (part.type) {
case "text":
if (!part.text.trim() && !hasReasoningBlocks) break;
assistantContent.push({ text: trimIfLast(isLastBlock, isLastMessage, isLastContentPart, part.text) });
break;
case "reasoning": {
const reasoningMetadata = await parseProviderOptions({
provider: "amazonBedrock",
providerOptions: part.providerOptions,
schema: amazonBedrockReasoningMetadataSchema
}) ?? await parseProviderOptions({
provider: "bedrock",
providerOptions: part.providerOptions,
schema: amazonBedrockReasoningMetadataSchema
});
if (reasoningMetadata?.signature != null) assistantContent.push({ reasoningContent: { reasoningText: {
text: part.text,
signature: reasoningMetadata.signature
} } });
else if (reasoningMetadata?.redactedContent != null) assistantContent.push({ reasoningContent: { redactedContent: reasoningMetadata.redactedContent } });
else if (reasoningMetadata?.redactedData != null) assistantContent.push({ reasoningContent: { redactedReasoning: { data: reasoningMetadata.redactedData } } });
break;
}
case "tool-call":
assistantContent.push({ toolUse: {
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
name: sanitizeToolName(part.toolName),
input: toBedrockToolInput(part.input)
} });
break;
case "tool-result":
flushAssistantContent();
toolResultContent.push({ toolResult: {
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
content: await convertToolResultOutput({
output: part.output,
getDocumentName
})
} });
}
pushCachePoint(part.type === "tool-result" ? toolResultContent : assistantContent, part.providerOptions);
}
pushCachePoint(content[content.length - 1]?.type === "tool-result" ? toolResultContent : assistantContent, message.providerOptions);
}
flushToolResultContent();
flushAssistantContent();
break;
}
default: throw new Error(`Unsupported type: ${type}`);
}
}
return {
system,
messages
};
}
function appendToUserMessage(messages, content) {
const lastMessage = messages[messages.length - 1];
if (lastMessage?.role === "user" && lastMessage.content.some((block) => "toolResult" in block)) lastMessage.content.push(...content);
else messages.push({
role: "user",
content
});
}
async function convertToolResultOutput({ output, getDocumentName }) {
switch (output.type) {
case "content": return Promise.all(output.value.map(async (contentPart) => {
switch (contentPart.type) {
case "text": return { text: contentPart.text };
case "file": {
if (contentPart.data.type !== "data" && (contentPart.data.type !== "url" || contentPart.data.url.protocol !== "s3:")) throw new UnsupportedFunctionalityError({ functionality: `tool result file data of type "${contentPart.data.type}"` });
const fullMediaType = resolveFullMediaType({ part: contentPart });
switch (getTopLevelMediaType(fullMediaType)) {
case "image": return { image: {
format: getAmazonBedrockImageFormat(fullMediaType),
source: getAmazonBedrockMediaSource({
data: contentPart.data,
functionality: `tool result file data of type "${contentPart.data.type}"`
})
} };
case "video": return { video: {
format: getAmazonBedrockVideoFormat(fullMediaType),
source: getAmazonBedrockMediaSource({
data: contentPart.data,
functionality: `tool result file data of type "${contentPart.data.type}"`
})
} };
default: {
if (contentPart.data.type !== "data") throw new UnsupportedFunctionalityError({ functionality: `tool result file data of type "${contentPart.data.type}"` });
const enableCitations = await shouldEnableCitations(contentPart.providerOptions);
return { document: {
format: getAmazonBedrockDocumentFormat(fullMediaType),
name: getDocumentName(contentPart.filename),
source: { bytes: convertToBase64(contentPart.data.data) },
...enableCitations && { citations: { enabled: true } }
} };
}
}
}
default: throw new UnsupportedFunctionalityError({ functionality: `unsupported tool content part type: ${contentPart.type}` });
}
}));
case "text":
case "error-text": return [{ text: output.value }];
case "execution-denied": return [{ text: output.reason ?? "Tool call execution denied." }];
default: return [{ text: stringifyWithBigIntAndCycles(output.value) }];
}
}
function toBedrockToolInput(input) {
if (typeof input === "string") try {
const parsed = secureJsonParse(input);
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) return parsed;
} catch {}
return typeof input === "object" && input !== null && !Array.isArray(input) ? input : { rawInvalidInput: input };
}
function getAmazonBedrockImageFormat(mimeType) {
const format = BEDROCK_IMAGE_MIME_TYPES[mimeType];
if (!format) throw new UnsupportedFunctionalityError({
functionality: `image mime type: ${mimeType}`,
message: `Unsupported image mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_IMAGE_MIME_TYPES).join(", ")}`
});
return format;
}
function getAmazonBedrockDocumentFormat(mimeType) {
const format = BEDROCK_DOCUMENT_MIME_TYPES[mimeType];
if (!format) throw new UnsupportedFunctionalityError({
functionality: `file mime type: ${mimeType}`,
message: `Unsupported file mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_DOCUMENT_MIME_TYPES).join(", ")}`
});
return format;
}
function getAmazonBedrockVideoFormat(mimeType) {
const format = BEDROCK_VIDEO_MIME_TYPES[mimeType];
if (!format) throw new UnsupportedFunctionalityError({
functionality: `video mime type: ${mimeType}`,
message: `Unsupported video mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_VIDEO_MIME_TYPES).join(", ")}`
});
return format;
}
function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
}
function groupIntoBlocks(prompt) {
const blocks = [];
let currentBlock = void 0;
for (const message of prompt) {
const { role } = message;
switch (role) {
case "system":
if (currentBlock?.type !== "system") {
currentBlock = {
type: "system",
messages: []
};
blocks.push(currentBlock);
}
currentBlock.messages.push(message);
break;
case "assistant":
if (currentBlock?.type !== "assistant") {
currentBlock = {
type: "assistant",
messages: []
};
blocks.push(currentBlock);
}
currentBlock.messages.push(message);
break;
case "user":
if (currentBlock?.type !== "user") {
currentBlock = {
type: "user",
messages: []
};
blocks.push(currentBlock);
}
currentBlock.messages.push(message);
break;
case "tool":
if (currentBlock?.type !== "user") {
currentBlock = {
type: "user",
messages: []
};
blocks.push(currentBlock);
}
currentBlock.messages.push(message);
break;
default: throw new Error(`Unsupported role: ${role}`);
}
}
return blocks;
}
//#endregion
//#region src/map-amazon-bedrock-finish-reason.ts
function mapAmazonBedrockFinishReason(finishReason, isJsonResponseFromTool) {
switch (finishReason) {
case "stop_sequence":
case "end_turn": return "stop";
case "max_tokens": return "length";
case "content_filtered":
case "guardrail_intervened": return "content-filter";
case "tool_use": return isJsonResponseFromTool ? "stop" : "tool-calls";
default: return "other";
}
}
//#endregion
//#region src/amazon-bedrock-chat-language-model.ts
const anthropicProviderOptions = lazySchema(() => zodSchema(z.object({
disableParallelToolUse: z.boolean().optional(),
structuredOutputMode: z.enum([
"outputFormat",
"jsonTool",
"auto"
]).optional()
})));
function createAmazonBedrockStreamError({ type, error, data }) {
const message = typeof error.message === "string" ? error.message : `Amazon Bedrock stream failed with ${type}`;
return createProviderStreamError({
message,
type,
...getAmazonBedrockStreamErrorMetadata(type),
data
});
}
var AmazonBedrockChatLanguageModel = class AmazonBedrockChatLanguageModel {
static [WORKFLOW_SERIALIZE](model) {
return serializeModelOptions({
modelId: model.modelId,
config: model.config
});
}
static [WORKFLOW_DESERIALIZE](options) {
return new AmazonBedrockChatLanguageModel(options.modelId, options.config);
}
constructor(modelId, config) {
this.modelId = modelId;
this.config = config;
this.specificationVersion = "v4";
this.provider = "amazon-bedrock";
this.supportedUrls = {
"image/*": [/^s3:\/\//],
"video/*": [/^s3:\/\//]
};
}
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, stopSequences, responseFormat, seed, tools, toolChoice, reasoning, providerOptions }) {
let amazonBedrockOptions = (await resolveProviderOptions({
providerOptions,
namespaces: ["amazonBedrock", "bedrock"],
resolution: "first",
schema: amazonBedrockLanguageModelChatOptions
})).options ?? {};
const anthropicOptions = await parseProviderOptions({
provider: "anthropic",
providerOptions,
schema: anthropicProviderOptions
});
const warnings = [];
const { supportsStructuredOutput: modelSupportsStructuredOutput, rejectsSamplingParameters, rejectsForcedToolUse, rejectsThinkingDisabled, rejectsThinkingDisabledAboveHighEffort, supportsBetweenToolsThinking, isKnownModel } = getModelCapabilities(this.modelId);
if (frequencyPenalty != null) warnings.push({
type: "unsupported",
feature: "frequencyPenalty"
});
if (presencePenalty != null) warnings.push({
type: "unsupported",
feature: "presencePenalty"
});
if (seed != null) warnings.push({
type: "unsupported",
feature: "seed"
});
if (rejectsSamplingParameters) {
if (temperature != null) {
warnings.push({
type: "unsupported",
feature: "temperature",
details: `temperature is not supported by ${this.modelId} and will be ignored`
});
temperature = void 0;
}
if (topK != null) {
warnings.push({
type: "unsupported",
feature: "topK",
details: `topK is not supported by ${this.modelId} and will be ignored`
});
topK = void 0;
}
if (topP != null) {
warnings.push({
type: "unsupported",
feature: "topP",
details: `topP is not supported by ${this.modelId} and will be ignored`
});
topP = void 0;
}
}
const modelEntry = getAmazonBedrockLanguageModelEntry(this.modelId);
const { openai: isOpenAIModel, openaiGptOss: isOpenAIGptOssModel, nova2Reasoning: isNovaReasoningModel } = modelEntry.requestConfig.language;
const shouldNormalizeTemperature = !isOpenAIModel || isOpenAIGptOssModel;
if (shouldNormalizeTemperature && temperature != null && temperature > 1) {
warnings.push({
type: "unsupported",
feature: "temperature",
details: `${temperature} exceeds bedrock maximum of 1.0. clamped to 1.0`
});
temperature = 1;
} else if (shouldNormalizeTemperature && temperature != null && temperature < 0) {
warnings.push({
type: "unsupported",
feature: "temperature",
details: `${temperature} is below bedrock minimum of 0. clamped to 0`
});
temperature = 0;
}
if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") warnings.push({
type: "unsupported",
feature: "responseFormat",
details: "Only text and json response formats are supported."
});
const isAnthropicModel$1 = isAnthropicModel({
modelId: this.modelId,
modelFamily: this.config.modelFamily,
reasoningBudgetTokens: amazonBedrockOptions.reasoningConfig?.budgetTokens
});
const hasExplicitDisabledThinking = amazonBedrockOptions.reasoningConfig?.type === "disabled";
const explicitReasoningEffort = amazonBedrockOptions.reasoningConfig?.maxReasoningEffort;
amazonBedrockOptions = resolveAmazonBedrockReasoningConfig({
reasoning,
amazonBedrockOptions,
warnings,
isAnthropicModel: isAnthropicModel$1,
isOpenAIModel,
modelId: this.modelId
});
let thinkingType = amazonBedrockOptions.reasoningConfig?.type;
let maxReasoningEffort = amazonBedrockOptions.reasoningConfig?.maxReasoningEffort;
if (isAnthropicModel$1 && thinkingType === "disabled") {
if (/claude-(?:instant|v?2|3-(?:5-)?(?:haiku|sonnet|opus))(?=[-.:]|$)/.test(this.modelId)) thinkingType = void 0;
else if (rejectsThinkingDisabled) {
thinkingType = supportsBetweenToolsThinking ? "between_tools" : "adaptive";
if (!hasExplicitDisabledThinking && thinkingType === "adaptive") {
maxReasoningEffort ??= explicitReasoningEffort;
if (maxReasoningEffort == null && amazonBedrockOptions.additionalModelRequestFields?.output_config?.effort == null) maxReasoningEffort = "low";
}
warnings.push({
type: hasExplicitDisabledThinking ? "unsupported" : "compatibility",
feature: hasExplicitDisabledThinking ? "providerOptions.amazonBedrock.reasoningConfig" : "reasoning",
details: `thinking cannot be disabled for ${this.modelId}. Using '${thinkingType}' thinking instead.`
});
}
const effort = maxReasoningEffort ?? amazonBedrockOptions.additionalModelRequestFields?.output_config?.effort;
if ((thinkingType === "disabled" && rejectsThinkingDisabledAboveHighEffort || thinkingType === "between_tools") && (effort === "xhigh" || effort === "max")) {
warnings.push({
type: "unsupported",
feature: maxReasoningEffort != null ? "providerOptions.amazonBedrock.reasoningConfig.maxReasoningEffort" : "providerOptions.amazonBedrock.additionalModelRequestFields.output_config.effort",
details: `effort '${effort}' is not supported by ${this.modelId} with '${thinkingType}' thinking. The effort has been lowered to 'high'.`
});
maxReasoningEffort = "high";
}
}
const isThinkingEnabled = thinkingType === "enabled" || thinkingType === "adaptive" || thinkingType === "between_tools";
const structuredOutputMode = amazonBedrockOptions.structuredOutputMode ?? anthropicOptions?.structuredOutputMode ?? "auto";
if (structuredOutputMode === "jsonTool") {
const additionalModelRequestFields = { ...amazonBedrockOptions.additionalModelRequestFields };
const outputConfig = additionalModelRequestFields.output_config;
if (outputConfig != null && typeof outputConfig === "object" && !Array.isArray(outputConfig)) {
const outputConfigWithoutFormat = { ...outputConfig };
delete outputConfigWithoutFormat.format;
if (Object.keys(outputConfigWithoutFormat).length > 0) additionalModelRequestFields.output_config = outputConfigWithoutFormat;
else delete additionalModelRequestFields.output_config;
amazonBedrockOptions = {
...amazonBedrockOptions,
additionalModelRequestFields
};
}
}
const modelSupportsNativeStructuredOutput = supportsNativeStructuredOutput(this.modelId) && (modelSupportsStructuredOutput || isThinkingEnabled || this.config.modelFamily === "anthropic");
const useNativeStructuredOutput = isAnthropicModel$1 && responseFormat?.type === "json" && responseFormat.schema != null && (structuredOutputMode === "outputFormat" || structuredOutputMode === "auto" && modelSupportsNativeStructuredOutput && supportsNativeStructuredOutputSchema(responseFormat.schema));
const useJsonInstructionForStructuredOutput = !useNativeStructuredOutput && isAnthropicModel$1 && responseFormat?.type === "json" && responseFormat.schema != null && (rejectsForcedToolUse || modelEntry.requestConfig.language.claude && (!isKnownModel || !modelSupportsStructuredOutput) && !supportsNativeStructuredOutput(this.modelId) && structuredOutputMode !== "jsonTool" || structuredOutputMode !== "jsonTool" && !supportsStrictTools(this.modelId) && tools != null && tools.length > 0);
const jsonResponseTool = responseFormat?.type === "json" && responseFormat.schema != null && !useNativeStructuredOutput && !useJsonInstructionForStructuredOutput ? {
type: "function",
name: "json",
description: "Respond with a JSON object.",
inputSchema: responseFormat.schema
} : void 0;
const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools$1({
tools: jsonResponseTool ? [...(tools ?? []).filter((tool) => toolChoice?.type !== "none" && (toolChoice?.type !== "tool" || tool.name === toolChoice.toolName)), jsonResponseTool] : tools,
toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
modelId: this.modelId,
modelFamily: this.config.modelFamily,
reasoningBudgetTokens: amazonBedrockOptions.reasoningConfig?.budgetTokens,
disableParallelToolUse: anthropicOptions?.disableParallelToolUse,
rejectsForcedToolUse
});
warnings.push(...toolWarnings);
if (additionalTools) amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
...additionalTools
};
if (betas.size > 0 || amazonBedrockOptions.anthropicBeta) {
const existingBetas = amazonBedrockOptions.anthropicBeta ?? [];
const mergedBetas = betas.size > 0 ? [...existingBetas, ...Array.from(betas)] : existingBetas;
amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
anthropic_beta: mergedBetas
};
}
const thinkingBudget = thinkingType === "enabled" ? amazonBedrockOptions.reasoningConfig?.budgetTokens : void 0;
const thinkingDisplay = thinkingType === "adaptive" ? amazonBedrockOptions.reasoningConfig?.display : void 0;
const isAnthropicThinkingEnabled = isAnthropicModel$1 && isThinkingEnabled;
const inferenceConfig = {
...maxOutputTokens != null && { maxTokens: maxOutputTokens },
...temperature != null && { temperature },
...topP != null && { topP },
...topK != null && { topK },
...stopSequences != null && { stopSequences }
};
if (isAnthropicModel$1) {
if (thinkingBudget != null) {
if (inferenceConfig.maxTokens != null) inferenceConfig.maxTokens += thinkingBudget;
else inferenceConfig.maxTokens = thinkingBudget + 4096;
amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
thinking: {
type: "enabled",
budget_tokens: thinkingBudget
}
};
} else if (thinkingType === "adaptive") amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
thinking: {
type: "adaptive",
...thinkingDisplay != null && { display: thinkingDisplay }
}
};
else if (thinkingType === "disabled" || thinkingType === "between_tools") amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
thinking: { type: thinkingType }
};
} else {
if (amazonBedrockOptions.reasoningConfig?.budgetTokens != null) warnings.push({
type: "unsupported",
feature: "budgetTokens",
details: "budgetTokens applies only to Anthropic models on Bedrock and will be ignored for this model."
});
if (thinkingType === "adaptive") warnings.push({
type: "unsupported",
feature: "adaptive thinking",
details: "adaptive thinking type applies only to Anthropic models on Bedrock."
});
}
if (maxReasoningEffort != null) {
if (isAnthropicModel$1) amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
output_config: {
...amazonBedrockOptions.additionalModelRequestFields?.output_config,
effort: maxReasoningEffort
}
};
else if (isOpenAIModel) amazonBedrockOptions.additionalModelRequestFields = isOpenAIGptOssModel ? {
...amazonBedrockOptions.additionalModelRequestFields,
reasoning_effort: maxReasoningEffort
} : {
...amazonBedrockOptions.additionalModelRequestFields,
reasoning: {
...amazonBedrockOptions.additionalModelRequestFields?.reasoning,
effort: maxReasoningEffort
}
};
else amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
reasoningConfig: {
...thinkingType != null && thinkingType !== "adaptive" && { type: thinkingType },
...thinkingBudget != null && { budgetTokens: thinkingBudget },
maxReasoningEffort
}
};
}
if (isNovaReasoningModel && thinkingType === "enabled" && (maxReasoningEffort === "high" || maxReasoningEffort === "xhigh" || maxReasoningEffort === "max") && inferenceConfig.maxTokens != null) {
delete inferenceConfig.maxTokens;
warnings.push({
type: "unsupported",
feature: "maxOutputTokens",
details: `maxOutputTokens is not supported by ${this.modelId} when high reasoning is enabled and will be ignored`
});
}
if (useNativeStructuredOutput) amazonBedrockOptions.additionalModelRequestFields = {
...amazonBedrockOptions.additionalModelRequestFields,
output_config: {
...amazonBedrockOptions.additionalModelRequestFields?.output_config,
format: {
type: "json_schema",
schema: sanitizeJsonSchema(responseFormat.schema)
}
}
};
if (isAnthropicThinkingEnabled && inferenceConfig.temperature != null) {
delete inferenceConfig.temperature;
warnings.push({
type: "unsupported",
feature: "temperature",
details: "temperature is not supported when thinking is enabled"
});
}
if (isAnthropicThinkingEnabled && inferenceConfig.topP != null) {
delete inferenceConfig.topP;
warnings.push({
type: "unsupported",
feature: "topP",
details: "topP is not supported when thinking is enabled"
});
}
if (isAnthropicThinkingEnabled && inferenceConfig.topK != null) {
delete inferenceConfig.topK;
warnings.push({
type: "unsupported",
feature: "topK",
details: "topK is not supported when thinking is enabled"
});
}
if (isOpenAIModel) {
const unsupportedFeatures = isOpenAIGptOssModel ? ["stopSequences"] : [
"temperature",
"topP",
"stopSequences"
];
for (const feature of unsupportedFeatures) if (inferenceConfig[feature] != null) {
delete inferenceConfig[feature];
warnings.push({
type: "unsupported",
feature,
details: `${feature} is not supported by this OpenAI model on the Converse API`
});
}
}
const hasAnyTools = (toolConfig.tools?.length ?? 0) > 0 || additionalTools;
let filteredPrompt = prompt;
if (!hasAnyTools) {
if (prompt.some((message) => "content" in message && Array.isArray(message.content) && message.content.some((part) => part.type === "tool-call" || part.type === "tool-result"))) {
filteredPrompt = prompt.map((message) => message.role === "system" ? message : {
...message,
content: message.content.filter((part) => part.type !== "tool-call" && part.type !== "tool-result")
}).filter((message) => message.role === "system" || message.content.length > 0);
warnings.push({
type: "unsupported",
feature: "toolContent",
details: "Tool calls and results removed from conversation because Bedrock does not support tool content without active tools."
});
}
}
if (useJsonInstructionForStructuredOutput) filteredPrompt = injectJsonInstructionIntoMessages({
messages: filteredPrompt,
schema: responseFormat.schema,
schemaSuffix: "You MUST answer with only a JSON object that matches the JSON schema above. Do not wrap it in markdown fences or include any other text."
});
const isMistral = isMistralModel(this.modelId);
const { system, messages } = await convertToAmazonBedrockChatMessages(filteredPrompt, isMistral);
const { reasoningConfig: _, additionalModelRequestFields: __, serviceTier: ___, structuredOutputMode: ____, requestMetadata: _____, ...filteredAmazonBedrockOptions } = providerOptions?.amazonBedrock ?? providerOptions?.bedrock ?? {};
const resolvedRequestMetadata = amazonBedrockOptions.requestMetadata;
const additionalModelResponseFieldPaths = isAnthropicModel$1 ? ["/delta/stop_sequence"] : void 0;
return {
command: {
system,
messages,
additionalModelRequestFields: amazonBedrockOptions.additionalModelRequestFields,
...additionalModelResponseFieldPaths && { additionalModelResponseFieldPaths },
...Object.keys(inferenceConfig).length > 0 && { inferenceConfig },
...amazonBedrockOptions.serviceTier != null && { serviceTier: { type: amazonBedrockOptions.serviceTier } },
...resolvedRequestMetadata != null && { requestMetadata: resolvedRequestMetadata },
...filteredAmazonBedrockOptions,
...toolConfig.tools !== void 0 && toolConfig.tools.length > 0 ? { toolConfig } : {}
},
warnings,
usesJsonInstruction: useJsonInstructionForStructuredOutput,
usesJsonResponseTool: jsonResponseTool != null,
betas
};
}
async getHeaders({ headers }) {
return combineHeaders(this.config.headers ? await resolve(this.config.headers) : void 0, headers);
}
async doGenerate(options) {
const { command: args, warnings, usesJsonInstruction, usesJsonResponseTool } = await this.getArgs(options);
const url = `${this.getUrl(this.modelId)}/converse`;
const { value: response, responseHeaders } = await postJsonToApi({
url,
headers: await this.getHeaders({ headers: options.headers }),
body: args,
failedResponseHandler: createJsonErrorResponseHandler({
errorSchema: AmazonBedrockErrorSchema,
errorToMessage: (error) => `${error.message ?? "Unknown error"}`
}),
successfulResponseHandler: createJsonResponseHandler(AmazonBedrockResponseSchema),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const content = [];
let isJsonResponseFromTool = false;
const jsonObjectTextExtractor = usesJsonInstruction ? new JsonObjectTextExtractor() : void 0;
for (const part of response.output.message.content) {
const textParts = part.text != null ? [part.text] : part.citationsContent?.content?.flatMap((content) => content.text == null ? [] : [content.text]) ?? [];
for (const text of textParts) content.push({
type: "text",
text: jsonObjectTextExtractor?.process(text) ?? text
});
if (part.reasoningContent) {
if ("reasoningText" in part.reasoningContent) {
const reasoning = {
type: "reasoning",
text: part.reasoningContent.reasoningText.text
};
if (part.reasoningContent.reasoningText.signature) {
const reasoningPayload = { signature: part.reasoningContent.reasoningText.signature };
reasoning.providerMetadata = {
amazonBedrock: reasoningPayload,
bedrock: reasoningPayload
};
}
content.push(reasoning);
} else if ("redactedReasoning" in part.reasoningContent) {
const redactedPayload = { redactedData: part.reasoningContent.redactedReasoning.data ?? "" };
content.push({
type: "reasoning",
text: "",
providerMetadata: {
amazonBedrock: redactedPayload,
bedrock: redactedPayload
}
});
} else if ("redactedContent" in part.reasoningContent) {
const redactedPayload = { redactedContent: part.reasoningContent.redactedContent };
content.push({
type: "reasoning",
text: "",
providerMetadata: {
amazonBedrock: redactedPayload,
bedrock: redactedPayload
}
});
}
}
if (part.toolUse) {
if (usesJsonResponseTool && part.toolUse.name === "json") {
isJsonResponseFromTool = true;
content.push({
type: "text",
text: JSON.stringify(part.toolUse.input)
});
} else {
const isMistral = isMistralModel(this.modelId);
const rawToolCallId = part.toolUse?.toolUseId || this.config.generateId();
content.push({
type: "tool-call",
toolCallId: normalizeToolCallId(rawToolCallId, isMistral),
toolName: part.toolUse?.name ?? `tool-${this.config.generateId()}`,
input: JSON.stringify(part.toolUse?.input ?? {})
});
}
}
}
const stopSequence = response.additionalModelResponseFields?.delta?.stop_sequence ?? null;
const providerMetadataPayload = response.trace || response.usage || response.performanceConfig || response.serviceTier || isJsonResponseFromTool || stopSequence ? {
...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
...response.performanceConfig && { performanceConfig: response.performanceConfig },
...response.serviceTier && { serviceTier: response.serviceTier },
...(response.usage?.cacheWriteInputTokens != null || response.usage?.cacheDetails != null) && { usage: {
...response.usage.cacheWriteInputTokens != null && { cacheWriteInputTokens: response.usage.cacheWriteInputTokens },
...response.usage.cacheDetails != null && { cacheDetails: response.usage.cacheDetails }
} },
...isJsonResponseFromTool && { isJsonResponseFromTool: true },
stopSequence
} : void 0;
const providerMetadata = providerMetadataPayload ? {
amazonBedrock: providerMetadataPayload,
bedrock: providerMetadataPayload
} : void 0;
return {
content,
finishReason: {
unified: mapAmazonBedrockFinishReason(response.stopReason, isJsonResponseFromTool),
raw: response.stopReason ?? void 0
},
usage: convertAmazonBedrockUsage(response.usage),
response: {
id: responseHeaders?.["x-amzn-requestid"] ?? void 0,
timestamp: responseHeaders?.["date"] != null ? new Date(responseHeaders["date"]) : void 0,
modelId: this.modelId,
headers: responseHeaders
},
warnings,
request: { body: args },
...providerMetadata && { providerMetadata }
};
}
async doStream(options) {
const { command: args, warnings, usesJsonInstruction, usesJsonResponseTool } = await this.getArgs(options);
const modelId = this.modelId;
const isMistral = isMistralModel(modelId);
const url = `${this.getUrl(modelId)}/converse-stream`;
const { value: response, responseHeaders } = await postJsonToApi({
url,
headers: await this.getHeaders({ headers: options.headers }),
body: args,
failedResponseHandler: amazonBedrockFailedResponseHandler,
successfulResponseHandler: createAmazonBedrockEventStreamResponseHandler(AmazonBedrockStreamSchema),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
let finishReason = {
unified: "interrupted",
raw: void 0
};
let usage = void 0;
let providerMetadata = void 0;
let isJsonResponseFromTool = false;
let stopSequence = null;
const jsonObjectTextExtractor = usesJsonInstruction ? new JsonObjectTextExtractor() : void 0;
const contentBlocks = {};
return {
stream: response.pipeThrough(new TransformStream({
start(controller) {
controller.enqueue({
type: "stream-start",
warnings
});
controller.enqueue({
type: "response-metadata",
id: responseHeaders?.["x-amzn-requestid"] ?? void 0,
timestamp: responseHeaders?.["date"] != null ? new Date(responseHeaders["date"]) : void 0,
modelId
});
},
transform(chunk, controller) {
function enqueueError(type, amazonBedrockError) {
finishReason = {
unified: "error",
raw: void 0
};
controller.enqueue({
type: "error",
error: createAmazonBedrockStreamError({
type,
error: amazonBedrockError,
data: chunk.rawValue
})
});
}
if (options.includeRawChunks) controller.enqueue({
type: "raw",
rawValue: chunk.rawValue
});
if (!chunk.success) {
finishReason = {
unified: "error",
raw: void 0
};
controller.enqueue({
type: "error",
error: chunk.error
});
return;
}
const value = chunk.value;
if (value.internalServerException) {
enqueueError("internalServerException", value.internalServerException);
return;
}
if (value.modelStreamErrorException) {
enqueueError("modelStreamErrorException", value.modelStreamErrorException);
return;
}
if (value.serviceUnavailableException) {
enqueueError("serviceUnavailableException", value.serviceUnavailableException);
return;
}
if (value.throttlingException) {
enqueueError("throttlingException", value.throttlingException);
return;
}
if (value.validationException) {
enqueueError("validationException", value.validationException);
return;
}
if (value.messageStop) {
finishReason = {
unified: mapAmazonBedrockFinishReason(value.messageStop.stopReason, isJsonResponseFromTool),
raw: value.messageStop.stopReason ?? void 0
};
stopSequence = value.messageStop.additionalModelResponseFields?.delta?.stop_sequence ?? null;
}
if (value.metadata) {
if (value.metadata.usage) usage = value.metadata.usage;
const cacheUsage = value.metadata.usage?.cacheWriteInputTokens != null || value.metadata.usage?.cacheDetails != null ? { usage: {
...value.metadata.usage?.cacheWriteInputTokens != null && { cacheWriteInputTokens: value.metadata.usage.cacheWriteInputTokens },
...value.metadata.usage?.cacheDetails != null && { cacheDetails: value.metadata.usage.cacheDetails }
} } : void 0;
const trace = value.metadata.trace ? { trace: value.metadata.trace } : void 0;
if (cacheUsage || trace || value.metadata.performanceConfig || value.metadata.serviceTier) {
const metadataPayload = {
...cacheUsage,
...trace,
...value.metadata.performanceConfig && { performanceConfig: value.metadata.performanceConfig },
...value.metadata.serviceTier && { serviceTier: value.metadata.serviceTier }
};
providerMetadata = {
amazonBedrock: metadataPayload,
bedrock: metadataPayload
};
}
}
if (value.contentBlockStart?.contentBlockIndex != null && !value.contentBlockStart?.start?.toolUse) {
const blockIndex = value.contentBlockStart.contentBlockIndex;
contentBlocks[blockIndex] = { type: "text" };
controller.enqueue({
type: "text-start",
id: String(blockIndex)
});
}
if (value.contentBlockDelta?.delta && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
if (contentBlocks[blockIndex] == null) {
contentBlocks[blockIndex] = { type: "text" };
controller.enqueue({
type: "text-start",
id: String(blockIndex)
});
}
const textDelta = jsonObjectTextExtractor?.process(value.contentBlockDelta.delta.text) ?? value.contentBlockDelta.delta.text;
if (textDelta.length > 0) controller.enqueue({
type: "text-delta",
id: String(blockIndex),
delta: textDelta
});
}
if (value.contentBlockStop?.contentBlockIndex != null) {
const blockIndex = value.contentBlockStop.contentBlockIndex;
const contentBlock = contentBlocks[blockIndex];
if (contentBlock != null) {
if (contentBlock.type === "reasoning") {
const redactedPayload = contentBlock.redactedContent != null ? { redactedContent: contentBlock.redactedContent } : null;
controller.enqueue({
type: "reasoning-end",
id: String(blockIndex),
...redactedPayload != null ? { providerMetadata: {
amazonBedrock: redactedPayload,
bedrock: redactedPayload
} } : {}
});
} else if (contentBlock.type === "text") controller.enqueue({
type: "text-end",
id: String(blockIndex)
});
else if (contentBlock.type === "tool-call") {
if (contentBlock.isJsonResponseTool) {
isJsonResponseFromTool = true;
controller.enqueue({
type: "text-start",
id: String(blockIndex)
});
controller.enqueue({
type: "text-delta",
id: String(blockIndex),
delta: contentBlock.jsonText
});
controller.enqueue({
type: "text-end",
id: String(blockIndex)
});
} else {
controller.enqueue({
type: "tool-input-end",
id: contentBlock.toolCallId
});
controller.enqueue({
type: "tool-call",
toolCallId: contentBlock.toolCallId,
toolName: contentBlock.toolName,
input: contentBlock.jsonText === "" ? "{}" : contentBlock.jsonText
});
}
}
delete contentBlocks[blockIndex];
}
}
if (value.contentBlockDelta?.delta && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
if ("text" in reasoningContent && reasoningContent.text) {
if (contentBlocks[blockIndex] == null) {
contentBlocks[blockIndex] = { type: "reasoning" };
controller.enqueue({
type: "reasoning-start",
id: String(blockIndex)
});
}
controller.enqueue({
type: "reasoning-delta",
id: String(blockIndex),
delta: reasoningContent.text
});
} else if ("signature" in reasoningContent && reasoningContent.signature) {
if (contentBlocks[blockIndex] == null) {
contentBlocks[blockIndex] = { type: "reasoning" };
controller.enqueue({
type: "reasoning-start",
id: String(blockIndex)
});
}
{
const signaturePayload = { signature: reasoningContent.signature };
controller.enqueue({
type: "reasoning-delta",
id: String(blockIndex),
delta: "",
providerMetadata: {
amazonBedrock: signaturePayload,
bedrock: signaturePayload
}
});
}
} else if ("data" in reasoningContent && reasoningContent.data) {
if (contentBlocks[blockIndex] == null) {
contentBlocks[blockIndex] = { type: "reasoning" };
controller.enqueue({
type: "reasoning-start",
id: String(blockIndex)
});
}
{
const redactedPayload = { redactedData: reasoningContent.data };
controller.enqueue({
type: "reasoning-delta",
id: String(blockIndex),
delta: "",
providerMetadata: {
amazonBedrock: redactedPayload,
bedrock: redactedPayload
}
});
}
} else if ("redactedContent" in reasoningContent && reasoningContent.redactedContent) {
if (contentBlocks[blockIndex] == null) {
contentBlocks[blockIndex] = { type: "reasoning" };
controller.enqueue({
type: "reasoning-start",
id: String(blockIndex)
});
}
const contentBlock = contentBlocks[blockIndex];
if (contentBlock.type === "reasoning") contentBlock.redactedContent = (contentBlock.redactedContent ?? "") + reasoningContent.redactedContent;
}
}
const contentBlockStart = value.contentBlockStart;
if (contentBlockStart?.start?.toolUse != null) {
const toolUse = contentBlockStart.start.toolUse;
const blockIndex = contentBlockStart.contentBlockIndex;
const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
const normalizedToolCallId = normalizeToolCallId(toolUse.toolUseId, isMistral);
contentBlocks[blockIndex] = {
type: "tool-call",
toolCallId: normalizedToolCallId,
toolName: toolUse.name,
jsonText: "",
isJsonResponseTool
};
if (!isJsonResponseTool) controller.enqueue({
type: "tool-input-start",
id: normalizedToolCallId,
toolName: toolUse.name
});
}
const contentBlockDelta = value.contentBlockDelta;
if (contentBlockDelta?.delta && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
const blockIndex = contentBlockDelta.contentBlockIndex;
const contentBlock = contentBlocks[blockIndex];
if (contentBlock?.type === "tool-call") {
const delta = contentBlockDelta.delta.toolUse.input ?? "";
if (!contentBlock.isJsonResponseTool) controller.enqueue({
type: "tool-input-delta",
id: contentBlock.toolCallId,
delta
});
contentBlock.jsonText += delta;
}
}
},
flush(controller) {
if (isJsonResponseFromTool || stopSequence != null) {
const updatePayload = {
...providerMetadata?.amazonBedrock ?? providerMetadata?.bedrock,
...isJsonResponseFromTool && { isJsonResponseFromTool: true },
stopSequence
};
providerMetadata = {
...providerMetadata,
amazonBedrock: updatePayload,
bedrock: updatePayload
};
}
controller.enqueue({
type: "finish",
finishReason,
usage: convertAmazonBedrockUsage(usage),
...providerMetadata && { providerMetadata }
});
}
})),
request: { body: args },
response: { headers: responseHeaders }
};
}
getUrl(modelId) {
return `${this.config.baseUrl()}/model/${encodeURIComponent(modelId)}`;
}
};
var JsonObjectTextExtractor = class {
constructor() {
this.started = false;
this.completed = false;
this.depth = 0;
this.inString = false;
this.escaped = false;
}
process(text) {
let result = "";
for (const character of text) {
if (this.completed) break;
if (!this.started) {
if (character !== "{") continue;
this.started = true;
this.depth = 1;
result += character;
continue;
}
result += character;
if (this.escaped) {
this.escaped = false;
continue;
}
if (character === "\\" && this.inString) {
this.escaped = true;
continue;
}
if (character === "\"") {
this.inString = !this.inString;
continue;
}
if (this.inString) continue;
if (character === "{") this.depth++;
else if (character === "}") {
this.depth--;
if (this.depth === 0) this.completed = true;
}
}
return result;
}
};
const AmazonBedrockStopReasonSchema = z.union([z.enum(BEDROCK_STOP_REASONS), z.string()]);
const AmazonBedrockAdditionalModelResponseFieldsSchema = z.object({ delta: z.object({ stop_sequence: z.string().nullish() }).nullish() }).catchall(z.unknown());
const AmazonBedrockToolUseSchema = z.object({
toolUseId: z.string(),
name: z.string(),
input: z.unknown().optional()
});
const AmazonBedrockReasoningTextSchema = () => z.object({
signature: z.string().nullish(),
text: z.string()
});
const AmazonBedrockRedactedReasoningSchema = () => z.object({ data: z.string() });
const AmazonBedrockCacheDetailSchema = z.object({
inputTokens: z.number(),
ttl: z.string()
}).catchall(z.json());
const AmazonBedrockResponseSchema = lazySchema(() => zodSchema(z.object({
metrics: z.object({ latencyMs: z.number() }).nullish(),
output: z.object({ message: z.object({
content: z.array(z.object({
text: z.string().nullish(),
citationsContent: z.object({ content: z.array(z.object({ text: z.string().nullish() })).nullish() }).nullish(),
toolUse: AmazonBedrockToolUseSchema.nullish(),
reasoningContent: z.union([
z.object({ reasoningText: AmazonBedrockReasoningTextSchema() }),
z.object({ redactedReasoning: AmazonBedrockRedactedReasoningSchema() }),
z.object({ redactedContent: z.string() })
]).nullish()
})),
role: z.string()
}) }),
stopReason: AmazonBedrockStopReasonSchema,
additionalModelResponseFields: AmazonBedrockAdditionalModelResponseFieldsSchema.nullish(),
trace: z.unknown().nullish(),
performanceConfig: z.object({ latency: z.string() }).nullish(),
serviceTier: z.object({ type: z.string() }).nullish(),
usage: z.object({
inputTokens: z.number(),
outputTokens: z.number(),
totalTokens: z.number(),
cacheReadInputTokens: z.number().nullish(),
cacheWriteInputTokens: z.number().nullish(),
cacheDetails: z.array(AmazonBedrockCacheDetailSchema).nullish()
}).catchall(z.json())
})));
const AmazonBedrockStreamSchema = z.object({
contentBlockDelta: z.object({
contentBlockIndex: z.number(),
delta: z.union([
z.object({ text: z.string() }),
z.object({ citation: z.record(z.string(), z.unknown()) }),
z.object({ toolUse: z.object({ input: z.string() }) }),
z.object({ reasoningContent: z.object({ text: z.string() }) }),
z.object({ reasoningContent: z.object({ signature: z.string() }) }),
z.object({ reasoningContent: z.object({ data: z.string() }) }),
z.object({ reasoningContent: z.object({ redactedContent: z.string() }) })
]).nullish()
}).nullish(),
contentBlockStart: z.object({
contentBlockIndex: z.number(),
start: z.object({ toolUse: AmazonBedrockToolUseSchema.nullish() }).nullish()
}).nullish(),
contentBlockStop: z.object({ contentBlockIndex: z.number() }).nullish(),
internalServerException: z.record(z.string(), z.unknown()).nullish(),
messageStop: z.object({
additionalModelResponseFields: AmazonBedrockAdditionalModelResponseFieldsSchema.nullish(),
stopReason: AmazonBedrockStopReasonSchema
}).nullish(),
metadata: z.object({
trace: z.unknown().nullish(),
performanceConfig: z.object({ latency: z.string() }).nullish(),
serviceTier: z.object({ type: z.string() }).nullish(),
usage: z.object({
cacheReadInputTokens: z.number().nullish(),
cacheWriteInputTokens: z.number().nullish(),
cacheDetails: z.array(AmazonBedrockCacheDetailSchema).nullish(),
inputTokens: z.number(),
outputTokens: z.number(),
totalTokens: z.number().optional()
}).catchall(z.json()).nullish()
}).nullish(),
modelStreamErrorException: z.record(z.string(), z.unknown()).nullish(),
serviceUnavailableException: z.record(z.string(), z.unknown()).nullish(),
throttlingException: z.record(z.string(), z.unknown()).nullish(),
validationException: z.record(z.string(), z.unknown()).nullish()
});
const amazonBedrockReasoningEffortMap = {
low: "low",
medium: "medium",
high: "high",
max: "max"
};
const amazonNovaReasoningEffortMap = {
low: "low",
medium: "medium",
high: "high"
};
const openAIGptOssReasoningEffortMap = {
low: "low",
medium: "medium",
high: "high"
};
const openAIReasoningEffortMap = {
none: "none",
minimal: "minimal",
low: "low",
medium: "medium",
high: "high",
xhigh: "xhigh",
max: "max"
};
function isNova2ReasoningModel(modelId) {
return getAmazonBedrockLanguageModelEntry(modelId).requestConfig.language.nova2Reasoning;
}
function resolveAmazonBedrockReasoningConfig({ reasoning, amazonBedrockOptions, warnings, isAnthropicModel, isOpenAIModel, modelId }) {
const reasoningConfig = amazonBedrockOptions.reasoningConfig;
if (!isCustomReasoning(reasoning)) return amazonBedrockOptions;
const result = { ...amazonBedrockOptions };
if (reasoningConfig?.type === "disabled") {
result.reasoningConfig = { ...reasoningConfig };
delete result.reasoningConfig.maxReasoningEffort;
delete result.reasoningConfig.budgetTokens;
return result;
}
const isNovaReasoningModel = isNova2ReasoningModel(modelId);
const supportsPortableReasoning = isOpenAIModel || isNovaReasoningModel || reasoningConfig != null;
if (isAnthropicModel) {
const capabilities = getModelCapabilities(modelId);
if (reasoning === "none") result.reasoningConfig = { type: "disabled" };
else if (capabilities.supportsAdaptiveThinking) {
const effort = reasoningConfig?.maxReasoningEffort ?? mapReasoningToProviderEffort({
reasoning,
effortMap: {
...amazonBedrockReasoningEffortMap,
...capabilities.supportsXhighEffort && { xhigh: "xhigh" }
},
warnings
});
result.reasoningConfig = {
...reasoningConfig,
type: reasoningConfig?.type ?? "adaptive",
maxReasoningEffort: effort
};
} else {
const budgetTokens = reasoningConfig?.budgetTokens ?? mapReasoningToProviderBudget({
reasoning,
maxOutputTokens: capabilities.maxOutputTokens,
maxReasoningBudget: capabilities.maxOutputTokens,
warnings
});
result.reasoningConfig = {
...reasoningConfig,
type: reasoningConfig?.type ?? "enabled",
budgetTokens
};
}
} else if (reasoning === "none" && isNovaReasoningModel) {} else if (supportsPortableReasoning) {
const effort = reasoningConfig?.maxReasoningEffort ?? mapReasoningToProviderEffort({
reasoning,
effortMap: isNovaReasoningModel ? amazonNovaReasoningEffortMap : isOpenAIModel ? getAmazonBedrockLanguageModelEntry(modelId).requestConfig.language.openaiGptOssReasoning ? openAIGptOssReasoningEffortMap : openAIReasoningEffortMap : amazonBedrockReasoningEffortMap,
warnings
});
result.reasoningConfig = {
...reasoningConfig,
...isNovaReasoningModel && { type: reasoningConfig?.type ?? "enabled" },
maxReasoningEffort: effort
};
} else warnings.push({
type: "unsupported",
feature: "reasoning",
details: "Portable reasoning is not supported for this model and will be ignored. If the model supports a provider-specific reasoning configuration, use providerOptions.amazonBedrock.reasoningConfig."
});
return result;
}
//#endregion
//#region src/amazon-bedrock-embedding-model-options.ts
const amazonBedrockEmbeddingModelOptionsSchema = lazySchema(() => zodSchema(z.object({
/**
* The number of dimensions the resulting output embeddings should have (defaults to 1024).
* Only supported in amazon.titan-embed-text-v2:0.
*/
dimensions: z.union([
z.literal(1024),
z.literal(512),
z.literal(256)
]).optional(),
/**
* Flag indicating whether or not to normalize the output embeddings. Defaults to true.
* Only supported in amazon.titan-embed-text-v2:0.
*/
normalize: z.boolean().optional(),
/**
* The number of dimensions for Nova embedding models (defaults to 1024).
* Supported values: 256, 384, 1024, 3072.
* Only supported in amazon.nova-* embedding models.
*/
embeddingDimension: z.union([
z.literal(256),
z.literal(384),
z.literal(1024),
z.literal(3072)
]).optional(),
/**
* The purpose of the embedding. Defaults to 'GENERIC_INDEX'.
* Only supported in amazon.nova-* embedding models.
*/
embeddingPurpose: z.enum([
"GENERIC_INDEX",
"TEXT_RETRIEVAL",
"IMAGE_RETRIEVAL",
"VIDEO_RETRIEVAL",
"DOCUMENT_RETRIEVAL",
"AUDIO_RETRIEVAL",
"GENERIC_RETRIEVAL",
"CLASSIFICATION",
"CLUSTERING"
]).optional(),
/**
* Input type for Cohere embedding models on Bedrock.
* Common values: `search_document`, `search_query`, `classification`, `clustering`.
* If not set, the provider defaults to `search_query`.
*/
inputType: z.enum([
"search_document",
"search_query",
"classification",
"clustering"
]).optional(),
/**
* Truncation behavior when input exceeds the model's context length.
* Supported in Cohere and Nova embedding models. Defaults to 'END' for Nova models.
*/
truncate: z.enum([
"NONE",
"START",
"END"
]).optional(),
/**
* The number of dimensions the resulting output embeddings should have (defaults to 1536).
* Only supported in cohere.embed-v4:0 and newer Cohere embedding models.
*/
outputDimension: z.union([
z.literal(256),
z.literal(512),
z.literal(1024),
z.literal(1536)
]).optional()
})));
//#endregion
//#region src/amazon-bedrock-embedding-model.ts
var AmazonBedrockEmbeddingModel = class AmazonBedrockEmbeddingModel {
get maxEmbeddingsPerCall() {
return this.modelFamily === "cohere" ? 96 : 1;
}
get modelFamily() {
return this.config.modelFamily ?? getAmazonBedrockEmbeddingModelFamily(this.modelId);
}
static [WORKFLOW_SERIALIZE](model) {
return serializeModelOptions({
modelId: model.modelId,
config: model.config
});
}
static [WORKFLOW_DESERIALIZE](options) {
return new AmazonBedrockEmbeddingModel(options.modelId, options.config);
}
constructor(modelId, config) {
this.modelId = modelId;
this.config = config;
this.specificationVersion = "v4";
this.provider = "amazon-bedrock";
this.supportsParallelCalls = true;
}
getUrl(modelId) {
const encodedModelId = encodeURIComponent(modelId);
return `${this.config.baseUrl()}/model/${encodedModelId}/invoke`;
}
async doEmbed({ values, dimensions, headers, abortSignal, providerOptions }) {
if (values.length > this.maxEmbeddingsPerCall) throw new TooManyEmbeddingValuesForCallError({
provider: this.provider,
modelId: this.modelId,
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
values
});
const amazonBedrockOptions = await parseProviderOptions({
provider: "amazonBedrock",
providerOptions,
schema: amazonBedrockEmbeddingModelOptionsSchema
}) ?? await parseProviderOptions({
provider: "bedrock",
providerOptions,
schema: amazonBedrockEmbeddingModelOptionsSchema
}) ?? {};
const modelFamily = this.modelFamily;
const args = modelFamily === "nova" ? {
taskType: "SINGLE_EMBEDDING",
singleEmbeddingParams: {
embeddingPurpose: amazonBedrockOptions.embeddingPurpose ?? "GENERIC_INDEX",
embeddingDimension: amazonBedrockOptions.embeddingDimension ?? dimensions ?? 1024,
text: {
truncationMode: amazonBedrockOptions.truncate ?? "END",
value: values[0]
}
}
} : modelFamily === "cohere" ? {
input_type: amazonBedrockOptions.inputType ?? "search_query",
texts: values,
truncate: amazonBedrockOptions.truncate,
output_dimension: amazonBedrockOptions.outputDimension ?? dimensions
} : {
inputText: values[0],
dimensions: amazonBedrockOptions.dimensions ?? dimensions,
normalize: amazonBedrockOptions.normalize
};
const url = this.getUrl(this.modelId);
const { value: response, responseHeaders } = await postJsonToApi({
url,
headers: await resolve(combineHeaders(this.config.headers ? await resolve(this.config.headers) : void 0, headers)),
body: args,
failedResponseHandler: amazonBedrockFailedResponseHandler,
successfulResponseHandler: createJsonResponseHandler(AmazonBedrockEmbeddingResponseSchema),
fetch: this.config.fetch,
abortSignal
});
let embeddings;
if ("embedding" in response) embeddings = [response.embedding];
else if (Array.isArray(response.embeddings)) {
const firstEmbedding = response.embeddings[0];
if (typeof firstEmbedding === "object" && firstEmbedding !== null && "embeddingType" in firstEmbedding) embeddings = [firstEmbedding.embedding];
else embeddings = response.embeddings;
} else embeddings = response.embeddings.float;
const headerTokenCount = Number(responseHeaders?.["x-amzn-bedrock-input-token-count"]);
const tokens = "inputTextTokenCount" in response ? response.inputTextTokenCount : "inputTokenCount" in response ? response.inputTokenCount ?? 0 : headerTokenCount;
return {
embeddings,
usage: { tokens },
warnings: []
};
}
};
const AmazonBedrockEmbeddingResponseSchema = lazySchema(() => zodSchema(z.union([
z.object({
embedding: z.array(z.number()),
inputTextTokenCount: z.number()
}),
z.object({
embeddings: z.array(z.object({
embeddingType: z.string(),
embedding: z.array(z.number())
})),
inputTokenCount: z.number().optional()
}),
z.object({ embeddings: z.array(z.array(z.number())) }),
z.object({ embeddings: z.object({ float: z.array(z.array(z.number())) }) })
])));
//#endregion
//#region src/amazon-bedrock-image-model-options.ts
const amazonBedrockImageModelOptionsSchema = lazySchema(() => zodSchema(z.object({
quality: z.enum(["standard", "premium"]).optional(),
negativeText: z.string().optional(),
cfgScale: z.number().optional(),
style: z.enum([
"3D_ANIMATED_FAMILY_FILM",
"DESIGN_SKETCH",
"FLAT_VECTOR_ILLUSTRATION",
"GRAPHIC_NOVEL_ILLUSTRATION",
"MAXIMALISM",
"MIDCENTURY_RETRO",
"PHOTOREALISM",
"SOFT_DIGITAL_PAINTING"
]).optional(),
taskType: z.enum([
"TEXT_IMAGE",
"IMAGE_VARIATION",
"INPAINTING",
"OUTPAINTING",
"BACKGROUND_REMOVAL"
]).optional(),
maskPrompt: z.string().optional(),
outPaintingMode: z.enum(["DEFAULT", "PRECISE"]).optional(),
similarityStrength: z.number().optional()
})));
//#endregion
//#region src/amazon-bedrock-image-model.ts
var AmazonBedrockImageModel = class AmazonBedrockImageModel {
static [WORKFLOW_SERIALIZE](model) {
return serializeModelOptions({
modelId: model.modelId,
config: model.config
});
}
static [WORKFLOW_DESERIALIZE](options) {
return new AmazonBedrockImageModel(options.modelId, options.config);
}
get maxImagesPerCall() {
return getAmazonBedrockImageModelEntry(this.modelId).capabilities.image?.maxImagesPerCall ?? 1;
}
get supportsFileInputs() {
return getAmazonBedrockImageModelEntry(this.modelId).capabilities.image?.fileInput === true ? true : void 0;
}
get supportsMaskInputs() {
return getAmazonBedrockImageModelEntry(this.modelId).capabilities.image?.maskInput === true ? true : void 0;
}
getUrl(modelId) {
const encodedModelId = encodeURIComponent(modelId);
return `${this.config.baseUrl()}/model/${encodedModelId}/invoke`;
}
constructor(modelId, config) {
this.modelId = modelId;
this.config = config;
this.specificationVersion = "v4";
this.provider = "amazon-bedrock";
}
async doGenerate({ prompt, n, size, aspectRatio, seed, providerOptions, headers, abortSignal, files, mask }) {
const warnings = [];
const [width, height] = size ? size.split("x").map(Number) : [];
const hasFiles = files != null && files.length > 0;
const amazonBedrockOptions = await parseProviderOptions({
provider: "amazonBedrock",
providerOptions,
schema: amazonBedrockImageModelOptionsSchema
}) ?? await parseProviderOptions({
provider: "bedrock",
providerOptions,
schema: amazonBedrockImageModelOptionsSchema
}) ?? {};
const imageGenerationConfig = {
...width ? { width } : {},
...height ? { height } : {},
...seed != null ? { seed } : {},
...n ? { numberOfImages: n } : {},
...amazonBedrockOptions?.quality ? { quality: amazonBedrockOptions.quality } : {},
...amazonBedrockOptions?.cfgScale ? { cfgScale: amazonBedrockOptions.cfgScale } : {}
};
let args;
if (hasFiles) {
const hasMask = mask?.type != null;
const hasMaskPrompt = amazonBedrockOptions?.maskPrompt != null;
const taskType = amazonBedrockOptions?.taskType ?? (hasMask || hasMaskPrompt ? "INPAINTING" : "IMAGE_VARIATION");
const sourceImageBase64 = getBase64Data(files[0]);
switch (taskType) {
case "INPAINTING": {
const inPaintingParams = {
image: sourceImageBase64,
...prompt ? { text: prompt } : {},
...amazonBedrockOptions?.negativeText ? { negativeText: amazonBedrockOptions.negativeText } : {}
};
if (hasMask) inPaintingParams.maskImage = getBase64Data(mask);
else if (hasMaskPrompt) inPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
args = {
taskType: "INPAINTING",
inPaintingParams,
imageGenerationConfig
};
break;
}
case "OUTPAINTING": {
const outPaintingParams = {
image: sourceImageBase64,
...prompt ? { text: prompt } : {},
...amazonBedrockOptions?.negativeText ? { negativeText: amazonBedrockOptions.negativeText } : {},
...amazonBedrockOptions?.outPaintingMode ? { outPaintingMode: amazonBedrockOptions.outPaintingMode } : {}
};
if (hasMask) outPaintingParams.maskImage = getBase64Data(mask);
else if (hasMaskPrompt) outPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
args = {
taskType: "OUTPAINTING",
outPaintingParams,
imageGenerationConfig
};
break;
}
case "BACKGROUND_REMOVAL":
args = {
taskType: "BACKGROUND_REMOVAL",
backgroundRemovalParams: { image: sourceImageBase64 }
};
break;
case "IMAGE_VARIATION":
args = {
taskType: "IMAGE_VARIATION",
imageVariationParams: {
images: files.map((file) => getBase64Data(file)),
...prompt ? { text: prompt } : {},
...amazonBedrockOptions?.negativeText ? { negativeText: amazonBedrockOptions.negativeText } : {},
...amazonBedrockOptions?.similarityStrength != null ? { similarityStrength: amazonBedrockOptions.similarityStrength } : {}
},
imageGenerationConfig
};
break;
default: throw new Error(`Unsupported task type: ${taskType}`);
}
} else args = {
taskType: "TEXT_IMAGE",
textToImageParams: {
text: prompt,
...amazonBedrockOptions?.negativeText ? { negativeText: amazonBedrockOptions.negativeText } : {},
...amazonBedrockOptions?.style ? { style: amazonBedrockOptions.style } : {}
},
imageGenerationConfig
};
if (aspectRatio != void 0) warnings.push({
type: "unsupported",
feature: "aspectRatio",
details: "This model does not support aspect ratio. Use `size` instead."
});
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
const { value: response, responseHeaders } = await postJsonToApi({
url: this.getUrl(this.modelId),
headers: await resolve(combineHeaders(this.config.headers ? await resolve(this.config.headers) : void 0, headers)),
body: args,
failedResponseHandler: amazonBedrockFailedResponseHandler,
successfulResponseHandler: createJsonResponseHandler(amazonBedrockImageResponseSchema),
abortSignal,
fetch: this.config.fetch
});
if (response.status === "Request Moderated") {
const moderationReasons = response.details?.["Moderation Reasons"];
throw new Error(`Amazon Bedrock request was moderated: ${(Array.isArray(moderationReasons) ? moderationReasons : ["Unknown"]).join(", ")}`);
}
if (!response.images || response.images.length === 0) throw new Error("Amazon Bedrock returned no images. " + (response.status ? `Status: ${response.status}` : ""));
return {
images: response.images,
warnings,
response: {
timestamp: currentDate,
modelId: this.modelId,
headers: responseHeaders
}
};
}
};
function getBase64Data(file) {
if (file.type === "url") throw new Error("URL-based images are not supported for Amazon Bedrock image editing. Please provide the image data directly.");
if (file.data instanceof Uint8Array) return convertUint8ArrayToBase64(file.data);
return file.data;
}
const amazonBedrockImageResponseSchema = lazySchema(() => zodSchema(z.object({
images: z.array(z.string()).optional(),
id: z.string().optional(),
status: z.string().optional(),
result: z.unknown().optional(),
progress: z.unknown().optional(),
details: z.record(z.string(), z.unknown()).optional(),
preview: z.unknown().optional()
})));
//#endregion
//#region src/version.ts
const VERSION = "5.0.142";
//#endregion
//#region src/amazon-bedrock-sigv4-fetch.ts
/**
* Creates a fetch function that applies AWS Signature Version 4 signing.
*
* @param getCredentials - Function that returns the AWS credentials to use when signing.
* @param fetch - Optional original fetch implementation to wrap. Defaults to global fetch.
* @param service - The AWS service name to use for SigV4 signing scope. Defaults to 'bedrock'.
* @returns A FetchFunction that signs requests before passing them to the underlying fetch.
*/
function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
return async (input, init) => {
const effectiveFetch = fetch ?? globalThis.fetch;
const request = input instanceof Request ? input : void 0;
const originalHeaders = combineHeaders(normalizeHeaders(request?.headers), normalizeHeaders(init?.headers));
const headersWithUserAgent = withUserAgentSuffix(originalHeaders, `ai-sdk-amazon-bedrock/${VERSION}`, getRuntimeEnvironmentUserAgent());
let effectiveBody = init?.body ?? void 0;
if (effectiveBody === void 0 && request && request.body !== null) try {
effectiveBody = await request.clone().text();
} catch {}
if ((init?.method ?? request?.method)?.toUpperCase() !== "POST" || !effectiveBody) return effectiveFetch(input, {
...init,
headers: headersWithUserAgent
});
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
const body = prepareBodyString(effectiveBody);
const credentials = await getCredentials();
const signingResult = await new AwsV4Signer({
url,
method: "POST",
headers: Object.entries(headersWithUserAgent).filter(([, value]) => isAscii(value)),
body,
region: credentials.region,
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken,
service
}).sign();
const signedHeaders = normalizeHeaders(signingResult.headers);
const combinedHeaders = combineHeaders(headersWithUserAgent, signedHeaders);
return effectiveFetch(input, {
...init,
body,
headers: combinedHeaders
});
};
}
function prepareBodyString(body) {
if (typeof body === "string") return body;
else if (body instanceof Uint8Array) return new TextDecoder().decode(body);
else if (body instanceof ArrayBuffer) return new TextDecoder().decode(new Uint8Array(body));
else return JSON.stringify(body);
}
function isAscii(value) {
return !/[\u0080-\uffff]/.test(value);
}
/**
* Creates a fetch function that applies Bearer token authentication.
*
* @param apiKey - The API key to use for Bearer token authentication.
* @param fetch - Optional original fetch implementation to wrap. Defaults to global fetch.
* @returns A FetchFunction that adds Authorization header with Bearer token to requests.
*/
function createApiKeyFetchFunction(apiKey, fetch) {
return async (input, init) => {
const effectiveFetch = fetch ?? globalThis.fetch;
const originalHeaders = normalizeHeaders(init?.headers);
const headersWithUserAgent = withUserAgentSuffix(originalHeaders, `ai-sdk-amazon-bedrock/${VERSION}`, getRuntimeEnvironmentUserAgent());
const resolvedApiKey = await resolve(apiKey);
if (typeof apiKey !== "string" && (typeof resolvedApiKey !== "string" || !resolvedApiKey.trim())) throw new LoadAPIKeyError({ message: "Amazon Bedrock API key must be a non-empty string." });
const finalHeaders = combineHeaders(headersWithUserAgent, { Authorization: `Bearer ${typeof apiKey === "string" ? resolvedApiKey : resolvedApiKey.trim()}` });
return effectiveFetch(input, {
...init,
headers: finalHeaders
});
};
}
//#endregion
//#region src/reranking/amazon-bedrock-reranking-api.ts
const amazonBedrockRerankingResponseSchema = lazySchema(() => zodSchema(z.object({
results: z.array(z.object({
index: z.number(),
relevanceScore: z.number()
})),
nextToken: z.string().optional()
})));
//#endregion
//#region src/reranking/amazon-bedrock-reranking-model-options.ts
const amazonBedrockRerankingModelOptionsSchema = lazySchema(() => zodSchema(z.object({
/**
* If the total number of results was greater than could fit in a response, a token is returned in the nextToken field. You can enter that token in this field to return the next batch of results.
*/
nextToken: z.string().optional(),
/**
* Additional model request fields to pass to the model.
*/
additionalModelRequestFields: z.record(z.string(), z.any()).optional()
})));
//#endregion
//#region src/reranking/amazon-bedrock-reranking-model.ts
var AmazonBedrockRerankingModel = class {
constructor(modelId, config) {
this.modelId = modelId;
this.config = config;
this.specificationVersion = "v4";
this.provider = "amazon-bedrock";
}
async doRerank({ documents, headers, query, topN, abortSignal, providerOptions }) {
const amazonBedrockOptions = await parseProviderOptions({
provider: "amazonBedrock",
providerOptions,
schema: amazonBedrockRerankingModelOptionsSchema
}) ?? await parseProviderOptions({
provider: "bedrock",
providerOptions,
schema: amazonBedrockRerankingModelOptionsSchema
});
const { value: response, responseHeaders, rawValue } = await postJsonToApi({
url: `${this.config.baseUrl()}/rerank`,
headers: await resolve(combineHeaders(await resolve(this.config.headers), headers)),
body: {
nextToken: amazonBedrockOptions?.nextToken,
queries: [{
textQuery: { text: query },
type: "TEXT"
}],
rerankingConfiguration: {
bedrockRerankingConfiguration: {
modelConfiguration: {
modelArn: `arn:aws:bedrock:${this.config.region}::foundation-model/${this.modelId}`,
additionalModelRequestFields: amazonBedrockOptions?.additionalModelRequestFields
},
numberOfResults: topN
},
type: "BEDROCK_RERANKING_MODEL"
},
sources: documents.values.map((value) => ({
type: "INLINE",
inlineDocumentSource: documents.type === "text" ? {
type: "TEXT",
textDocument: { text: value }
} : {
type: "JSON",
jsonDocument: value
}
}))
},
failedResponseHandler: amazonBedrockFailedResponseHandler,
successfulResponseHandler: createJsonResponseHandler(amazonBedrockRerankingResponseSchema),
fetch: this.config.fetch,
abortSignal
});
return {
ranking: response.results,
response: {
headers: responseHeaders,
body: rawValue
}
};
}
};
//#endregion
//#region src/resolve-amazon-bedrock-base-url.ts
const AWS_PARTITION_DNS_SUFFIXES = [
{
regionPrefix: "cn-",
dnsSuffix: "amazonaws.com.cn"
},
{
regionPrefix: "us-iso-",
dnsSuffix: "c2s.ic.gov"
},
{
regionPrefix: "us-isob-",
dnsSuffix: "sc2s.sgov.gov"
},
{
regionPrefix: "eu-isoe-",
dnsSuffix: "cloud.adc-e.uk"
},
{
regionPrefix: "us-isof-",
dnsSuffix: "csp.hci.ic.gov"
},
{
regionPrefix: "eusc-",
dnsSuffix: "amazonaws.eu"
}
];
function resolveAmazonBedrockBaseURL({ baseURL, getRegion, service, serviceEndpointUrlEnvironmentVariableName }) {
const resolvedBaseURL = baseURL ?? loadOptionalSetting({
settingValue: void 0,
environmentVariableName: serviceEndpointUrlEnvironmentVariableName
}) ?? loadOptionalSetting({
settingValue: void 0,
environmentVariableName: "AWS_ENDPOINT_URL"
});
if (resolvedBaseURL != null) return withoutTrailingSlash(resolvedBaseURL) ?? resolvedBaseURL;
const region = getRegion();
if (!isValidHostnamePart(region)) throw new InvalidArgumentError({
argument: "region",
message: "Invalid AWS region. Expected a single DNS label (letters, digits, and hyphens). Use `baseURL` for custom endpoints."
});
const generatedBaseURL = `https://${service}.${region}.${AWS_PARTITION_DNS_SUFFIXES.find(({ regionPrefix }) => region.startsWith(regionPrefix))?.dnsSuffix ?? "amazonaws.com"}`;
return withoutTrailingSlash(generatedBaseURL) ?? generatedBaseURL;
}
//#endregion
//#region src/amazon-bedrock-provider.ts
/**
* Create an Amazon Bedrock provider instance.
*/
function createAmazonBedrock(options = {}) {
const rawApiKey = typeof options.apiKey === "string" || options.apiKey == null ? loadOptionalSetting({
settingValue: options.apiKey,
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
}) : options.apiKey;
const apiKey = typeof rawApiKey === "string" ? rawApiKey.trim() || void 0 : rawApiKey;
const fetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
const region = loadSetting({
settingValue: options.region,
settingName: "region",
environmentVariableName: "AWS_REGION",
description: "AWS region"
});
if (options.credentialProvider) try {
return {
...await options.credentialProvider(),
region
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`);
}
try {
return {
region,
accessKeyId: loadSetting({
settingValue: options.accessKeyId,
settingName: "accessKeyId",
environmentVariableName: "AWS_ACCESS_KEY_ID",
description: "AWS access key ID"
}),
secretAccessKey: loadSetting({
settingValue: options.secretAccessKey,
settingName: "secretAccessKey",
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
description: "AWS secret access key"
}),
sessionToken: options.accessKeyId != null && options.secretAccessKey != null ? options.sessionToken : loadOptionalSetting({
settingValue: options.sessionToken,
environmentVariableName: "AWS_SESSION_TOKEN"
})
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) throw new Error(`AWS SigV4 authentication requires AWS credentials. Please provide either:
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
2. Provide accessKeyId and secretAccessKey in options
3. Use a credentialProvider function
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
Original error: ${errorMessage}`);
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) throw new Error(`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
Original error: ${errorMessage}`);
throw error;
}
}, options.fetch);
const getHeaders = () => {
const baseHeaders = options.headers ?? {};
return withUserAgentSuffix(baseHeaders, `ai-sdk-amazon-bedrock/${VERSION}`);
};
const getAmazonBedrockRuntimeBaseUrl = () => resolveAmazonBedrockBaseURL({
baseURL: options.baseURL,
getRegion: () => loadSetting({
settingValue: options.region,
settingName: "region",
environmentVariableName: "AWS_REGION",
description: "AWS region"
}),
service: "bedrock-runtime",
serviceEndpointUrlEnvironmentVariableName: "AWS_ENDPOINT_URL_BEDROCK_RUNTIME"
});
const getAmazonBedrockAgentRuntimeBaseUrl = () => resolveAmazonBedrockBaseURL({
baseURL: options.baseURL,
getRegion: () => loadSetting({
settingValue: options.region,
settingName: "region",
environmentVariableName: "AWS_REGION",
description: "AWS region"
}),
service: "bedrock-agent-runtime",
serviceEndpointUrlEnvironmentVariableName: "AWS_ENDPOINT_URL_BEDROCK_AGENT_RUNTIME"
});
const createChatModel = (modelId, settings = {}) => new AmazonBedrockChatLanguageModel(modelId, {
baseUrl: getAmazonBedrockRuntimeBaseUrl,
headers: getHeaders,
fetch: fetchFunction,
generateId,
modelFamily: settings.modelFamily
});
const provider = function(modelId, settings) {
if (new.target) throw new Error("The Amazon Bedrock model function cannot be called with the new keyword.");
return createChatModel(modelId, settings);
};
const createEmbeddingModel = (modelId, settings = {}) => new AmazonBedrockEmbeddingModel(modelId, {
baseUrl: getAmazonBedrockRuntimeBaseUrl,
headers: getHeaders,
fetch: fetchFunction,
modelFamily: settings.modelFamily
});
const createImageModel = (modelId) => new AmazonBedrockImageModel(modelId, {
baseUrl: getAmazonBedrockRuntimeBaseUrl,
headers: getHeaders,
fetch: fetchFunction
});
const createRerankingModel = (modelId) => new AmazonBedrockRerankingModel(modelId, {
baseUrl: getAmazonBedrockAgentRuntimeBaseUrl,
region: loadSetting({
settingValue: options.region,
settingName: "region",
environmentVariableName: "AWS_REGION",
description: "AWS region"
}),
headers: getHeaders,
fetch: fetchFunction
});
provider.specificationVersion = "v4";
provider.languageModel = createChatModel;
provider.embedding = createEmbeddingModel;
provider.embeddingModel = createEmbeddingModel;
provider.textEmbedding = createEmbeddingModel;
provider.textEmbeddingModel = createEmbeddingModel;
provider.image = createImageModel;
provider.imageModel = createImageModel;
provider.reranking = createRerankingModel;
provider.rerankingModel = createRerankingModel;
provider.tools = anthropicTools;
return provider;
}
/**
* Default Amazon Bedrock provider instance.
*/
const amazonBedrock = createAmazonBedrock();
//#endregion
export { VERSION, amazonBedrock, amazonBedrock as bedrock, createAmazonBedrock };
//# sourceMappingURL=index.js.map