fix: map Zhipu GLM non-standard finish_reason values

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
This commit is contained in:
Chesars 2026-03-22 18:36:13 -03:00
parent f5194b5ce3
commit 6a466913fc
2 changed files with 11 additions and 0 deletions

View File

@ -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

View File

@ -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"]