fix(clf_ai_gateway): refuse a reasoning_effort dict without a level unless drop_params drops it

This commit is contained in:
mateo-berri 2026-10-29 18:22:16 -07:00
parent b7ea058030
commit c7103ef626
2 changed files with 35 additions and 7 deletions

View file

@ -2,6 +2,7 @@ from typing import Final
import litellm
from litellm._logging import verbose_logger
from litellm.exceptions import UnsupportedParamsError
from litellm.litellm_core_utils.core_helpers import as_str_mapping
from litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfig
from litellm.router_utils.reasoning_effort_capability import declared_reasoning_efforts_for_model
@ -56,12 +57,16 @@ class ClfAiGatewayConfig(OpenAIGPTConfig):
requested_mapping: Final = as_str_mapping(requested)
effort: Final = requested_mapping.get("effort") if requested_mapping is not None else requested
declared: Final = declared_reasoning_efforts_for_model(model, "clf_ai_gateway")
drops_effort: Final = (
(drop_params or litellm.drop_params is True)
and effort is not None
and declared is not None
and effort not in declared
)
drop_enabled: Final = drop_params or litellm.drop_params is True
if requested_mapping is not None and effort is None and not drop_enabled:
raise UnsupportedParamsError(
message=f"clf_ai_gateway needs an effort level in reasoning_effort, got {requested!r}. To drop "
"unsupported openai params from the call, set `litellm.drop_params = True`",
llm_provider="clf_ai_gateway",
model=model,
status_code=400,
)
drops_effort: Final = drop_enabled and effort is not None and declared is not None and effort not in declared
if drops_effort:
verbose_logger.warning(
"Dropping reasoning_effort=%r for model=%r (drop_params=True) because clf_ai_gateway takes only %s for it.",
@ -70,7 +75,7 @@ class ClfAiGatewayConfig(OpenAIGPTConfig):
declared,
)
others: Final = {param: value for param, value in non_default_params.items() if param != "reasoning_effort"}
normalized: Final = {**others, "reasoning_effort": effort} if effort is not None else non_default_params
normalized: Final = {**others, "reasoning_effort": effort} if effort is not None else others
forwarded: Final = others if drops_effort else normalized
mapped: Final[dict[str, object]] = super().map_openai_params( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # OpenAIGPTConfig types its params and result as bare dicts # mutable-ok: parent returns a dict
non_default_params=forwarded,

View file

@ -180,6 +180,29 @@ def test_drop_params_drops_a_reasoning_effort_level_the_model_does_not_declare(
assert json.loads(route.calls.last.request.content).get("reasoning_effort") == forwarded
def test_a_reasoning_effort_dict_without_a_level_is_refused_unless_dropped(local_model_cost_map: None) -> None:
with respx.mock() as upstream:
route: Final = upstream.post(CHAT_URL).respond(
200, json=_chat_response({"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2})
)
with pytest.raises(litellm.UnsupportedParamsError):
litellm.completion(
model="clf_ai_gateway/glm-5.3",
api_key="fake-test-key",
messages=[{"role": "user", "content": "hi"}],
reasoning_effort={"summary": "auto"},
)
litellm.completion(
model="clf_ai_gateway/glm-5.3",
api_key="fake-test-key",
messages=[{"role": "user", "content": "hi"}],
reasoning_effort={"summary": "auto"},
drop_params=True,
)
assert route.call_count == 1
assert "reasoning_effort" not in json.loads(route.calls.last.request.content)
def test_reasoning_effort_is_advertised_only_for_reasoning_models(local_model_cost_map: None) -> None:
config: Final = ClfAiGatewayConfig()
assert "reasoning_effort" in config.get_supported_openai_params(model="glm-5.3")