Skip to content

Commit 44e3379

Browse files
authored
Merge pull request earendil-works#5527 from AJM10565/fix/bedrock-arn-region-parsing
fix(amazon-bedrock): extract region from inference profile ARNs
2 parents 20b78ea + 8cef3c8 commit 44e3379

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

packages/ai/src/providers/amazon-bedrock.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ export const streamBedrock: StreamFunction<"bedrock-converse-stream", BedrockOpt
143143

144144
// in Node.js/Bun environment only
145145
if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) {
146-
// Region resolution: explicit option > env vars > SDK default chain.
147-
// When AWS_PROFILE is set, we leave region undefined so the SDK can
148-
// resovle it from aws profile configs. Otherwise fall back to us-east-1.
149-
if (configuredRegion) {
146+
// Region resolution: ARN-embedded > explicit option > env vars > SDK default chain.
147+
// When the model ID is an inference profile ARN, extract the region from it.
148+
// This avoids conflicts with AWS_REGION set for other services.
149+
const arnRegionMatch = model.id.match(/^arn:aws(?:-[a-z0-9-]+)?:bedrock:([a-z0-9-]+):/);
150+
if (arnRegionMatch) {
151+
config.region = arnRegionMatch[1];
152+
} else if (configuredRegion) {
150153
config.region = configuredRegion;
151154
} else if (endpointRegion && useExplicitEndpoint) {
152155
config.region = endpointRegion;

packages/ai/test/bedrock-endpoint-resolution.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,30 @@ describe("bedrock endpoint resolution", () => {
128128
expect(config.endpoint).toBe("https://bedrock-vpc.example.com");
129129
expect(config.region).toBe("us-west-2");
130130
});
131+
132+
it("extracts region from inference profile ARN regardless of AWS_REGION", async () => {
133+
process.env.AWS_REGION = "us-east-1";
134+
const baseModel = getModel("amazon-bedrock", "us.anthropic.claude-opus-4-8");
135+
const model: Model<"bedrock-converse-stream"> = {
136+
...baseModel,
137+
id: "arn:aws:bedrock:us-west-2:123456789012:application-inference-profile/abc123",
138+
};
139+
140+
const config = await captureClientConfig(model);
141+
142+
expect(config.region).toBe("us-west-2");
143+
});
144+
145+
it("extracts region from GovCloud inference profile ARN", async () => {
146+
process.env.AWS_REGION = "us-east-1";
147+
const baseModel = getModel("amazon-bedrock", "us.anthropic.claude-opus-4-8");
148+
const model: Model<"bedrock-converse-stream"> = {
149+
...baseModel,
150+
id: "arn:aws-us-gov:bedrock:us-gov-west-1:123456789012:application-inference-profile/abc123",
151+
};
152+
153+
const config = await captureClientConfig(model);
154+
155+
expect(config.region).toBe("us-gov-west-1");
156+
});
131157
});

0 commit comments

Comments
 (0)