feat(models): Enable multi-provider support for Claude and LiteLLM

Merges: https://github.com/google/adk-python/pull/2810

Co-authored-by: Xuan Yang <xygoogle@google.com>
PiperOrigin-RevId: 836706608
This commit is contained in:
Virtuoso633
2025-11-25 09:46:44 -08:00
committed by Copybara-Service
co-authored by Xuan Yang
parent e6be5bc9c6
commit d29261a3dc
5 changed files with 156 additions and 7 deletions
+55 -3
View File
@@ -15,7 +15,7 @@
from google.adk import models
from google.adk.models.anthropic_llm import Claude
from google.adk.models.google_llm import Gemini
from google.adk.models.registry import LLMRegistry
from google.adk.models.lite_llm import LiteLlm
import pytest
@@ -34,6 +34,7 @@ import pytest
],
)
def test_match_gemini_family(model_name):
"""Test that Gemini models are resolved correctly."""
assert models.LLMRegistry.resolve(model_name) is Gemini
@@ -51,12 +52,63 @@ def test_match_gemini_family(model_name):
],
)
def test_match_claude_family(model_name):
LLMRegistry.register(Claude)
"""Test that Claude models are resolved correctly."""
assert models.LLMRegistry.resolve(model_name) is Claude
@pytest.mark.parametrize(
'model_name',
[
'openai/gpt-4o',
'openai/gpt-4o-mini',
'groq/llama3-70b-8192',
'groq/mixtral-8x7b-32768',
'anthropic/claude-3-opus-20240229',
'anthropic/claude-3-5-sonnet-20241022',
],
)
def test_match_litellm_family(model_name):
"""Test that LiteLLM models are resolved correctly."""
assert models.LLMRegistry.resolve(model_name) is LiteLlm
def test_non_exist_model():
with pytest.raises(ValueError) as e_info:
models.LLMRegistry.resolve('non-exist-model')
assert 'Model non-exist-model not found.' in str(e_info.value)
def test_helpful_error_for_claude_without_extensions():
"""Test that missing Claude models show helpful install instructions.
Note: This test may pass even when anthropic IS installed, because it
only checks the error message format when a model is not found.
"""
# Use a non-existent Claude model variant to trigger error
with pytest.raises(ValueError) as e_info:
models.LLMRegistry.resolve('claude-nonexistent-model-xyz')
error_msg = str(e_info.value)
# The error should mention anthropic package and installation instructions
# These checks work whether or not anthropic is actually installed
assert 'Model claude-nonexistent-model-xyz not found' in error_msg
assert 'anthropic package' in error_msg
assert 'pip install' in error_msg
def test_helpful_error_for_litellm_without_extensions():
"""Test that missing LiteLLM models show helpful install instructions.
Note: This test may pass even when litellm IS installed, because it
only checks the error message format when a model is not found.
"""
# Use a non-existent provider to trigger error
with pytest.raises(ValueError) as e_info:
models.LLMRegistry.resolve('unknown-provider/gpt-4o')
error_msg = str(e_info.value)
# The error should mention litellm package for provider-style models
assert 'Model unknown-provider/gpt-4o not found' in error_msg
assert 'litellm package' in error_msg
assert 'pip install' in error_msg
assert 'Provider-style models' in error_msg