fix(vertex-ai): reuse anthropic messages config instances - #28996
fix(vertex-ai): reuse anthropic messages config instances#28996milan-berri wants to merge 1 commit into
Conversation
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
Greptile SummaryThis PR backports the
Confidence Score: 4/5The change is a narrow, well-scoped caching addition that matches existing patterns in the codebase; the main outstanding item is running the new regression test to confirm the fix behaves as expected. The implementation is straightforward and mirrors patterns already used in the project. The only notable gap is that the PR's own regression test is explicitly marked as not yet run, so there is no confirmed signal that the backport behaves identically to the original main-branch fix. Additionally, the cache is keyed on The
|
| Filename | Overview |
|---|---|
| litellm/utils.py | Splits get_provider_anthropic_messages_config into a thin public wrapper and a new @staticmethod @lru_cache-decorated private method, ensuring Vertex (and all other) provider config instances are reused per (model, provider) pair. No breaking changes; the lru_cache key includes model even for providers that don't use it (ANTHROPIC, AZURE_AI), which wastes a few cache slots but is otherwise harmless. |
| tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py | Adds regression test asserting config-instance identity across repeated lookups; correctly isolates itself with cache_clear() in a try/finally, but the test is shown as not yet run in the PR checklist. |
Reviews (1): Last reviewed commit: "fix(vertex-ai): reuse anthropic messages..." | Re-trigger Greptile
| 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() |
There was a problem hiding this comment.
Regression test left unchecked in PR description
The PR's test plan checkbox for test_provider_config_manager_reuses_vertex_anthropic_messages_config_instance is explicitly unchecked (- [ ]). Per the project rule requiring evidence of fix resolution, the test should be confirmed passing before merge. Without confirmation, there's no assurance the lru_cache correctly preserves the config instance across multiple invocations in the actual test environment.
Rule Used: What: Ensure that any PR claiming to fix an issue ... (source)
| if litellm.LlmProviders.ANTHROPIC == provider: | ||
| return litellm.AnthropicMessagesConfig() |
There was a problem hiding this comment.
AnthropicMessagesConfig cached per model string unnecessarily
For provider=ANTHROPIC, the function returns litellm.AnthropicMessagesConfig() regardless of the model name. Because model is part of the lru_cache key, each distinct model string (e.g. "claude-3-5-sonnet", "claude-opus-4-6") gets its own AnthropicMessagesConfig instance in the cache, slowly filling the 64-entry LRU even though the config is model-agnostic. The same minor concern applies to AzureAnthropicMessagesConfig. This won't break anything but can evict useful Vertex AI entries from the cache under load.
Summary
f4dd727b45ontorelease/v1.83.7-stable.ProviderConfigManager.get_provider_anthropic_messages_configlookups via@lru_cacheso repeated Vertex Claude/v1/messagesrequests reuse the sameVertexAIPartnerModelsAnthropicMessagesConfiginstance and preserve credential cache state.Already merged to
mainvia #26099; this backports the fix to the v1.83.7 stable line.Test plan
release/v1.83.7-stablepytest tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py::test_provider_config_manager_reuses_vertex_anthropic_messages_config_instanceMade with Cursor