Repository navigation
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 1 commit
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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 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.