Skip to content

Python: fix Mistral AI chat completion crash on unknown finish reasons (model_length) - #14696

Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/mistral-model-length-c0879
Open

alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/mistral-model-length-c0879

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Motivation and Context

MistralAIChatCompletion builds FinishReason(choice.finish_reason) for every choice. FinishReason only defines stop, length, content_filter, tool_calls and function_call, but the Mistral SDK also returns model_length (the model's context window was reached) and error. For those valid responses, both get_chat_message_contents and get_streaming_chat_message_contents raised ValueError: 'model_length' is not a valid FinishReason.

Description

  • Added _get_finish_reason in mistral_ai_chat_completion.py: model_length maps to FinishReason.LENGTH, any other value that FinishReason does not define maps to None (logged at debug), and known values behave as before. Both the non-streaming and streaming paths use it.
  • Tests (parametrized, non-streaming and streaming): model_length -> LENGTH, an unknown value -> None, length unchanged. They fail on main and pass with the change.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the SK Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • I didn't break anyone

Disclosure

This change was prepared with AI assistance (Claude Code).

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.
Copilot AI balanced review requested due to automatic review settings October 31, 2026 03:53
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Oct 31, 2026

Copilot AI left a comment

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.

🟢 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_length to FinishReason.LENGTH and returns None for 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 length remains 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

1 active deployment
github-app-auth — 22717d0f Deployed Oct 31, 2026 by pcbeingused333 via add_label #29516
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants