From ed9a9b8096ca00a2ca80ee8e062e52bc2f24f4c0 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Fri, 24 Oct 2025 14:12:29 -0300 Subject: [PATCH] fix(bedrock): prefix model id with region automatically --- providers/anthropic/anthropic.go | 2 ++ providers/anthropic/bedrock.go | 13 +++++++++++++ providertests/bedrock_test.go | 8 ++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index a40ec96712dba7a135f70542d5098c30286dc3a4..42f8d72f1039cabf9c85341120655b4a24125fa3 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -157,6 +157,8 @@ func (a *provider) LanguageModel(ctx context.Context, modelID string) (fantasy.L ) } if a.options.useBedrock { + modelID = bedrockPrefixModelWithRegion(modelID) + if a.options.skipAuth || a.options.apiKey != "" { clientOptions = append( clientOptions, diff --git a/providers/anthropic/bedrock.go b/providers/anthropic/bedrock.go index ae9b5a30976ba46d328a99874da948b55559e41d..8d9b94959210e57fde8fa594a67bfc8db8d27415 100644 --- a/providers/anthropic/bedrock.go +++ b/providers/anthropic/bedrock.go @@ -3,6 +3,7 @@ package anthropic import ( "cmp" "os" + "strings" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/smithy-go/auth/bearer" @@ -14,3 +15,15 @@ func bedrockBasicAuthConfig(apiKey string) aws.Config { BearerAuthTokenProvider: bearer.StaticTokenProvider{Token: bearer.Token{Value: apiKey}}, } } + +func bedrockPrefixModelWithRegion(modelID string) string { + region := os.Getenv("AWS_REGION") + if len(region) < 2 { + region = "us-east-1" + } + prefix := region[:2] + "." + if strings.HasPrefix(modelID, prefix) { + return modelID + } + return prefix + modelID +} diff --git a/providertests/bedrock_test.go b/providertests/bedrock_test.go index 7eab1eb8407040623303f8480d98065c5cb44b5c..e377f3568d393a30b53d13604a8dcf1c9970d322 100644 --- a/providertests/bedrock_test.go +++ b/providertests/bedrock_test.go @@ -30,7 +30,7 @@ func builderBedrockClaude3Sonnet(t *testing.T, r *recorder.Recorder) (fantasy.La if err != nil { return nil, err } - return provider.LanguageModel(t.Context(), "us.anthropic.claude-3-sonnet-20240229-v1:0") + return provider.LanguageModel(t.Context(), "anthropic.claude-3-sonnet-20240229-v1:0") } func builderBedrockClaude3Opus(t *testing.T, r *recorder.Recorder) (fantasy.LanguageModel, error) { @@ -41,7 +41,7 @@ func builderBedrockClaude3Opus(t *testing.T, r *recorder.Recorder) (fantasy.Lang if err != nil { return nil, err } - return provider.LanguageModel(t.Context(), "us.anthropic.claude-3-opus-20240229-v1:0") + return provider.LanguageModel(t.Context(), "anthropic.claude-3-opus-20240229-v1:0") } func builderBedrockClaude3Haiku(t *testing.T, r *recorder.Recorder) (fantasy.LanguageModel, error) { @@ -52,7 +52,7 @@ func builderBedrockClaude3Haiku(t *testing.T, r *recorder.Recorder) (fantasy.Lan if err != nil { return nil, err } - return provider.LanguageModel(t.Context(), "us.anthropic.claude-3-haiku-20240307-v1:0") + return provider.LanguageModel(t.Context(), "anthropic.claude-3-haiku-20240307-v1:0") } func buildersBedrockBasicAuth(t *testing.T, r *recorder.Recorder) (fantasy.LanguageModel, error) { @@ -64,5 +64,5 @@ func buildersBedrockBasicAuth(t *testing.T, r *recorder.Recorder) (fantasy.Langu if err != nil { return nil, err } - return provider.LanguageModel(t.Context(), "us.anthropic.claude-3-sonnet-20240229-v1:0") + return provider.LanguageModel(t.Context(), "anthropic.claude-3-sonnet-20240229-v1:0") }