litellm/tests/unit/llms/anthropic
zoveki 65d625b86c
fix(bedrock): stop the prefill 400 when the last user message is empty (#50701)
* fix(bedrock): keep an empty user turn on Converse so Claude does not answer a prefill 400

* fix(responses): keep a user input item without content as an empty user turn

* fix(bedrock): only fill an empty last user turn after an assistant turn

A placeholder now goes in only when the request ends on a user turn with no content right after an assistant turn. Mid-conversation empty turns stay dropped, so tool calls stay next to their results and media-only turns are not called empty. The Responses adapter keeps a content-less final user item only on routes that fill it, and only when building the request to send.

* fix(bedrock): send the empty turn placeholder only to models that refuse a prefill

Check the explicit supports_assistant_prefill false entry in the cost map before filling an empty last user turn, so Nova and older Claude models keep their current behavior

Treat blank guarded_text and whitespace-only last turns as empty on those models, keep cache_control on the placeholder, and keep a lone null user item on a previous_response_id follow-up

* fix(responses): add the empty user turn after the stored history loads

A lone null user item on a previous_response_id follow-up was filled before
the stored history loaded, so an unknown id reached the model instead of
LiteLLM's own 400. The follow-up fill now runs after the history merge and
only when the merged messages end on an assistant turn

Stored turns replay with the same empty user turn when the route fills, so a
later follow-up resends the conversation the model saw and keeps its cache
prefix, and /v1/responses/input_tokens counts the same messages

* fix(responses): replay a stored empty user turn with the text the model saw

Converse drops an empty user message that sits between two assistant turns,
so the replayed empty turn vanished and the next turn sent a different
conversation than the follow-up did, which also missed its prompt cache. The
replay now sends the same placeholder text the follow-up sent, and that
placeholder constant is public so the Responses bridge can reuse it

* test(rust): point the placeholder comment at the public constant

The Python placeholder constant lost its leading underscore, so the Rust test comment now names it as it is

* fix(responses): replay each stored empty turn by its own model and budget its cache mark

A previous_response_id follow-up whose one null user item carried cache_control got its empty user turn after the replayed history had already used all 4 cache marks, so the provider saw 5 and refused the request. The turn is now built before the replay budget, counted as one of the request's own marks and checked the same way

A stored turn that got no answer no longer replays the placeholder, which used to merge into the next user turn

A stored turn now replays its empty user turn only when the model that answered it needed one, read from its own stored model, provider and priced model. A conversation that switched models mid-session used to replay every stored turn by the current model, so a turn the old model answered without the placeholder came back with it

The /v1/responses/input_tokens count for a deployment named by an inference profile ARN now reads the deployment's base_model the way inference does, so the counted request keeps the same empty user turn the real request sends

A request with no provider and a model name that does not resolve, such as a Bedrock batch record naming an alias, no longer raises when its input ends on an empty user item. It keeps the old behavior and drops the item

A new test covers litellm.modify_params on Bedrock Converse, which keeps sending its continue message for an empty last user turn

* fix(responses): count a blank turn the way inference sends it and keep a restored turn's mark under the cap

/v1/responses/input_tokens on Anthropic, Azure AI, Vertex and Bedrock now sends a blank user or assistant turn through the translation chat inference uses, so the count request carries the same placeholder text inference sends. A follow-up whose input ended on a null user item after an assistant turn used to count with an empty user message, and the count API answered that with a 400

Under drop_params, the empty user turn a previous_response_id follow-up restores now shares the request's 4 cache marks with the tools, the top-level cache_control and the input items, and the newest marks win as they do on a single-turn request. A marked null item used to add a 5th mark that the provider refused

* fix(responses): match inference for object inputs, alias counts, string follow-ups and model_info base_model

An object input on /v1/responses no longer reaches the empty-turn restore, so it is served instead of answering 500

input_tokens bridges the request with the routed deployment's model, so an alias counts the placeholder inference sends, and a string input on a previous_response_id follow-up replays the stored empty turn like a list input

A deployment that sets base_model only in model_info now gets the placeholder on /v1/responses and its count, as it already did on chat

The Anthropic, Azure AI and Bedrock counters keep a native Anthropic block as sent when a blank text turn sits next to it

* fix(responses): treat an empty base_model as unset when reading model_info

A deployment with base_model "" in litellm_params and a base_model in model_info now gets the placeholder on /v1/responses, as chat and input_tokens already read it

Tests now cover the previous_response_id follow-up read of model_info.base_model, a request base_model winning over it, and the empty-string fallback

* fix(responses): count what inference sends for client base_model and Converse blank assistant turns

The input_tokens route now leaves out the client pricing fields inference strips for a key without allow_client_pricing_override, and the Bedrock Converse Claude count drops empty assistant text and empty assistant turns the same way Converse inference does.

* fix(bedrock): count an empty Converse assistant turn as the continue message under modify_params

Converse inference replaces an all-empty assistant turn with the default continue message when modify_params is on, so the count now sends the same turn instead of dropping it.

* fix(bedrock): match the Converse continue-message check when counting empty assistant turns

The count now takes the continue-message path whenever litellm.modify_params is not False, as the inference helper does, counts the continue message when the helper leaves nothing Converse reads, and keeps the plain rule for a text block without text so it no longer answers 500.

* fix(bedrock): drop an empty Converse assistant turn the way inference does and add its continue turn between user turns

Under modify_params, an empty assistant turn with a non-content key is now dropped like Converse inference drops it, and the count adds Please continue. between adjacent user or tool turns only when modify_params is True, as the Converse loop does.

* fix(bedrock): count the continue turns and the neutralized tool history that Converse inference sends

* fix(bedrock): count Converse system messages and response_format tool history the way inference sends them

The Converse count now maps developer messages to system, keeps the leading system run for the system prompt, turns later system messages into the user turns inference sends, and keeps tool history as tool blocks when response_format adds a tool. Counting no longer logs inference's neutralization warning.

* fix(responses): replay a stored blank user turn with the placeholder its model saw

A previous_response_id follow-up replayed a stored user turn of only whitespace as sent, so Bedrock answered 400 on the next turn. The replay now swaps that turn for the placeholder when the stored model needed one, for list items and string input alike

* fix(responses): replay a blank user turn with the continue text Converse sent

With modify_params or a deployment user_continue_message, Bedrock Converse
sends the continue text for a blank user turn. A previous_response_id
follow-up replayed that stored turn with the empty-content placeholder
instead, so the history the model saw changed between turns. The replay now
keeps the turn blank in that case, so Converse fills it with the same continue
text it sent the first time

* fix(bedrock): count blank last user turns and system text the way Converse inference sends them

The Converse token count now sends the placeholder or continue text that
inference sends for an empty, null or blank last user turn, keeps only the
non-empty text parts of a leading system message, decides the response_format
tool by the deployment model, and leaves a message without a role for Bedrock
to reject with a 400. A previous_response_id follow-up count drops
additional_tools input items before restoring the empty user turn, as inference
does. When the deployment continues blank user turns, a stored follow-up with
several blank user items replays each of them instead of one placeholder turn.

* fix(bedrock): count list continue messages, keep string turns when lifting system, and leave other routes' stored blanks alone

The count now reads the deployment's continue message the way inference does, so list content no longer answers 500 and a continue turn around an assistant turn counts. A leading system message is lifted without translating the rest of the request, a malformed user block answers 400 again, a Responses count passes the deployment's continue message to the stored history, and a follow-up on a route that takes blank turns replays them as stored

* fix(responses): fill a stored blank turn only on routes that refuse blank text

* fix(bedrock): drop the empty turns from a Converse token count that inference drops

* fix(bedrock): count a spaces-only user turn as Converse sends it

* fix(responses): replay a stored empty string input turn on follow-ups

* fix(bedrock): count an empty last user turn the way Converse inference sends it

Drop an earlier empty user turn before the placeholder turn, and keep the
blank turn's cache mark on the placeholder, so the count matches inference.

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-18 03:44:11 +00:00
..
batches feat(anthropic): workload identity federation and pluggable identity sources (#44448) 2026-10-03 17:08:30 -07:00
chat fix(anthropic): accept temperature=1 on Claude models that removed sampling params (#52341) 2026-11-17 13:24:45 -08: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): close the Responses upstream when a bridged /v1/messages stream is closed (#51274) 2026-11-17 22:14:25 +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
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(bedrock): stop the prefill 400 when the last user message is empty (#50701) 2026-11-18 03:44:11 +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 feat(messages): serve anthropic messages on rust only (#51119) 2026-11-12 00:07:20 +00: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