Repository navigation
Commit 8cda8fe
authored
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
- tests/unit/connectors/ai/mistral_ai/services
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
198 | 215 | | |
199 | 216 | | |
200 | 217 | | |
| |||
213 | 230 | | |
214 | 231 | | |
215 | 232 | | |
216 | | - | |
| 233 | + | |
217 | 234 | | |
218 | 235 | | |
219 | 236 | | |
| |||
238 | 255 | | |
239 | 256 | | |
240 | 257 | | |
241 | | - | |
| 258 | + | |
242 | 259 | | |
243 | 260 | | |
244 | 261 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
453 | 454 | | |
454 | 455 | | |
455 | 456 | | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
456 | 544 | | |
457 | 545 | | |
458 | 546 | | |
| |||
0 commit comments