fix(vertex): respect vertex_count_tokens_location for Claude count_tokens#23907
Conversation
…kens The count_tokens handler unconditionally overrode vertex_location to us-central1 for Claude models, ignoring the user-configured vertex_count_tokens_location parameter. Also, us-central1 is no longer a supported region — Google now supports us-east5, europe-west1, and asia-southeast1. Now vertex_count_tokens_location takes precedence, vertex_location is used as fallback, and us-east5 is the default only when neither is set. Fixes BerriAI#23872
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes a bug in the Vertex AI partner model Key changes:
Potential issue: The original condition ( Confidence Score: 3/5
|
| Filename | Overview |
|---|---|
| litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py | Fixes the Claude count-tokens location override bug; correctly gives precedence to vertex_count_tokens_location and defaults to us-east5. However, the change also removes the generic location fallback for non-Claude partner models (Mistral, Meta, etc.) when no location is set, which can cause silent broken URLs. |
| tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/test_count_tokens_location.py | Well-structured mock-only tests covering the three main Claude location-resolution scenarios. No real network calls are made. Missing coverage for non-Claude models under the new logic. |
| tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/init.py | Empty init.py to make the directory a Python package — no issues. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[handle_count_tokens_request] --> B{vertex_count_tokens_location set?}
B -- Yes --> C[Use vertex_count_tokens_location]
B -- No --> D{get_vertex_ai_location returns value?}
D -- Yes --> E[Use vertex_location]
D -- No --> F{"claude" in model.lower?}
F -- Yes --> G[Default: us-east5]
F -- No --> H[vertex_location = None ⚠️]
C --> I[_build_count_tokens_endpoint]
E --> I
G --> I
H --> I
I --> J[HTTP POST to Vertex AI count-tokens API]
Last reviewed commit: 8f015e2
|
@greptile Count-tokens on Vertex AI only works for Claude (and Gemini, which uses a different endpoint) Vertex docs: https://docs.cloud.google.com/vertex-ai/generative-ai/docs/partner-models/claude/count-tokens . Mistral and Llama don't have a count-tokens API on Vertex AI. |
|
Thanks for the clarification! That's helpful context — so the PR's focus on Claude is correct since Mistral and Llama don't support count_tokens on Vertex AI at all. The fix ensures that when Claude's count_tokens is called, it respects the For Gemini using a different endpoint, that shouldn't be affected by this change since it presumably has its own handling path. |
4947074
into
BerriAI:litellm_oss_staging_03_17_2026
Relevant issues
Fixes #23872
Pre-Submission checklist
tests/test_litellm/directorymake test-unit@greptileaiType
🐛 Bug Fix
Changes
The
count_tokenshandler for Vertex AI partner models unconditionally overrodevertex_locationtous-central1for Claude models (if not vertex_location or "claude" in model.lower()), ignoring the documentedvertex_count_tokens_locationparameter.Additionally,
us-central1is no longer a supported region for Claude count_tokens — Google now supportsus-east5,europe-west1, andasia-southeast1(docs).Fix:
vertex_count_tokens_locationtakes precedence,vertex_locationis used as fallback, andus-east5is the default only when neither is set.