Skip to content

Commit e5ff698

Browse files
Issue 203: Fix model alias matching for provider prefixes (claude-3-7-sonnet support) (#206)
## Resolves issue #203 Fix model alias matching to properly handle provider prefixes, particularly for Bedrock models that can have varying prefixes (e.g., `anthropic.` or `us.anthropic.`). ## Changes - Updated alias matching logic to handle provider prefixes more flexibly - Fixed support for `claude-3-7-sonnet` model which uses the `us.anthropic.` prefix - Ran aliases.rake to add proper model aliases back for Claude-3-7-sonnet ## Problem The alias matching system was too strict with provider prefixes, causing models like `claude-3-7-sonnet` to fail matching when the Bedrock version used a different prefix (`us.anthropic.`) compared to other Claude models (`anthropic.`). ## Solution Modified the alias matching to be more flexible with provider prefixes while maintaining the correct model identification. This ensures compatibility with both existing and new model versions regardless of their provider prefix format. ## Testing - Verified that `claude-3-7-sonnet` model works correctly with both Anthropic API and Bedrock - Added spec for handling different provider prefixes correctly for bedrock models - Ensured existing model aliases continue to work as expected
1 parent 213e601 commit e5ff698

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

lib/ruby_llm/aliases.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
"anthropic": "claude-3-5-sonnet-20241022",
3030
"bedrock": "anthropic.claude-3-5-sonnet-20240620-v1:0:200k"
3131
},
32+
"claude-3-7-sonnet": {
33+
"anthropic": "claude-3-7-sonnet-latest",
34+
"bedrock": "us.anthropic.claude-3-7-sonnet-20250219-v1:0"
35+
},
36+
"claude-3-7-sonnet-20250219": {
37+
"anthropic": "claude-3-7-sonnet-20250219",
38+
"bedrock": "us.anthropic.claude-3-7-sonnet-20250219-v1:0"
39+
},
3240
"claude-3-haiku": {
3341
"bedrock": "anthropic.claude-3-haiku-20240307-v1:0",
3442
"openrouter": "anthropic/claude-3-haiku"

lib/tasks/aliases.rake

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,23 @@ namespace :aliases do # rubocop:disable Metrics/BlockLength
123123

124124
def find_best_bedrock_model(anthropic_model, bedrock_models) # rubocop:disable Metrics/PerceivedComplexity,Rake/MethodDefinitionInTask
125125
# Special mapping for Claude 2.x models
126-
bedrock_pattern = case anthropic_model
127-
when 'claude-2.0', 'claude-2'
128-
'anthropic.claude-v2'
129-
when 'claude-2.1'
130-
'anthropic.claude-v2:1'
131-
when 'claude-instant-v1', 'claude-instant'
132-
'anthropic.claude-instant'
133-
else
134-
# For Claude 3+ models, extract base name
135-
base_name = extract_base_name(anthropic_model)
136-
"anthropic.#{base_name}"
137-
end
138-
139-
# Find all matching Bedrock models
126+
base_pattern = case anthropic_model
127+
when 'claude-2.0', 'claude-2'
128+
'claude-v2'
129+
when 'claude-2.1'
130+
'claude-v2:1'
131+
when 'claude-instant-v1', 'claude-instant'
132+
'claude-instant'
133+
else
134+
# For Claude 3+ models, extract base name
135+
extract_base_name(anthropic_model)
136+
end
137+
138+
# Find all matching Bedrock models by stripping provider prefix and comparing base name
140139
matching_models = bedrock_models.select do |bedrock_model|
141-
bedrock_model.start_with?(bedrock_pattern)
140+
# Strip any provider prefix (anthropic. or us.anthropic.)
141+
model_without_prefix = bedrock_model.sub(/^(?:us\.)?anthropic\./, '')
142+
model_without_prefix.start_with?(base_pattern)
142143
end
143144

144145
return nil if matching_models.empty?

spec/ruby_llm/chat_model_aliases_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@
2424
expect(chat.model.id).to eq('anthropic.claude-3-5-haiku-20241022-v1:0')
2525
expect(chat.model.provider).to eq('bedrock')
2626
end
27+
28+
it 'handles different provider prefixes correctly' do # rubocop:disable RSpec/MultipleExpectations
29+
# Test that we can match models regardless of their provider prefix
30+
chat = RubyLLM.chat(model: 'claude-3-7-sonnet', provider: :bedrock)
31+
expect(chat.model.id).to eq('us.anthropic.claude-3-7-sonnet-20250219-v1:0')
32+
expect(chat.model.provider).to eq('bedrock')
33+
end
2734
end

0 commit comments

Comments
 (0)