litellm/tests
Mikalai Karytka 7507379f61
fix(anthropic)!: send extra_body fields at the top level of the Messages request (#48373)
* fix(anthropic): send extra_body fields at the top level of the Messages request

extra_body passed to litellm.completion for an Anthropic model reached the
Messages API as a literal nested "extra_body" key, and Anthropic rejected the
request with "extra_body: Extra inputs are not permitted". That blocked every
Anthropic request field litellm doesn't model yet, including server-side
fallbacks

AnthropicConfig.transform_request now pops extra_body from the request params
and merges its keys into the wire body. The direct Anthropic, Vertex AI and
Azure paths all go through this transform. Paths on llm_http_handler, like
Bedrock invoke, already pop extra_body before transform_request, so nothing
is merged twice. extra_body only adds fields the request doesn't already
have. It can't override anything litellm built, can't change the model
(EXTRA_BODY_ROUTING_KEYS), and can't set messages or system, so content
that pre-call guardrails checked or redacted is the content that is sent

fallbacks is registered in BETA_GATED_FIELDS under both betas that unlock it,
server-side-fallback-2026-07-01 and server-side-fallback-2026-06-01. Under
drop_params a request that doesn't end up sending either of them, like a
Vertex AI leg where the 07-01 beta is filtered out or a deployment without
the header, drops fallbacks with a warning instead of getting a 400

The proxy rejects fallbacks inside a client's extra_body with a 400. Anthropic
runs the fallback model itself, so those targets never reach the key's model
access checks. Server-side fallback is set in the deployment's litellm_params
instead, next to the beta header the admin already configures there

When Anthropic serves a turn from a fallback model, usage.iterations spans
models, so its entries are no longer summed. The top-level usage, which
Anthropic documents as the attempt that served the turn, is priced at the
response's model. Attempts that declined with a billed refusal category are
not priced yet

The regression tests drive litellm.completion end to end against a mock
transport and assert the request body

* fix(anthropic): never take tools from extra_body

The proxy checks a key's or team's allowed tools against the request's
top-level tools only. A tools list inside extra_body skipped that check
and still reached Anthropic when the request had no top-level tools, so
extra_body now never supplies tools, like messages and system.

* fix(anthropic): keep every fallback attempt in usage

Dropping usage.iterations for a server-side fallback chain left only the
serving attempt in spend, so billed refusals on earlier attempts escaped
key and team budgets. Usage is back to adding up every attempt, which can
overcount but never lets billed tokens escape. #50556 prices each attempt
at its own model's rate.

* fix(proxy): reject fallbacks inside a fallback target's extra_body

A litellm fallback target can carry its own extra_body, which the router
merges into the request when that target runs. The proxy only rejected
fallbacks in the request's own extra_body, so a target's extra_body could
still pick Anthropic server-side fallback models the key isn't allowed to
use. Every fallback target's extra_body now gets the same check.

* test(anthropic): type the extra_body request test

* fix(anthropic): let extra_body override defaults on every Claude route and admit client fallbacks through the admin opt-ins

extra_body now wins over fields litellm set, so its max_tokens takes effect, while model, messages,
system, tools, and stream stay litellm's on anthropic/, azure_ai, and Bedrock invoke. A non-object
extra_body answers 400 instead of a retried 500. Bedrock invoke drops extra_body fallbacks under
drop_params without a fallback beta, like anthropic/. Client extra_body fallbacks go through the
banned-param opt-ins (allow_client_side_credentials or configurable_clientside_auth_params)

* fix(anthropic): let extra_body lower max_tokens but never raise it past the request's cap

The rate limiter reserves output tokens from the top-level max_tokens, so an
extra_body max_tokens above it would spend tokens the limiter never reserved.

* fix(anthropic): answer 400 for extra_body keys litellm builds unless drop_params is on

Setting model, messages, system, tools or stream in extra_body now answers 400 naming them, or drops them under drop_params, on anthropic/, Vertex AI, Azure AI and Bedrock invoke Claude. Databricks keeps sending extra_body as it is, and a non-object extra_body error is returned as a value

* fix(vertex_ai): apply the batch row's drop_params to its extra_body

A Claude batch row's extra_body merge got an empty litellm_params, so drop_params set on the row or deployment never reached the extra_body rule and a field litellm builds answered 400 instead of being dropped

* fix(anthropic): add the beta headers a field sent in extra_body needs

A field like context_management sent in extra_body reached Anthropic at the top level without the anthropic-beta header that the same top-level field gets, so Anthropic answered 400. The extra_body fields litellm forwards now count toward the beta headers too

* fix(anthropic): answer 400 for a raised extra_body max_tokens and promote system on Azure AI and OpenCode Go

An extra_body max_tokens above the request's answers 400 unless drop_params
is on, which caps it, so the output tokens the limiters reserved still bound
the call. Keys litellm builds that extra_body tries to set are dropped with a
warning under drop_params. Azure AI and OpenCode Go send extra_body.system as
the system prompt, since litellm moves a top-level system kwarg there.
extra_body output_config now wins over the mapped one, and transform_request
no longer pops extra_body from the caller's optional_params.

* fix(bedrock): keep the extra_body guard on Bedrock Mantle without the invoke beta strip

Bedrock Mantle inherits the Bedrock invoke extra_body guard. The invoke strip reads betas from the body's anthropic_beta list, which a Mantle body never carries, so under drop_params it dropped beta-gated thinking fields the client sent. Mantle now runs the Anthropic guard alone, the same way its own request path skips the strip

The internal-params wire test now reads the custom key at the top level on anthropic/, where extra_body fields go after this change

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-20 06:52:56 -08:00
..
_support feat(gateway): resolve Redis and the database in their own crates and hand them to routes as axum state (#52222) 2026-11-18 09:16:13 +09:00
audio_tests
base_sdk_tests fix(packaging): ship the license public key in the litellm-proxy sdist (#52970) 2026-11-19 23:18:32 +00:00
basic_proxy_startup_tests feat(docker)!: one shipped image and one entrypoint beside the Dockerfile (#43332) 2026-11-10 18:32:16 -08:00
benchmarks fix(proxy): count the request's tool_choice on the token count routes (#51133) 2026-11-14 07:53:27 -05:00
code_coverage_tests feat(docker): add a FIPS 140-3 proxy image (#50330) 2026-11-19 17:16:05 -08:00
documentation_tests chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
e2e test(e2e): move the Gemini Flash alias off the retired gemini-2.5-flash (#52823) 2026-11-19 20:47:54 -08:00
e2e_harness fix(perf): integrate pod CPU windows so cpu_seconds_per_request stops under-reading (#52735) 2026-11-19 09:57:06 -08:00
fixtures
harness_e2e
integration fix(anthropic)!: send extra_body fields at the top level of the Messages request (#48373) 2026-11-20 06:52:56 -08:00
rust-python-harness feat(messages): serve vertex ai claude messages on rust only (#52314) 2026-11-19 21:15:10 +00:00
test_litellm_rust feat(messages): serve bedrock claude_platform messages on rust only (#52681) 2026-11-19 14:34:02 -08:00
typing chore: move config and docs off the repo root (#50966) 2026-11-11 18:29:35 -08:00
unit fix(anthropic)!: send extra_body fields at the top level of the Messages request (#48373) 2026-11-20 06:52:56 -08:00
windows_tests 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
__init__.py
_fake_openai_endpoint_server.py
_flush_vcr_cache.py
_live_test_helpers.py
_master_key.py
_module_state.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
_openai_record_replay_proxy.py
_process_helpers.py
_vcr_conftest_common.py ci: remove CircleCI (#50950) 2026-11-11 15:38:27 -08:00
_vcr_redis_persister.py
_wait_helpers.py
_ws_vcr.py
AGENTS.md
capturing_transport.py
eval_swe_bench.py
fake_openai_endpoint.py
gettysburg.wav
large_text.py
openai_batch_completions.jsonl
pyrightconfig.json
README.MD
test_logging.conf
test_rust_python_harness.py
VCR_TESTING.md
white_100x100.png

In total litellm runs 1000+ tests

[02/20/2025] Update:

To make it easier to contribute and map what behavior is tested,

we've started mapping the litellm directory in tests/unit

This folder can only run mock tests.