fix(anthropic): leave reasoning to the model on a first-turn between_tools when its levels are unlisted

A first-turn between_tools request sent reasoning effort none to every
openai/ deployment whose cost map row lists no effort levels, so a backend
that requires reasoning answered 400 where main answered 200. That turn
now sends no reasoning unless the row or the deployment's model_info lists
the model's levels. The OpenAI integration fixture now expects the summary
that display summarized asks for.
This commit is contained in:
mateo-berri 2026-11-03 13:06:01 -08:00
parent 476755238f
commit c53486f37e
6 changed files with 56 additions and 11 deletions

View file

@ -1234,6 +1234,7 @@ class LiteLLMAnthropicMessagesAdapter:
return
thinking_type: Final = thinking.get("type") if isinstance(thinking, dict) else None
is_between_tools: Final = isinstance(thinking_type, str) and thinking_type == "between_tools"
declared_effort: Final = output_effort if thinking_type == "adaptive" else None
if is_claude_target and not declared_effort:
return
@ -1241,7 +1242,7 @@ class LiteLLMAnthropicMessagesAdapter:
mirrors_responses_bridge: Final = custom_llm_provider in RESPONSES_API_PROVIDERS
reasoning_effort: Final = declared_effort or (
self._between_tools_reasoning_effort(anthropic_message_request, output_effort)
if thinking_type == "between_tools" and mirrors_responses_bridge
if is_between_tools and mirrors_responses_bridge
else self.translate_anthropic_thinking_to_reasoning_effort(
cast(AnthropicThinkingParam, thinking),
model=model,
@ -1264,7 +1265,12 @@ class LiteLLMAnthropicMessagesAdapter:
)
)
target_effort: Final = (
reasoning_off_level(model, custom_llm_provider, drop_params=drop_params)
reasoning_off_level(
model,
custom_llm_provider,
drop_params=drop_params,
between_tools=is_between_tools,
)
if mirrors_responses_bridge and effort_sent == REASONING_EFFORT_OFF
else effort_sent
)

View file

@ -270,13 +270,16 @@ def _reasoning_for_target(
custom_llm_provider: str | None,
deployment_model_info: object,
drop_params: bool,
between_tools: bool,
) -> object:
reasoning_params: Final = as_str_mapping(reasoning)
effort: Final = reasoning_params.get("effort") if reasoning_params is not None else None
if reasoning_params is None or not isinstance(effort, str):
return reasoning
if effort == REASONING_EFFORT_OFF:
off_level: Final = reasoning_off_level(model, custom_llm_provider, drop_params=drop_params)
off_level: Final = reasoning_off_level(
model, custom_llm_provider, drop_params=drop_params, between_tools=between_tools
)
if off_level is None or _responses_api_refuses_reasoning_effort(model, custom_llm_provider):
return None
return {**reasoning_params, "effort": off_level}
@ -463,6 +466,7 @@ def _build_responses_kwargs(
custom_llm_provider=provider,
deployment_model_info=forwarded_kwargs.get("model_info"),
drop_params=drop_params,
between_tools=thinking is not None and thinking.get("type") == "between_tools",
)
if _maps_reasoning(thinking, model, provider, forwarded_kwargs)
else None

View file

@ -259,11 +259,13 @@ def _lowest_tier_instead_of_off(levels: tuple[str, ...] | None) -> str | None:
return next((tier for tier in _LOWEST_REASONING_TIERS if tier in levels), None)
def reasoning_off_level(model: str, custom_llm_provider: str | None, *, drop_params: bool) -> str | None:
def reasoning_off_level(
model: str, custom_llm_provider: str | None, *, drop_params: bool, between_tools: bool
) -> str | None:
from litellm.utils import supports_none_reasoning_effort, supports_reasoning
levels: Final = model_reasoning_efforts(model, custom_llm_provider)
if levels == ():
if levels == () or (between_tools and levels is None):
return None
lowest_tier: Final = _lowest_tier_instead_of_off(levels)
if lowest_tier is not None:

View file

