litellm/tests/unit/llms/databricks
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
..
agent feat(agents): support Databricks Agent Platform agents in the agent gateway (#45681) 2026-10-15 12:42:06 -07:00
chat fix(anthropic)!: send extra_body fields at the top level of the Messages request (#48373) 2026-11-20 06:52:56 -08:00
decisions fix(databricks): reuse base OAuth for decisions (#48395) (#48527) 2026-10-26 20:18:16 -07:00
messages fix(anthropic): close /v1/messages thinking blocks with a signature and keep bridged output (#52116) 2026-11-16 20:44:27 -08:00
responses test(unit): make every tests/unit directory a package so pytest collection is unique 2026-09-20 11:50:59 +00:00
__init__.py test(unit): make every tests/unit directory a package so pytest collection is unique 2026-09-20 11:50:59 +00:00
test_databricks_common_utils.py fix(databricks): reuse base OAuth for decisions (#48395) (#48527) 2026-10-26 20:18:16 -07:00
test_databricks_cost_calculator.py chore(model_prices): sync databricks gemini-4-carbon and gemini-4-hydrogen prices (#52986) 2026-11-19 18:05:11 -08:00
test_databricks_partner_integration.py feat(agents): support Databricks Agent Platform agents in the agent gateway (#45681) 2026-10-15 12:42:06 -07:00
test_databricks_streaming_utils.py fix(vertex_ai): route Model Garden models through the generateContent adapter (#41836) 2026-10-15 08:06:49 -07:00