fix(zai): forward every response_format type so allowed_openai_params keeps working

This commit is contained in:
mateo-berri 2026-10-29 10:32:19 -07:00
parent 9cc719df62
commit db34951d38
2 changed files with 6 additions and 38 deletions

View file

@ -2,7 +2,6 @@ from collections.abc import Mapping
from typing import Final
import litellm
from litellm.exceptions import UnsupportedParamsError
from litellm.secret_managers.main import get_secret_str
from litellm.types.llms.openai import AllMessageValues, ChatCompletionToolParam
@ -73,27 +72,11 @@ class ZAIChatConfig(OpenAIGPTConfig):
drop_params: bool,
) -> dict[str, object]: # mutable-ok: request pipeline adds parameters to the returned body
supported_params: Final = self.get_supported_openai_params(model)
response_format: Final = non_default_params.get("response_format")
unsupported_response_format: Final = response_format is not None and not _is_json_mode(response_format)
if unsupported_response_format and not (drop_params or litellm.drop_params):
raise UnsupportedParamsError(
message=f"zai supports only response_format {{'type': 'json_object'}} for model={model}. To drop it, set `litellm.drop_params=True`",
llm_provider="zai",
model=model,
)
return {
**optional_params,
**{
("max_tokens" if param == "max_completion_tokens" else param): value
for param, value in non_default_params.items()
if param in supported_params and not (param == "response_format" and unsupported_response_format)
if param in supported_params
},
}
def _is_json_mode(response_format: object) -> bool:
match response_format:
case {"type": "json_object" | "text"}:
return True
case _:
return False

View file

@ -141,36 +141,21 @@ def test_zai_max_completion_tokens_wins_over_max_tokens(respx_mock, zai_response
assert body["max_tokens"] == 15
def test_zai_rejects_json_schema_response_format(respx_mock, monkeypatch):
monkeypatch.setenv("ZAI_API_KEY", "test-api-key")
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
route = respx_mock.post("https://api.z.ai/api/paas/v4/chat/completions")
with pytest.raises(litellm.UnsupportedParamsError) as error:
completion(
model="zai/glm-5v-turbo",
messages=[{"role": "user", "content": "Ann is 30"}],
response_format=JSON_SCHEMA_RESPONSE_FORMAT,
)
assert error.value.status_code == 400
assert not route.called
def test_zai_drops_json_schema_response_format_under_drop_params(respx_mock, zai_response, monkeypatch):
@pytest.mark.parametrize("pass_through", [{}, {"allowed_openai_params": ["response_format"]}])
def test_zai_forwards_json_schema_response_format_as_sent(respx_mock, zai_response, monkeypatch, pass_through):
monkeypatch.setenv("ZAI_API_KEY", "test-api-key")
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
respx_mock.post("https://api.z.ai/api/paas/v4/chat/completions").respond(json=zai_response)
completion(
model="zai/glm-5v-turbo",
model="zai/glm-5.3",
messages=[{"role": "user", "content": "Ann is 30"}],
response_format=JSON_SCHEMA_RESPONSE_FORMAT,
drop_params=True,
**pass_through,
)
body = json.loads(respx_mock.calls[0].request.content)
assert "response_format" not in body
assert body["response_format"] == JSON_SCHEMA_RESPONSE_FORMAT
def test_zai_glm_5v_turbo_bills_cached_input_at_cache_read_price(local_model_cost_map: None) -> None: