litellm/tests/unit/llms/anthropic/chat
Abdellatif Anaflous 74fd46a59f
fix(vertex_ai): resolve correct default max_tokens for versioned claude ids (#40376)
* fix(vertex_ai): resolve correct default max_tokens for versioned claude ids

two defects stacked on the vertex claude path so a request without
max_tokens was silently capped at 4096 output tokens

the fill in transform_request called AnthropicConfig.get_config directly
so get_max_tokens_for_model on VertexAIAnthropicConfig never ran, and the
bare versioned id like claude-haiku-4-5@20251001 is not a cost map key,
so the fill fell back to the 4096 default

now transform_request calls self.get_config and VertexAIAnthropicConfig
resolves the vertex_ai prefixed key first, then falls back to the base
lookup

also the two vertex_ai claude haiku 4 5 map entries said 8192 max output
while every sibling claude-haiku-4-5 key says 64000 and google documents
64000, the reporter verified 64000 live on vertex with a 200 and 15290
completion tokens, both entries and the runtime backup map now say 64000

Fixes #40363

* chore(anthropic): drop redundant mutable-ok suppressions

the type discipline gate flags these two comments as LIT013, both suppress nothing because neither line has an LIT001 violation. no behavior change

* fix(vertex_ai): look up the vertex max tokens without the partially unknown cost map

the membership check on litellm.model_cost is a reportUnknownMemberType the basedpyright delta gate counts, and it adds exactly one over the merge-base. get_max_tokens already raises for a model that is not mapped, so catch that and fall back to the parent, same as the parent does for its own lookup. the focused vertex anthropic tests still pass

* fix(vertex_ai): guard the model cost map lookup with a pyright ignore

the try/except form added a blind except that the strict ruff gate counts as BLE001, one over the merge-base. keep the membership check and suppress only the reportUnknownMemberType on the dynamically typed registry, the same pattern the complexity router already uses. ruff strict is identical to the base, ruff format and ruff check clean, focused vertex anthropic tests 42 passed

* fix(vertex_ai): read the unversioned Claude row before the Vertex row for a missing max_tokens

A Vertex @version id now reads the cost-map row of its id without the version before the vertex_ai row, so claude-sonnet-4-6@default keeps the 128000 default that Vertex accepts while versioned Haiku, Opus, and Sonnet 4.5 ids still get 64000.

* fix(anthropic): keep an explicit DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS ahead of the unversioned and provider rows

An operator who set DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS, for example to 4096 as the v1.109.0 notes
suggest, got that value for every Claude id with no own cost-map row. The new unversioned and
provider-row steps ranked above it, so vertex_ai/claude-haiku-4-5@20251001 went from 4096 to 64000
and claude-sonnet-4-6@default from 4096 to 128000 for them. An explicit value now comes right after
the model's own row, so only deployments on the built-in fallback change

* fix(anthropic): keep DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS as the last fallback after the unversioned and provider rows

Reverts the previous commit. With DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS set to any value, versioned
Vertex Claude ids ignored their model's row again: at 4096 a Haiku 4.5 reply still stopped at 4096
tokens, the symptom the linked issue reports, and at 128000 Vertex still answered 400. The env var is
documented as the default for models missing from the cost map, and a versioned id whose model has a
row is not missing, so it now gets the same cap as its unversioned id

* fix(anthropic): read the unversioned max_tokens row directly and test the lookup on other routes

The unversioned lookup went through get_max_tokens, which prints the provider banner a second time
when the id has no row, so SDK calls on ids like claude-3-5-haiku@20241022 printed it twice. Reading
the row directly gives the same max_tokens for every cost-map Claude id on every route. New tests
drive the provider and unversioned rows on the Anthropic, Azure AI and Claude Platform routes

* ci: restart the required checks after their held runs failed at startup

---------

Co-authored-by: hktitof <hktitof@users.noreply.github.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-14 17:36:51 -05:00
..
guardrail_translation feat(router): add the Switchyard stage router as an auto router strategy (#50407) 2026-11-06 12:07:57 -08:00
__init__.py
conftest.py
test_anthropic_chat_handler.py fix(anthropic): preserve streamed tool argument fragments verbatim (#50470) 2026-11-07 01:44:48 -08:00
test_anthropic_chat_transformation.py fix(vertex_ai): resolve correct default max_tokens for versioned claude ids (#40376) 2026-11-14 17:36:51 -05:00
test_code_interpreter_results_extraction.py feat(anthropic): workload identity federation and pluggable identity sources (#44448) 2026-10-03 17:08:30 -07:00
test_transformation.py test: move whole-unit legacy test files into tests/unit and delete dead skips (#44807) 2026-10-07 05:02:28 +00:00