Skip to content

fix(mistral): treat model_length as a context window finish reason - #477

Open
pcbeingused333 wants to merge 3 commits into
posit-dev:mainfrom
pcbeingused333:fix/mistral-model-length-finish
Open

pcbeingused333 wants to merge 3 commits into
posit-dev:mainfrom
pcbeingused333:fix/mistral-model-length-finish

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Mistral ends a response with finish_reason: "model_length" when the request reaches the model's context length. normalize_finish_reason in the Chat Completions provider, which ChatMistral() uses, did not know the value and passed the raw string through. chatlas therefore did not warn that the response was truncated, and .chat_structured() tried to parse the cut-off JSON instead of failing with the truncation message, which is what happens for the other providers' context-window reasons.

model_length now maps to "context_window", the value Anthropic and Bedrock already use for the same situation.

Tests. test_normalize_finish_reason_maps_known_reasons covers the mapping, and test_model_length_finish_reason_warns_about_the_context_window checks that a model_length completion becomes a context_window turn and raises the context-window error when a data model was requested. Both fail on main and pass here, and the rest of tests/test_provider_openai_completions.py and tests/test_provider_mistral.py pass. No request was made to the Mistral API.

This change was made with Claude Code.

🤖 Generated with Claude Code

pcbeingused333 and others added 3 commits October 21, 2026 04:56
Mistral stops a response with finish_reason model_length when the request
reaches the model's context length. It was passed through as an unknown raw
string, so chatlas neither warned about the truncated response nor refused to
parse truncated structured data. Map it to "context_window".

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	CHANGELOG.md
#	tests/test_provider_openai_completions.py

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant