mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-24 22:09:56 +00:00
* fix(anthropic): strip beta-gated body fields when a leg drops the anthropic-beta When a provider leg drops an anthropic-beta value (the chat handler's allowlist filter, /v1/messages' filtered headers, Bedrock Converse's additionalModelRequestFields), also drop the body fields that beta gates (thinking.block_binding, thinking.display=updates, mid-conversation system clear_at, output_config, tool_addition/tool_removal blocks) so the request degrades on that leg instead of failing with a provider 400. Copy-on-write so a fallback leg still sends the full request, and each strip is logged at WARNING. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(integration): prove a leg that drops a beta sends neither the beta nor clear_at Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(bedrock): strip beta-gated fields against the final body anthropic_beta list Converse counts the body anthropic_beta list as active, Invoke strips after its auto-injected betas are known and injects thinking-binding-controls for thinking.block_binding, test helpers are fully typed Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(anthropic): strip beta-gated system fields before an older model's system turn becomes a user turn Models without supports_mid_conversation_system send no mid-conversation beta, so tool_addition and tool_removal blocks are dropped (and logged) before the turn is converted instead of riding along as user content. The Invoke output_config regression test reads the real cost map instead of patching _bedrock_model_supports Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(anthropic): pin the integration proxy to the bundled beta allowlist and type the helper locals Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(anthropic): annotate every new test local with Final and type the fixtures and returns Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(anthropic): strip beta-gated body fields only under drop_params Without drop_params the request now goes out as sent and the provider answers its own 400, like any other unsupported param. inline-tools-2026-09-15 also unlocks tool_addition and tool_removal blocks, so a leg that carries it keeps them. * fix(anthropic): strip beta-gated fields on Foundry chat and Bedrock chat Invoke under drop_params Foundry chat and Bedrock chat Invoke now drop beta-gated body fields the same way the other legs do when drop_params is on and the request does not send the gating beta. The mid-conversation system normalizer validates only on the conversion path, and the strip WARNING names the beta the caller can send to keep the field. * fix(anthropic): send the thinking betas for block_binding and display updates, strip Databricks chat thinking Every Claude leg now adds the thinking-binding-controls beta when thinking carries block_binding and the display-updates beta for display updates, so those fields go through instead of a 400. Databricks chat rejects both fields with or without the beta, so it drops them under drop_params. A system turn the strip empties stays when it still carries output_config, and the drop warning names both causes. * fix(anthropic): send the thinking betas on every leg and keep fields where the beta is unknown Vertex messages and Claude Platform chat now add the thinking betas when thinking carries block_binding or display updates. Converse sends only the caller's header betas when it has them. A provider with no row in the beta config forwards beta-gated fields instead of dropping them, an emptied trailing system turn that would leave an assistant prefill is sent as is, and the prompt cache baseline body matches the body that goes out. The drop WARNING now names the beta that unlocks each field * fix(anthropic): filter Converse body betas, keep Foundry auto betas, read the bundled beta config for row checks * fix(anthropic): validate messages only when a beta-gated drop runs A non-object messages entry on a Claude model without mid-conversation system support raised a raw pydantic ValidationError before the beta-config and drop_params checks. The strip now passes the messages through untouched unless it drops something, so the provider's 400 reaches SDK callers as BadRequestError again * fix(anthropic): keep system blocks with a non-string type instead of raising --------- Co-authored-by: mateo <mateo@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
301 lines
12 KiB
Python
301 lines
12 KiB
Python
import logging
|
|
from typing import Final
|
|
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm.anthropic_beta_gated_fields import (
|
|
ANTHROPIC_INLINE_TOOLS_BETA_HEADER,
|
|
ANTHROPIC_MID_CONVERSATION_SYSTEM_CLEAR_AT_BETA_HEADER,
|
|
ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER,
|
|
BETA_GATED_FIELDS,
|
|
active_beta_values,
|
|
log_beta_gated_strips,
|
|
strip_beta_gated_native_request,
|
|
strip_beta_gated_request_fields,
|
|
strip_beta_gated_system_messages,
|
|
strip_beta_gated_thinking,
|
|
)
|
|
from litellm.types.llms.anthropic import (
|
|
ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER,
|
|
ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER,
|
|
ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER,
|
|
)
|
|
|
|
EVERY_BETA: Final = frozenset(BETA_GATED_FIELDS)
|
|
BOUND_THINKING: Final = {
|
|
"type": "adaptive",
|
|
"block_binding": {"prefix_mismatch_behavior": "error"},
|
|
"display": "updates",
|
|
}
|
|
|
|
|
|
def _system_turn() -> dict[str, object]:
|
|
return {
|
|
"role": "system",
|
|
"clear_at": "end_turn",
|
|
"output_config": {"effort": "low"},
|
|
"content": [
|
|
{"type": "text", "text": "stay terse"},
|
|
{"type": "tool_addition", "tools": [{"name": "lookup"}]},
|
|
{"type": "tool_removal", "tool_names": ["lookup"]},
|
|
],
|
|
}
|
|
|
|
|
|
def test_thinking_with_every_beta_active_is_returned_as_is() -> None:
|
|
result: Final = strip_beta_gated_thinking(BOUND_THINKING, EVERY_BETA, drop_params=True)
|
|
assert result.thinking is BOUND_THINKING
|
|
assert result.stripped == ()
|
|
|
|
|
|
def test_thinking_loses_only_the_field_whose_beta_is_absent() -> None:
|
|
without_binding: Final = strip_beta_gated_thinking(
|
|
BOUND_THINKING, EVERY_BETA - {ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER}, drop_params=True
|
|
)
|
|
assert without_binding.thinking == {"type": "adaptive", "display": "updates"}
|
|
assert without_binding.stripped == ("thinking.block_binding",)
|
|
|
|
without_display: Final = strip_beta_gated_thinking(
|
|
BOUND_THINKING, EVERY_BETA - {ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER}, drop_params=True
|
|
)
|
|
assert without_display.thinking == {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}}
|
|
assert without_display.stripped == ("thinking.display=updates",)
|
|
assert BOUND_THINKING == {
|
|
"type": "adaptive",
|
|
"block_binding": {"prefix_mismatch_behavior": "error"},
|
|
"display": "updates",
|
|
}, "copy-on-write: the caller's thinking dict must survive for the fallback leg"
|
|
|
|
|
|
def test_thinking_display_other_than_updates_is_not_gated() -> None:
|
|
thinking: Final = {"type": "adaptive", "display": "summarized"}
|
|
result: Final = strip_beta_gated_thinking(thinking, frozenset(), drop_params=True)
|
|
assert result.thinking is thinking
|
|
|
|
|
|
@pytest.mark.parametrize("thinking", [None, "enabled", 12, ["adaptive"]])
|
|
def test_non_mapping_thinking_passes_through(thinking: object) -> None:
|
|
result: Final = strip_beta_gated_thinking(thinking, frozenset(), drop_params=True)
|
|
assert result.thinking is thinking
|
|
assert result.stripped == ()
|
|
|
|
|
|
def test_system_message_keeps_every_gated_field_when_the_betas_are_active() -> None:
|
|
messages: Final = [{"role": "user", "content": "hi"}, _system_turn()]
|
|
result: Final = strip_beta_gated_system_messages(messages, EVERY_BETA, drop_params=True)
|
|
assert result.messages is messages
|
|
assert result.stripped == ()
|
|
|
|
|
|
def test_system_message_loses_exactly_the_fields_whose_betas_are_absent() -> None:
|
|
original: Final = _system_turn()
|
|
messages: Final = [{"role": "user", "content": "hi"}, original]
|
|
result: Final = strip_beta_gated_system_messages(
|
|
messages,
|
|
EVERY_BETA
|
|
- {
|
|
ANTHROPIC_MID_CONVERSATION_SYSTEM_CLEAR_AT_BETA_HEADER,
|
|
ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER,
|
|
ANTHROPIC_INLINE_TOOLS_BETA_HEADER,
|
|
},
|
|
drop_params=True,
|
|
)
|
|
assert result.messages == [
|
|
{"role": "user", "content": "hi"},
|
|
{
|
|
"role": "system",
|
|
"output_config": {"effort": "low"},
|
|
"content": [{"type": "text", "text": "stay terse"}],
|
|
},
|
|
]
|
|
assert result.stripped == (
|
|
"messages[].system.clear_at",
|
|
"messages[].system.content[].tool_addition|tool_removal",
|
|
)
|
|
assert original == _system_turn(), "copy-on-write: the caller's system message must survive"
|
|
|
|
|
|
def test_per_turn_control_beta_also_unlocks_system_output_config() -> None:
|
|
messages: Final = [{"role": "system", "output_config": {"effort": "low"}, "content": "terse"}]
|
|
assert (
|
|
strip_beta_gated_system_messages(
|
|
messages, frozenset({"per-turn-control-2026-07-01"}), drop_params=True
|
|
).messages
|
|
is messages
|
|
)
|
|
assert (
|
|
strip_beta_gated_system_messages(
|
|
messages, frozenset({ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER}), drop_params=True
|
|
).messages
|
|
is messages
|
|
)
|
|
assert strip_beta_gated_system_messages(messages, frozenset(), drop_params=True).messages == [
|
|
{"role": "system", "content": "terse"}
|
|
]
|
|
|
|
|
|
def test_inline_tools_beta_also_unlocks_system_tool_changes() -> None:
|
|
messages: Final = [
|
|
{
|
|
"role": "system",
|
|
"content": [{"type": "tool_removal", "tool_names": ["lookup"]}, {"type": "text", "text": "x"}],
|
|
}
|
|
]
|
|
assert (
|
|
strip_beta_gated_system_messages(
|
|
messages, frozenset({ANTHROPIC_INLINE_TOOLS_BETA_HEADER}), drop_params=True
|
|
).messages
|
|
is messages
|
|
)
|
|
assert (
|
|
strip_beta_gated_system_messages(
|
|
messages, frozenset({ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER}), drop_params=True
|
|
).messages
|
|
is messages
|
|
)
|
|
assert strip_beta_gated_system_messages(messages, frozenset(), drop_params=True).messages == [
|
|
{"role": "system", "content": [{"type": "text", "text": "x"}]}
|
|
]
|
|
|
|
|
|
def test_system_block_with_unhashable_type_is_kept_for_the_provider_to_reject() -> None:
|
|
messages: Final = [{"role": "system", "content": [{"type": [], "text": "x"}]}]
|
|
result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True)
|
|
assert result.messages is messages
|
|
assert result.stripped == ()
|
|
|
|
|
|
def test_system_message_emptied_by_the_strip_is_dropped() -> None:
|
|
messages: Final = [
|
|
{"role": "user", "content": "hi"},
|
|
{"role": "system", "content": [{"type": "tool_removal", "tool_names": ["lookup"]}]},
|
|
{"role": "assistant", "content": "ok"},
|
|
]
|
|
result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True)
|
|
assert result.messages == [{"role": "user", "content": "hi"}, {"role": "assistant", "content": "ok"}]
|
|
assert result.stripped == (
|
|
"messages[].system.content[].tool_addition|tool_removal",
|
|
"messages[].system (emptied by the strip)",
|
|
)
|
|
|
|
|
|
def test_emptied_trailing_system_turn_after_an_assistant_turn_is_sent_as_is() -> None:
|
|
messages: Final = [
|
|
{"role": "user", "content": "hi"},
|
|
{"role": "assistant", "content": "ok"},
|
|
{"role": "system", "content": [{"type": "tool_removal", "tool_names": ["lookup"]}]},
|
|
]
|
|
result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True)
|
|
assert result.messages is messages
|
|
assert result.stripped == ()
|
|
|
|
|
|
def test_emptied_system_turn_is_dropped_when_a_user_turn_still_ends_the_conversation() -> None:
|
|
messages: Final = [
|
|
{"role": "user", "content": "hi"},
|
|
{"role": "assistant", "content": "ok"},
|
|
{"role": "system", "content": [{"type": "tool_removal", "tool_names": ["lookup"]}]},
|
|
{"role": "user", "content": "go on"},
|
|
]
|
|
result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True)
|
|
assert [message["role"] for message in result.messages] == ["user", "assistant", "user"]
|
|
|
|
|
|
def test_system_turn_emptied_by_the_strip_stays_as_a_directive_when_its_output_config_survives() -> None:
|
|
directive: Final = {
|
|
"role": "system",
|
|
"output_config": {"effort": "low"},
|
|
"content": [{"type": "tool_removal", "tool_names": ["lookup"]}],
|
|
}
|
|
result: Final = strip_beta_gated_system_messages(
|
|
[directive], frozenset({ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER}), drop_params=True
|
|
)
|
|
assert result.messages == [{"role": "system", "output_config": {"effort": "low"}, "content": []}]
|
|
assert result.stripped == ("messages[].system.content[].tool_addition|tool_removal",)
|
|
|
|
|
|
def test_system_turn_emptied_by_the_strip_is_dropped_when_its_output_config_is_gated_too() -> None:
|
|
directive: Final = {
|
|
"role": "system",
|
|
"output_config": {"effort": "low"},
|
|
"content": [{"type": "tool_removal", "tool_names": ["lookup"]}],
|
|
}
|
|
result: Final = strip_beta_gated_system_messages([directive], frozenset(), drop_params=True)
|
|
assert result.messages == []
|
|
assert result.stripped[-1] == "messages[].system (emptied by the strip)"
|
|
|
|
|
|
def test_request_strip_replaces_only_thinking_and_messages_and_keeps_the_dict_otherwise() -> None:
|
|
request: Final = {
|
|
"model": "claude",
|
|
"thinking": dict(BOUND_THINKING),
|
|
"messages": [_system_turn()],
|
|
"max_tokens": 5,
|
|
}
|
|
result: Final = strip_beta_gated_request_fields(request, frozenset(), drop_params=True)
|
|
assert result.request == {
|
|
"model": "claude",
|
|
"thinking": {"type": "adaptive"},
|
|
"messages": [{"role": "system", "content": [{"type": "text", "text": "stay terse"}]}],
|
|
"max_tokens": 5,
|
|
}
|
|
assert result.request is not request
|
|
assert request["thinking"] == BOUND_THINKING
|
|
assert request["messages"] == [_system_turn()]
|
|
|
|
|
|
def test_request_strip_is_identity_when_nothing_is_gated() -> None:
|
|
request: Final = {
|
|
"thinking": {"type": "enabled", "budget_tokens": 1024},
|
|
"messages": [{"role": "user", "content": "hi"}],
|
|
}
|
|
result: Final = strip_beta_gated_request_fields(request, frozenset(), drop_params=True)
|
|
assert result.request is request
|
|
assert result.stripped == ()
|
|
|
|
|
|
@pytest.mark.parametrize("drop_params", [None, False, "false"])
|
|
def test_without_drop_params_every_gated_field_is_sent_as_is(
|
|
drop_params: object, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
messages: Final = [{"role": "user", "content": "hi"}, _system_turn()]
|
|
request: Final = {"thinking": BOUND_THINKING, "messages": messages}
|
|
|
|
assert strip_beta_gated_thinking(BOUND_THINKING, frozenset(), drop_params=drop_params).thinking is BOUND_THINKING
|
|
assert strip_beta_gated_system_messages(messages, frozenset(), drop_params=drop_params).messages is messages
|
|
assert strip_beta_gated_request_fields(request, frozenset(), drop_params=drop_params).request is request
|
|
native: Final = strip_beta_gated_native_request({}, {"thinking": BOUND_THINKING}, messages, drop_params=drop_params)
|
|
assert (native.thinking, list(native.messages), native.thinking_stripped, native.messages_stripped) == (
|
|
BOUND_THINKING,
|
|
messages,
|
|
False,
|
|
False,
|
|
)
|
|
|
|
|
|
def test_global_drop_params_strips_without_a_per_call_flag(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(litellm, "drop_params", True)
|
|
assert strip_beta_gated_thinking(BOUND_THINKING, frozenset(), drop_params=None).thinking == {"type": "adaptive"}
|
|
|
|
|
|
def test_active_beta_values_reads_the_header_case_insensitively_and_merges_body_betas() -> None:
|
|
assert active_beta_values({"Anthropic-Beta": "a-2026-01-01, b-2026-01-01 ,"}, ["c-2026-01-01", 7]) == frozenset(
|
|
{"a-2026-01-01", "b-2026-01-01", "c-2026-01-01"}
|
|
)
|
|
assert active_beta_values({}) == frozenset()
|
|
|
|
|
|
def test_every_strip_is_logged_once_at_warning_with_the_fields_model_and_provider(
|
|
caplog: pytest.LogCaptureFixture,
|
|
) -> None:
|
|
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
|
|
log_beta_gated_strips(("thinking.block_binding", "thinking.block_binding"), model="claude", provider="azure_ai")
|
|
log_beta_gated_strips((), model="claude", provider="azure_ai")
|
|
warnings: Final = [record for record in caplog.records if record.levelno == logging.WARNING]
|
|
assert len(warnings) == 1
|
|
assert (
|
|
"Dropped thinking.block_binding (unlocked by anthropic-beta thinking-binding-controls-2026-08-01) for "
|
|
"model=claude provider=azure_ai" in warnings[0].getMessage()
|
|
)
|