litellm/tests/unit/llms/anthropic
paveln83 73bbc8caab
fix(anthropic): drop unsigned thinking blocks before /v1/messages reaches Claude (#51793)
* fix(anthropic): drop unsigned thinking blocks before /v1/messages reaches Claude

A thought another provider served (Gemini through the /v1/messages adapter) comes back from Claude Code with an empty signature. Anthropic, Bedrock, Vertex, Azure AI, Databricks and Bedrock Mantle Claude reject it, so the first attempt 400s and the retry turns thinking off. Drop unsigned and encrypted thinking before the first attempt for backends that check Anthropic signatures, and keep the encrypted-only strip for third-party Anthropic-compatible APIs that replay their own unsigned thinking.

* fix(anthropic): strip unsigned thinking only when the model is Claude

The strip keyed on provider anthropic, so Kimi and other models behind an
Anthropic-compatible api_base lost their own unsigned thinking. Every host
now strips only for a Claude model, which also covers Claude on GitHub
Copilot, and a Bedrock ARN still counts as Claude. Types the new tests.

* fix(anthropic): drop unsigned thinking only where the host checks Claude signatures

Anthropic-compatible hosts such as DeepSeek accept Gemini's unsigned thought and need
it passed back on tool turns, so a Claude model name alone no longer triggers the strip
under anthropic/. Python and Rust now also require an anthropic.com or Azure AI Foundry
host there, while fixed-host providers keep the name check.

Bedrock Converse drops the unsigned thought instead of sending it as reply text, and
count_tokens drops it for Claude targets so it accepts the history /v1/messages accepts.
The Bedrock ARN override is removed, so Bedrock uses the shared name check.

* fix(messages): drop unsigned thinking from Vertex AI and Bedrock count_tokens and test the Rust handler strip

* fix(anthropic): keep unsigned thinking for providers and hosts that do not check Claude signatures

DeepSeek takes deepseek/claude-* names and Anthropic-compatible hosts take
anthropic/claude-* names, and both want the thought back. The strip and
count_tokens now share one gate, a Claude model on a provider or host that
checks Claude signatures. The Bedrock count judges the deployment model,
and a message that is not an object goes to the provider untouched

* fix(bedrock): drop unsigned thinking before Claude Platform on AWS whatever host it names

Claude Platform on AWS inherited the anthropic host check, so an explicit api_base or an ANTHROPIC_BASE_URL that points elsewhere kept another model's unsigned thought and Claude rejected it. It now uses the Bedrock rule, which checks only the model name

Also adds a count_tokens handler test that posts the same thinking history Vertex AI /v1/messages sends

* fix(messages): drop unsigned thinking on azure_ai's rust messages route

Main now serves azure_ai /v1/messages on Rust only, so the Python strip no
longer runs there. The azure_ai Rust config drops thinking blocks with an
empty, missing or non-string signature for a Claude deployment, in parsed
and unparsed messages, before the request goes to Azure AI Foundry.

* fix(messages): drop unsigned thinking on bedrock invoke's rust messages route

* test(messages): use rstest for the new bedrock invoke thinking tests

* fix(messages): drop provider_specific_fields from count_tokens messages

litellm puts provider_specific_fields on a returned content block, such as Gemini's thought signature on a tool_use. /v1/messages already drops it before Claude, but count_tokens sent it on, so Anthropic, Bedrock and Vertex AI answered 400 on a history with a Gemini tool turn

* fix(messages): keep stripping unsigned thinking for Claude behind any gateway

Strip unsigned thinking for every Claude name, as main does, and keep it only for
the providers and hosts known to need it back (DeepSeek, MiniMax, Tencent, z.ai,
BigModel). An anthropic/ deployment behind a custom gateway or forwarder now
strips again, so it makes one call with thinking on instead of a 400 and a retry
that drops all thinking

* test(messages): pass typed litellm params to the DeepSeek unsigned thinking test

Main's DeepSeek messages transform now reads drop_params off litellm_params, so the
plain dict the test passed failed on the merge with main

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-24 05:30:55 +00:00
..
batches feat(anthropic): workload identity federation and pluggable identity sources (#44448) 2026-10-03 17:08:30 -07:00
chat feat(cost): price Claude token calls, budget holds and auth price checks in Rust (#52484) 2026-11-23 22:00:28 +00:00
files chore(typing): clear 670 basedpyright Any errors by validating provider payloads at the boundary (#45388) 2026-10-13 15:35:23 -07:00
messages fix(anthropic): drop unsigned thinking blocks before /v1/messages reaches Claude (#51793) 2026-11-24 05:30:55 +00:00
pass_through refactor(anthropic): rename llms/anthropic/pass_through to llms/anthropic/messages (#48510) 2026-10-27 10:08:25 -07:00
__init__.py test(unit): add package markers to migrated unit test directories 2026-09-20 11:53:14 +00:00
anthropic_docs_refusal.py fix(anthropic): keep refusal stop_reason and stop_details on the logged response (#46104) 2026-10-14 14:42:49 -07:00
test_anthropic_common_utils.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_anthropic_count_tokens_transformation.py fix(anthropic): drop unsigned thinking blocks before /v1/messages reaches Claude (#51793) 2026-11-24 05:30:55 +00:00
test_anthropic_files_and_batches.py feat(anthropic): workload identity federation and pluggable identity sources (#44448) 2026-10-03 17:08:30 -07:00
test_anthropic_output_format_filter.py fix(bedrock): use native structured output on Haiku 5.5 and Sonnet 5.5, and fix Fable 5 and Sonnet 4.6 schema 400s (#46870) 2026-10-16 20:57:23 -07:00
test_anthropic_prompt_cache_prediction.py fix(proxy): resolve admin oidc api_key references on batch cancel, fine-tuning listing, OCR, /v1/messages and cache prediction (#53678) 2026-11-23 10:11:10 -08:00
test_anthropic_prompt_cache_pricing.py refactor(anthropic): move prompt cache pricing helper out of the proxy package (#45753) 2026-10-10 20:32:34 +00:00
test_anthropic_reasoning_effort.py fix(anthropic): disable optional default thinking for reasoning effort none (#50541) 2026-11-07 12:51:37 -08:00
test_anthropic_schema_filter.py fix(bedrock): use native structured output on Haiku 5.5 and Sonnet 5.5, and fix Fable 5 and Sonnet 4.6 schema 400s (#46870) 2026-10-16 20:57:23 -07:00
test_anthropic_structured_output.py test: move tests/test_litellm/llms into tests/unit/llms (#43191) 2026-09-25 12:43:23 -07:00
test_anthropic_wif.py fix(anthropic): exchange Google identity tokens again on refresh and read oidc/ references out of ANTHROPIC_IDENTITY_TOKEN (#50583) 2026-11-07 16:48:48 -08:00
test_azure_ai_cache_pricing.py test: move tests/test_litellm/llms into tests/unit/llms (#43191) 2026-09-25 12:43:23 -07:00
test_cost_calculation_dict_safety.py feat(anthropic): workload identity federation and pluggable identity sources (#44448) 2026-10-03 17:08:30 -07:00
test_count_tokens_oauth.py fix(count_tokens): count a history that carries a compaction block (#44784) 2026-10-30 00:11:55 -07:00
test_message_sanitization.py fix(bedrock): stop the prefill 400 when the last user message is empty (#50701) 2026-11-18 03:44:11 +00:00
test_messages_http_error_retry_cases.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00