fix(vertex-ai): reuse anthropic messages config instances
Some checks failed
Unit Tests: Security / security (push) Has been cancelled
Unit Tests: Proxy DB Operations / assert-shard-coverage (push) Has been cancelled
Unit Tests: Proxy DB Operations / proxy-utils (push) Has been cancelled
Unit Tests: Proxy DB Operations / auth-checks (push) Has been cancelled
Unit Tests: Proxy DB Operations / budgets (push) Has been cancelled
Unit Tests: Proxy DB Operations / custom-logging (push) Has been cancelled
Unit Tests: Proxy DB Operations / db-and-spend (push) Has been cancelled
Unit Tests: Proxy DB Operations / endpoints-and-responses (push) Has been cancelled
Unit Tests: Proxy DB Operations / guardrails-hooks (push) Has been cancelled
Unit Tests: Proxy DB Operations / jwt-and-keys (push) Has been cancelled
Unit Tests: Proxy DB Operations / key-generation (push) Has been cancelled
Unit Tests: Proxy DB Operations / logging-misc (push) Has been cancelled
Unit Tests: Proxy DB Operations / proxy-runtime (push) Has been cancelled
Unit Tests: Proxy DB Operations / proxy-server-core (push) Has been cancelled
Unit Tests: Proxy DB Operations / schema-migration (push) Has been cancelled

Cache provider config lookups for Vertex Anthropic messages so repeated requests reuse the same config object and preserve credential cache state. Add a regression test to catch any future loss of config reuse.

Made-with: Cursor
This commit is contained in:
Sameer Kankute 2026-04-20 16:24:31 +05:30
parent 8e652d129d
commit 361c486615
No known key found for this signature in database
2 changed files with 39 additions and 2 deletions

View file

@ -8410,6 +8410,17 @@ class ProviderConfigManager:
model: str,
provider: LlmProviders,
) -> Optional[BaseAnthropicMessagesConfig]:
return ProviderConfigManager._get_provider_anthropic_messages_config_cached(
model=model, provider=provider
)
@staticmethod
@lru_cache(maxsize=DEFAULT_MAX_LRU_CACHE_SIZE)
def _get_provider_anthropic_messages_config_cached(
model: str,
provider: LlmProviders,
) -> Optional[BaseAnthropicMessagesConfig]:
model_lower = model.lower()
if litellm.LlmProviders.ANTHROPIC == provider:
return litellm.AnthropicMessagesConfig()
# The 'BEDROCK' provider corresponds to Amazon's implementation of Anthropic Claude v3.
@ -8419,14 +8430,14 @@ class ProviderConfigManager:
return BedrockModelInfo.get_bedrock_provider_config_for_messages_api(model)
elif litellm.LlmProviders.VERTEX_AI == provider:
if "claude" in model.lower():
if "claude" in model_lower:
from litellm.llms.vertex_ai.vertex_ai_partner_models.anthropic.experimental_pass_through.transformation import (
VertexAIPartnerModelsAnthropicMessagesConfig,
)
return VertexAIPartnerModelsAnthropicMessagesConfig()
elif litellm.LlmProviders.AZURE_AI == provider:
if "claude" in model.lower():
if "claude" in model_lower:
from litellm.llms.azure_ai.anthropic.messages_transformation import (
AzureAnthropicMessagesConfig,
)

View file

@ -311,3 +311,29 @@ def test_transform_anthropic_messages_request_removes_scope_from_cache_control()
# scope removed from message content
assert "scope" not in result["messages"][0]["content"][0]["cache_control"]
assert result["messages"][0]["content"][0]["cache_control"]["type"] == "ephemeral"
def test_provider_config_manager_reuses_vertex_anthropic_messages_config_instance():
"""
Regression test: repeated provider config lookups for the same Vertex Claude model
should return the same config instance (which preserves auth cache state).
"""
import litellm
from litellm.utils import ProviderConfigManager
ProviderConfigManager._get_provider_anthropic_messages_config_cached.cache_clear()
try:
first_config = ProviderConfigManager.get_provider_anthropic_messages_config(
model="claude-opus-4-6",
provider=litellm.LlmProviders.VERTEX_AI,
)
second_config = ProviderConfigManager.get_provider_anthropic_messages_config(
model="claude-opus-4-6",
provider=litellm.LlmProviders.VERTEX_AI,
)
assert isinstance(first_config, VertexAIPartnerModelsAnthropicMessagesConfig)
assert isinstance(second_config, VertexAIPartnerModelsAnthropicMessagesConfig)
assert first_config is second_config
finally:
ProviderConfigManager._get_provider_anthropic_messages_config_cached.cache_clear()