@ -140,7 +140,7 @@ OPENAI_RESPONSES_REQUEST_FROM_MESSAGES: Final[Mapping[str, JsonValue]] = {
"zero, negative and non-integer quantities before choosing the next file to "
"inspect.",
"max_output_tokens": 1024,
"reasoning": {"effort": "high"},
"reasoning": {"effort": "high", "summary": "auto"},
"tools": [
{
"type": "function",

View file

@ -388,7 +388,7 @@ _RESPONSE: Final = {
"id": "msg_1",
"status": "completed",
"role": "assistant",
"content": [{"type": "output_text", "text": "ok", "annotations": []}],
"content": [{"type": "output_text", "text": "ok", "annotations": ()}],
}
],
"usage": {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2},
@ -633,10 +633,12 @@ def _chat_completion_kwargs(
return completion_kwargs
def _lowest_accepted_level(model: str, *, drop_params: bool) -> str | None:
def _lowest_accepted_level(model: str, *, drop_params: bool, between_tools: bool) -> str | None:
levels: Final = model_reasoning_efforts(model, "openai")
if levels is not None:
return levels[0] if levels else None
if between_tools:
return None
drops_unconfirmed_off: Final = (
drop_params and supports_reasoning(model, "openai") and not supports_none_reasoning_effort(model, "openai")
)
@ -676,7 +678,9 @@ class TestAnOpenAIChatRouteRequestGetsTheResponsesBridgeThinkingControls:
drop_params=drop_params,
)
assert sent.get("reasoning_effort") == _lowest_accepted_level(model, drop_params=drop_params)
assert sent.get("reasoning_effort") == _lowest_accepted_level(
model, drop_params=drop_params, between_tools=thinking["type"] == "between_tools"
)
@pytest.mark.parametrize("model", [*OPENAI_CHAT_TARGETS, "openai/gpt-4.1", "openai/o3"])
def test_a_bare_output_effort_is_the_tier_when_the_model_reasons_unasked(
@ -817,3 +821,22 @@ class TestDisabledOnAnOffMapOpenAIDeployment:
)
assert sent["reasoning"] == {"effort": "none"}
@pytest.mark.asyncio
@pytest.mark.parametrize(
"uses_chat_route, reasoning_key", [(True, "reasoning_effort"), (False, "reasoning")], ids=["chat-route", "responses-bridge"]
)
async def test_a_first_turn_between_tools_leaves_reasoning_to_the_model(
self, respx_mock: respx.MockRouter, local_model_cost_map: None, uses_chat_route: bool, reasoning_key: str
) -> None:
sent: Final = await _body_sent_upstream(
respx_mock,
{**_OFF_MAP_OPENAI_DEPLOYMENT, "model": "openai/vendor/off-map-hybrid-reasoner"},
{},
rf"https://compatible\.example\.test/v1/{'chat/completions' if uses_chat_route else 'responses'}",
_CHAT_COMPLETION if uses_chat_route else _RESPONSE,
thinking={"type": "between_tools"},
stop_sequences=["STOP"] if uses_chat_route else None,
)
assert reasoning_key not in sent

View file

@ -152,6 +152,10 @@ OFF_THINKING: Final = [{"type": "disabled"}, {"type": "between_tools"}]
OFF_THINKING_IDS: Final = ["disabled", "between_tools-first-turn"]
def _leaves_reasoning_to_the_model(model: str, thinking: dict[str, object]) -> bool:
return thinking["type"] == "between_tools" and model_reasoning_efforts(model, "openai") is None
def _lowest_listed_off_switch(model: str) -> str:
levels: Final = model_reasoning_efforts(model, "openai") or ()
return next((level for level in ("none", "minimal", "low") if level in levels), "none")
@ -172,7 +176,10 @@ def test_build_responses_kwargs_turns_reasoning_off_at_the_lowest_level_the_mode
map_row: Final = litellm.model_cost.get(model)
never_reasons: Final = map_row is not None and not map_row.get("supports_reasoning")
assert responses_kwargs.get("reasoning") == (None if never_reasons else {"effort": _lowest_listed_off_switch(model)})
sends_no_reasoning: Final = never_reasons or _leaves_reasoning_to_the_model(model, thinking)
assert responses_kwargs.get("reasoning") == (
None if sends_no_reasoning else {"effort": _lowest_listed_off_switch(model)}
)
@pytest.mark.parametrize("thinking", OFF_THINKING, ids=OFF_THINKING_IDS)
@ -192,7 +199,10 @@ def test_build_responses_kwargs_under_drop_params_keeps_a_listed_lower_tier_and_
map_row: Final = litellm.model_cost.get(model)
confirms_none: Final = map_row is not None and map_row.get("supports_none_reasoning_effort") is True
keeps_none: Final = map_row is None or (bool(map_row.get("supports_reasoning")) and confirms_none)
assert responses_kwargs.get("reasoning") == ({"effort": off_switch} if off_switch != "none" or keeps_none else None)
sends_off_switch: Final = (off_switch != "none" or keeps_none) and not _leaves_reasoning_to_the_model(
model, thinking
)
assert responses_kwargs.get("reasoning") == ({"effort": off_switch} if sends_off_switch else None)
@pytest.mark.parametrize("model", ["gpt-5.4-mini", "gpt-5.1", "gpt-6-luna", "gpt-bridge-unmapped"])