From 218e7e08ef0b871bd35ee9a4e3502225549d0e37 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:48:16 -0700 Subject: [PATCH] fix(zai): write the prompted JSON schema as JSON so models answer with valid JSON --- litellm/litellm_core_utils/prompt_templates/factory.py | 4 ++-- tests/unit/llms/zai/test_zai_provider.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 14f54f99629..834d26b4fe8 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -5906,8 +5906,8 @@ def default_response_schema_prompt(response_schema: Mapping[str, object]) -> str This is the default prompt. Allow user to override this with a custom_prompt. """ prompt_str: Final = f"""Use this JSON schema: - ```json - {response_schema} + ```json + {json.dumps(response_schema, default=str)} ```""" return prompt_str diff --git a/tests/unit/llms/zai/test_zai_provider.py b/tests/unit/llms/zai/test_zai_provider.py index c0776a223e5..91733798658 100644 --- a/tests/unit/llms/zai/test_zai_provider.py +++ b/tests/unit/llms/zai/test_zai_provider.py @@ -223,7 +223,9 @@ def test_zai_puts_json_schema_in_prompt_for_models_that_ignore_it( assert body["response_format"] == {"type": "json_object"} assert body["messages"][0] == {"role": "user", "content": "Ann likes 7"} assert body["messages"][-1]["role"] == "user" - assert "favorite_number" in body["messages"][-1]["content"] + prompt = body["messages"][-1]["content"] + rendered_schema = prompt[prompt.index("{") : prompt.rindex("}") + 1] + assert json.loads(rendered_schema) == JSON_SCHEMA_RESPONSE_FORMAT["json_schema"]["schema"] def test_zai_glm_5v_turbo_bills_cached_input_at_cache_read_price(local_model_cost_map: None) -> None: