-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
fix(streaming): map unknown finish_reason values to finish_reason_unspecified to prevent ValidationError in stream_chunk_builder #22673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,11 @@ | ||||||||||||||||||||||||||||||||
| # What is this? | ||||||||||||||||||||||||||||||||
| ## Helper utilities | ||||||||||||||||||||||||||||||||
| from typing import TYPE_CHECKING, Any, Iterable, List, Literal, Optional, Union | ||||||||||||||||||||||||||||||||
| from typing import TYPE_CHECKING, Any, Iterable, List, Literal, Optional, Union, get_args | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import httpx | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| from litellm._logging import verbose_logger | ||||||||||||||||||||||||||||||||
| from litellm.types.llms.openai import AllMessageValues | ||||||||||||||||||||||||||||||||
| from litellm.types.llms.openai import AllMessageValues, OpenAIChatCompletionFinishReason | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||||||||||||||||
| from opentelemetry.trace import Span as _Span | ||||||||||||||||||||||||||||||||
|
|
@@ -58,6 +58,12 @@ def safe_divide( | |||||||||||||||||||||||||||||||
| return numerator / denominator | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Module-level constant derived from the source-of-truth Literal type. | ||||||||||||||||||||||||||||||||
| # Avoids recreating the set on every call (map_finish_reason is called per-chunk | ||||||||||||||||||||||||||||||||
| # during streaming) and stays in sync when the Literal is updated. | ||||||||||||||||||||||||||||||||
| _VALID_OPENAI_FINISH_REASONS = frozenset(get_args(OpenAIChatCompletionFinishReason)) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def map_finish_reason( | ||||||||||||||||||||||||||||||||
| finish_reason: str, | ||||||||||||||||||||||||||||||||
| ): # openai supports 5 stop sequences - 'stop', 'length', 'function_call', 'content_filter', 'null' | ||||||||||||||||||||||||||||||||
|
|
@@ -96,6 +102,18 @@ 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-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( | ||||||||||||||||||||||||||||||||
| "litellm.map_finish_reason: unknown finish_reason %r from provider; " | ||||||||||||||||||||||||||||||||
| "mapping to 'finish_reason_unspecified' to avoid ValidationError.", | ||||||||||||||||||||||||||||||||
| finish_reason, | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| return "finish_reason_unspecified" | ||||||||||||||||||||||||||||||||
|
Comment on lines
+105
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider logging unknown finish reasons Silently mapping unknown values to
Suggested change
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 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.