Skip to content

Commit 8cda8fe

Browse files
Python: fix Mistral AI chat completion crash on unknown finish reasons (model_length) (#14696)
### 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 - [x] The code builds clean without any errors or warnings - [x] The PR follows the SK Contribution Guidelines - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone ### Disclosure This change was prepared with AI assistance (Claude Code).
1 parent 457e12b commit 8cda8fe

2 files changed

Lines changed: 107 additions & 2 deletions

File tree

‎python/semantic_kernel/connectors/ai/mistral_ai/services/mistral_ai_chat_completion.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,23 @@ async def _inner_get_streaming_chat_message_contents(
195195

196196
# region content conversion to SK
197197

198+
@staticmethod
199+
def _get_finish_reason(finish_reason: str | None) -> FinishReason | None:
200+
"""Map a Mistral finish reason to a FinishReason.
201+
202+
Mistral can return values that FinishReason does not define (e.g. `model_length`, which
203+
means the model's context window was reached), so those are mapped or ignored instead of raising.
204+
"""
205+
if not finish_reason:
206+
return None
207+
if finish_reason == "model_length":
208+
return FinishReason.LENGTH
209+
try:
210+
return FinishReason(finish_reason)
211+
except ValueError:
212+
logger.debug(f"Unknown finish reason from Mistral AI: {finish_reason}")
213+
return None
214+
198215
def _create_chat_message_content(
199216
self, response: ChatCompletionResponse, choice: ChatCompletionChoice, response_metadata: dict[str, Any]
200217
) -> "ChatMessageContent":
@@ -213,7 +230,7 @@ def _create_chat_message_content(
213230
metadata=metadata,
214231
role=AuthorRole(choice.message.role),
215232
items=items,
216-
finish_reason=FinishReason(choice.finish_reason) if choice.finish_reason else None,
233+
finish_reason=self._get_finish_reason(choice.finish_reason),
217234
)
218235

219236
def _create_streaming_chat_message_content(
@@ -238,7 +255,7 @@ def _create_streaming_chat_message_content(
238255
ai_model_id=self.ai_model_id,
239256
metadata=metadata,
240257
role=AuthorRole(choice.delta.role) if choice.delta.role else AuthorRole.ASSISTANT,
241-
finish_reason=FinishReason(choice.finish_reason) if choice.finish_reason else None,
258+
finish_reason=self._get_finish_reason(choice.finish_reason),
242259
items=items,
243260
function_invoke_attempt=function_invoke_attempt,
244261
)

‎python/tests/unit/connectors/ai/mistral_ai/services/test_mistralai_chat_completion.py‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from semantic_kernel.contents.chat_message_content import ChatMessageContent
3131
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
3232
from semantic_kernel.contents.utils.author_role import AuthorRole
33+
from semantic_kernel.contents.utils.finish_reason import FinishReason
3334
from semantic_kernel.exceptions import (
3435
ServiceInitializationError,
3536
ServiceInvalidExecutionSettingsError,
@@ -453,6 +454,93 @@ async def test_mistral_ai_chat_completion_get_chat_message_contents_success():
453454
async_mock_client.chat.complete_async.assert_awaited_once()
454455

455456

457+
@pytest.mark.parametrize(
458+
("mistral_finish_reason", "expected"),
459+
[
460+
("model_length", FinishReason.LENGTH),
461+
("length", FinishReason.LENGTH),
462+
("something_new", None),
463+
],
464+
)
465+
async def test_mistral_ai_chat_completion_finish_reason_mapping(mistral_finish_reason, expected):
466+
"""Finish reasons FinishReason does not define must not raise."""
467+
mock_response = ChatCompletionResponse(
468+
id="some_id",
469+
object="object",
470+
created=12345,
471+
usage=UsageInfo(prompt_tokens=10, completion_tokens=20, total_tokens=30),
472+
model="test-model",
473+
choices=[
474+
ChatCompletionChoice(
475+
index=0,
476+
message=AssistantMessage(role="assistant", content="Hello!"),
477+
finish_reason=mistral_finish_reason,
478+
)
479+
],
480+
)
481+
482+
async_mock_client = MagicMock(spec=Mistral)
483+
async_mock_client.chat = MagicMock()
484+
async_mock_client.chat.complete_async = AsyncMock(return_value=mock_response)
485+
486+
chat_completion = MistralAIChatCompletion(
487+
ai_model_id="test-model",
488+
api_key="test_key",
489+
async_client=async_mock_client,
490+
)
491+
492+
results = await chat_completion.get_chat_message_contents(ChatHistory(), MistralAIChatPromptExecutionSettings())
493+
494+
assert results[0].finish_reason == expected
495+
496+
497+
@pytest.mark.parametrize(
498+
("mistral_finish_reason", "expected"),
499+
[
500+
("model_length", FinishReason.LENGTH),
501+
("something_new", None),
502+
],
503+
)
504+
async def test_mistral_ai_chat_completion_streaming_finish_reason_mapping(mistral_finish_reason, expected):
505+
"""Streaming finish reasons FinishReason does not define must not raise."""
506+
mock_chunk = CompletionEvent(
507+
data=CompletionChunk(
508+
id="chunk1",
509+
created=1,
510+
model="test-model",
511+
choices=[
512+
CompletionResponseStreamChoice(
513+
index=0,
514+
delta=DeltaMessage(role="assistant", content="Hello"),
515+
finish_reason=mistral_finish_reason,
516+
)
517+
],
518+
)
519+
)
520+
521+
async def mock_stream_async(**kwargs):
522+
yield mock_chunk
523+
524+
async_mock_client = MagicMock(spec=Mistral)
525+
async_mock_client.chat = MagicMock()
526+
async_mock_client.chat.stream_async = AsyncMock(return_value=mock_stream_async())
527+
528+
chat_completion = MistralAIChatCompletion(
529+
ai_model_id="test-model",
530+
api_key="test_key",
531+
async_client=async_mock_client,
532+
)
533+
534+
chunks = [
535+
chunk_list
536+
async for chunk_list in chat_completion.get_streaming_chat_message_contents(
537+
ChatHistory(), MistralAIChatPromptExecutionSettings()
538+
)
539+
]
540+
541+
assert chunks[0][0].finish_reason == expected
542+
543+
456544
async def test_mistral_ai_chat_completion_get_chat_message_contents_failure():
457545
"""Test get_chat_message_contents should raise ServiceResponseException if Mistral call fails."""
458546
async_mock_client = MagicMock(spec=Mistral)

0 commit comments

Comments
 (0)