Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions litellm/litellm_core_utils/core_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ def map_finish_reason(
return "tool_calls"
elif finish_reason == "compaction":
return "length"
# Unknown finish_reason values (e.g. provider-specific error codes like
# "network_error" from ZhipuAI/GLM) are not in OpenAIChatCompletionFinishReason
# Literal and will cause a Pydantic ValidationError in Choices.__init__.
# Map them to "finish_reason_unspecified" so the stream can be assembled
# without raising an exception.
_valid_finish_reasons = {
"stop",
"content_filter",
"function_call",
"tool_calls",
"length",
"guardrail_intervened",
"eos",
"finish_reason_unspecified",
"malformed_function_call",
}
if finish_reason not in _valid_finish_reasons:
return "finish_reason_unspecified"
Comment thread
RheagalFire marked this conversation as resolved.
Resolved
Comment on lines +105 to +116

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider logging unknown finish reasons

Silently mapping unknown values to "finish_reason_unspecified" is the right approach to prevent the ValidationError, but it may hide useful debugging information. Consider adding a verbose_logger.warning() (already imported) when an unknown finish reason is encountered so operators can track which providers are returning non-standard values:

Suggested change
# Unknown finish_reason values (e.g. provider-specific error codes like
# "network_error" from ZhipuAI/GLM-5) are not in OpenAIChatCompletionFinishReason
# Literal and will cause a Pydantic ValidationError in Choices.__init__.
# Map them to "finish_reason_unspecified" so the stream can be assembled
# without raising an exception.
if finish_reason not in _VALID_OPENAI_FINISH_REASONS:
return "finish_reason_unspecified"
# Unknown finish_reason values (e.g. provider-specific error codes like
# "network_error" from ZhipuAI/GLM-5) are not in OpenAIChatCompletionFinishReason
# Literal and will cause a Pydantic ValidationError in Choices.__init__.
# Map them to "finish_reason_unspecified" so the stream can be assembled
# without raising an exception.
if finish_reason not in _VALID_OPENAI_FINISH_REASONS:
verbose_logger.warning("Unknown finish_reason '%s' mapped to 'finish_reason_unspecified'", finish_reason)
return "finish_reason_unspecified"

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

return finish_reason


Expand Down
Loading