Repository navigation
Python: fix Mistral AI chat completion crash on unknown finish reasons (model_length) - #14696
Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
alexcastgonz (pcbeingused333) wants to merge 1 commit into
Conversation
Mistral returns finish_reason values such as model_length that FinishReason does not define, so FinishReason(choice.finish_reason) raised ValueError for valid responses in both the non-streaming and streaming paths. model_length now maps to LENGTH and any other unknown value to None.
alexcastgonz (pcbeingused333)
deployed
to
github-app-auth
October 31, 2026 03:53 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the Mistral AI finish-reason handling update.
No open issues 🎉
What changed in this PR
This PR prevents Semantic Kernel’s Mistral AI chat connector from raising ValueError when responses contain finish reasons outside the shared FinishReason enum.
Changes:
- Maps
model_lengthtoFinishReason.LENGTHand returnsNonefor unknown values, with debug logging. - Uses the mapping for both streaming and non-streaming responses.
- Adds parametrized regression tests for both paths, plus a non-streaming check that
lengthremains unchanged.
| File | Description |
|---|---|
python/tests/unit/connectors/ai/mistral_ai/services/test_mistralai_chat_completion.py |
Adds finish-reason mapping regression tests. |
python/semantic_kernel/connectors/ai/mistral_ai/services/mistral_ai_chat_completion.py |
Centralizes safe finish-reason conversion for both response paths. |
Review effort: Balanced
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
MistralAIChatCompletionbuildsFinishReason(choice.finish_reason)for every choice.FinishReasononly definesstop,length,content_filter,tool_callsandfunction_call, but the Mistral SDK also returnsmodel_length(the model's context window was reached) anderror. For those valid responses, bothget_chat_message_contentsandget_streaming_chat_message_contentsraisedValueError: 'model_length' is not a valid FinishReason.Description
_get_finish_reasoninmistral_ai_chat_completion.py:model_lengthmaps toFinishReason.LENGTH, any other value thatFinishReasondoes not define maps toNone(logged at debug), and known values behave as before. Both the non-streaming and streaming paths use it.model_length->LENGTH, an unknown value ->None,lengthunchanged. They fail on main and pass with the change.Contribution Checklist
Disclosure
This change was prepared with AI assistance (Claude Code).