Skip to content

fix: map Zhipu GLM non-standard finish_reason values - #24373

Merged
Chesars merged 1 commit into
BerriAI:litellm_staging_03_22_2026from
Chesars:fix/zhipu-finish-reason-mapping
Mar 22, 2026
Merged

fix: map Zhipu GLM non-standard finish_reason values#24373
Chesars merged 1 commit into
BerriAI:litellm_staging_03_22_2026from
Chesars:fix/zhipu-finish-reason-mapping

Conversation

@Chesars

@Chesars Chesars commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Relevant issues

Fixes #23386

Pre-Submission checklist

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Type

🐛 Bug Fix

Changes

Zhipu GLM returns non-standard finish_reason values during streaming when inference fails mid-request. These values are not in LiteLLM's _FINISH_REASON_MAP, causing a Pydantic validation crash in stream_chunk_builder.

Add two mappings to the centralized _FINISH_REASON_MAP in core_helpers.py:

  • "network_error""stop" (inference interrupted)
  • "sensitive""content_filter" (content policy violation)

Tests added

  • TestMapFinishReasonZhipu::test_network_error
  • TestMapFinishReasonZhipu::test_sensitive

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 BerriAI#23386
@vercel

vercel Bot commented Mar 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Mar 22, 2026 9:38pm

Request Review

@greptile-apps

greptile-apps Bot commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a Pydantic validation crash in stream_chunk_builder by adding two Zhipu GLM-specific finish reason mappings ("network_error""stop" and "sensitive""content_filter") to the centralized _FINISH_REASON_MAP in core_helpers.py.

  • The fix is minimal, targeted, and follows the exact same pattern used for other providers (Cohere, Bedrock, HuggingFace, etc.) already present in _FINISH_REASON_MAP.
  • Both new mappings are semantically correct: "network_error" signals an interrupted inference (effectively a stop), and "sensitive" signals a content policy violation (content_filter).
  • Two unit tests are added that are local-only (no network calls), correctly placed in the mocked test directory, and follow the established test class pattern.
  • One minor concern: the key names ("network_error", "sensitive") are lowercase generic strings rather than Zhipu-namespaced identifiers. "network_error" carries no behavioral risk since it previously fell through to the "stop" default anyway. "sensitive" changes behavior from the "stop" fallback to "content_filter" — semantically more accurate but worth documenting so future contributors know these keys are Zhipu-specific.

Confidence Score: 5/5

  • This PR is safe to merge — it is a two-line additive change to a lookup map with no impact on existing functionality.
  • The change is purely additive (two new dictionary entries), all previously mapped values are unaffected, the fallback behaviour for other providers is unchanged, and the fix is covered by correctly written unit tests with no network calls.
  • No files require special attention.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/core_helpers.py Adds two Zhipu GLM finish reason mappings ("network_error" → "stop", "sensitive" → "content_filter") to the centralized _FINISH_REASON_MAP. Change is minimal and correct; minor concern around generic key names potentially colliding with future providers.
tests/test_litellm/litellm_core_utils/test_core_helpers.py Adds TestMapFinishReasonZhipu class with two unit tests covering the new mappings. Tests are local-only (no network calls), follow the established test pattern, and are correctly placed in the mocked test directory.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Provider stream chunk] --> B{finish_reason in\n_FINISH_REASON_MAP?}
    B -- Yes --> C[Return mapped OpenAI value]
    B -- No --> D[Log warning]
    D --> E[Return 'stop' default]

    subgraph Zhipu GLM new mappings
        F["'network_error'"] --> G["'stop'"]
        H["'sensitive'"] --> I["'content_filter'"]
    end

    C --> J[Pydantic validates\nOpenAIChatCompletionFinishReason]
    J --> K[stream_chunk_builder succeeds]
Loading

Reviews (1): Last reviewed commit: "fix: map Zhipu GLM non-standard finish_r..." | Re-trigger Greptile

Comment thread litellm/litellm_core_utils/core_helpers.py
@codspeed-hq

codspeed-hq Bot commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing Chesars:fix/zhipu-finish-reason-mapping (6a46691) with main (c89496f)

Open in CodSpeed

@Chesars
Chesars changed the base branch from main to litellm_staging_03_22_2026 March 22, 2026 21:41
@Chesars
Chesars merged commit de91bbb into BerriAI:litellm_staging_03_22_2026 Mar 22, 2026
38 of 39 checks passed
@Chesars
Chesars deleted the fix/zhipu-finish-reason-mapping branch March 22, 2026 21:42
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
…-mapping

fix: map Zhipu GLM non-standard finish_reason values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Zhipu GLM returns non-standard finish_reason ("network_error", "sensitive") causing Pydantic crash

1 participant