From 6a466913fc855e1bb3b3ea5a3dbac1db9ab6dae4 Mon Sep 17 00:00:00 2001 From: Chesars Date: Sun, 22 Mar 2026 18:36:13 -0300 Subject: [PATCH] fix: map Zhipu GLM non-standard finish_reason values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zhipu GLM returns non-standard finish_reason values during streaming when inference fails mid-request, causing Pydantic validation crash: - "network_error" (inference interrupted) → map to "stop" - "sensitive" (content policy violation) → map to "content_filter" Fixes #23386 --- litellm/litellm_core_utils/core_helpers.py | 3 +++ .../test_litellm/litellm_core_utils/test_core_helpers.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 22006be21a..0f0d6631ad 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -89,6 +89,9 @@ _FINISH_REASON_MAP: dict[str, OpenAIChatCompletionFinishReason] = { "IMAGE_PROHIBITED_CONTENT": "content_filter", "TOO_MANY_TOOL_CALLS": "stop", "MALFORMED_RESPONSE": "stop", + # Zhipu GLM + "network_error": "stop", + "sensitive": "content_filter", # Bedrock "guardrail_intervened": "content_filter", # OpenAI passthrough diff --git a/tests/test_litellm/litellm_core_utils/test_core_helpers.py b/tests/test_litellm/litellm_core_utils/test_core_helpers.py index 8397fc2224..0519ac6a8f 100644 --- a/tests/test_litellm/litellm_core_utils/test_core_helpers.py +++ b/tests/test_litellm/litellm_core_utils/test_core_helpers.py @@ -126,6 +126,14 @@ class TestMapFinishReasonBedrock: assert map_finish_reason("guardrail_intervened") == "content_filter" +class TestMapFinishReasonZhipu: + def test_network_error(self): + assert map_finish_reason("network_error") == "stop" + + def test_sensitive(self): + assert map_finish_reason("sensitive") == "content_filter" + + class TestMapFinishReasonOpenAIPassthrough: @pytest.mark.parametrize( "reason", ["stop", "length", "tool_calls", "function_call", "content_filter"]