Skip to content

Commit 7ed833c

Browse files
authored
Fix: AWS Bedrock 1M context - Move anthropic_beta to additionalModelRequestFields (#7056)
1 parent 962df86 commit 7ed833c

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/api/providers/__tests__/bedrock.spec.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,11 @@ describe("AwsBedrockHandler", () => {
635635
expect(mockConverseStreamCommand).toHaveBeenCalled()
636636
const commandArg = mockConverseStreamCommand.mock.calls[0][0] as any
637637

638-
// Should include anthropic_beta parameter but NOT anthropic_version (only for thinking)
639-
expect(commandArg.anthropic_beta).toEqual(["context-1m-2025-08-07"])
640-
expect(commandArg.anthropic_version).toBeUndefined()
638+
// Should include anthropic_beta in additionalModelRequestFields
639+
expect(commandArg.additionalModelRequestFields).toBeDefined()
640+
expect(commandArg.additionalModelRequestFields.anthropic_beta).toEqual(["context-1m-2025-08-07"])
641+
// Should not include anthropic_version since thinking is not enabled
642+
expect(commandArg.additionalModelRequestFields.anthropic_version).toBeUndefined()
641643
})
642644

643645
it("should not include anthropic_beta parameter when 1M context is disabled", async () => {
@@ -663,8 +665,8 @@ describe("AwsBedrockHandler", () => {
663665
expect(mockConverseStreamCommand).toHaveBeenCalled()
664666
const commandArg = mockConverseStreamCommand.mock.calls[0][0] as any
665667

666-
// Should not include anthropic_beta parameter
667-
expect(commandArg.anthropic_beta).toBeUndefined()
668+
// Should not include anthropic_beta in additionalModelRequestFields
669+
expect(commandArg.additionalModelRequestFields).toBeUndefined()
668670
})
669671

670672
it("should not include anthropic_beta parameter for non-Claude Sonnet 4 models", async () => {
@@ -690,8 +692,8 @@ describe("AwsBedrockHandler", () => {
690692
expect(mockConverseStreamCommand).toHaveBeenCalled()
691693
const commandArg = mockConverseStreamCommand.mock.calls[0][0] as any
692694

693-
// Should not include anthropic_beta parameter for non-Sonnet 4 models
694-
expect(commandArg.anthropic_beta).toBeUndefined()
695+
// Should not include anthropic_beta for non-Sonnet 4 models
696+
expect(commandArg.additionalModelRequestFields).toBeUndefined()
695697
})
696698

697699
it("should enable 1M context window with cross-region inference for Claude Sonnet 4", () => {
@@ -738,9 +740,11 @@ describe("AwsBedrockHandler", () => {
738740
mockConverseStreamCommand.mock.calls.length - 1
739741
][0] as any
740742

741-
// Should include anthropic_beta parameter but NOT anthropic_version (only for thinking)
742-
expect(commandArg.anthropic_beta).toEqual(["context-1m-2025-08-07"])
743-
expect(commandArg.anthropic_version).toBeUndefined()
743+
// Should include anthropic_beta in additionalModelRequestFields
744+
expect(commandArg.additionalModelRequestFields).toBeDefined()
745+
expect(commandArg.additionalModelRequestFields.anthropic_beta).toEqual(["context-1m-2025-08-07"])
746+
// Should not include anthropic_version since thinking is not enabled
747+
expect(commandArg.additionalModelRequestFields.anthropic_version).toBeUndefined()
744748
// Model ID should have cross-region prefix
745749
expect(commandArg.modelId).toBe(`us.${BEDROCK_CLAUDE_SONNET_4_MODEL_ID}`)
746750
})

src/api/providers/bedrock.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ interface BedrockInferenceConfig {
4747
topP?: number
4848
}
4949

50-
// Define interface for Bedrock thinking configuration
51-
interface BedrockThinkingConfig {
52-
thinking: {
50+
// Define interface for Bedrock additional model request fields
51+
// This includes thinking configuration, 1M context beta, and other model-specific parameters
52+
interface BedrockAdditionalModelFields {
53+
thinking?: {
5354
type: "enabled"
5455
budget_tokens: number
5556
}
57+
anthropic_beta?: string[]
5658
[key: string]: any // Add index signature to be compatible with DocumentType
5759
}
5860

@@ -63,8 +65,7 @@ interface BedrockPayload {
6365
system?: SystemContentBlock[]
6466
inferenceConfig: BedrockInferenceConfig
6567
anthropic_version?: string
66-
anthropic_beta?: string[]
67-
additionalModelRequestFields?: BedrockThinkingConfig
68+
additionalModelRequestFields?: BedrockAdditionalModelFields
6869
}
6970

7071
// Define specific types for content block events to avoid 'as any' usage
@@ -341,7 +342,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
341342
conversationId,
342343
)
343344

344-
let additionalModelRequestFields: BedrockThinkingConfig | undefined
345+
let additionalModelRequestFields: BedrockAdditionalModelFields | undefined
345346
let thinkingEnabled = false
346347

347348
// Determine if thinking should be enabled
@@ -382,16 +383,22 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
382383
const baseModelId = this.parseBaseModelId(modelConfig.id)
383384
const is1MContextEnabled = baseModelId === BEDROCK_CLAUDE_SONNET_4_MODEL_ID && this.options.awsBedrock1MContext
384385

386+
// Add anthropic_beta for 1M context to additionalModelRequestFields
387+
if (is1MContextEnabled) {
388+
if (!additionalModelRequestFields) {
389+
additionalModelRequestFields = {} as BedrockAdditionalModelFields
390+
}
391+
additionalModelRequestFields.anthropic_beta = ["context-1m-2025-08-07"]
392+
}
393+
385394
const payload: BedrockPayload = {
386395
modelId: modelConfig.id,
387396
messages: formatted.messages,
388397
system: formatted.system,
389398
inferenceConfig,
390399
...(additionalModelRequestFields && { additionalModelRequestFields }),
391-
// Add anthropic_version when using thinking features
400+
// Add anthropic_version at top level when using thinking features
392401
...(thinkingEnabled && { anthropic_version: "bedrock-2023-05-31" }),
393-
// Add anthropic_beta when 1M context is enabled
394-
...(is1MContextEnabled && { anthropic_beta: ["context-1m-2025-08-07"] }),
395402
}
396403

397404
// Create AbortController with 10 minute timeout

0 commit comments

Comments
 (0)