Skip to content

fix(mistralai): raise when a stream ends before conversation.response.done - #8800

Open
ANDROIDHASSAN wants to merge 1 commit into
livekit:mainfrom
ANDROIDHASSAN:fix/mistralai-stream-end-before-response-done
Open

ANDROIDHASSAN wants to merge 1 commit into
livekit:mainfrom
ANDROIDHASSAN:fix/mistralai-stream-end-before-response-done

Conversation

@ANDROIDHASSAN

Copy link
Copy Markdown
Contributor

Problem

mistralai.LLMStream._run treats the end of the Conversations event stream as a finished reply, but nothing checks that conversation.response.done arrived. The SDK's SSE reader (mistralai.client.utils.eventstreaming, no sentinel on start_stream_async) just stops iterating when the body runs out. So:

  • a reply cut after some text is delivered as if it were complete;
  • an empty body counts as a successful turn and is not retried;
  • a function call that was still streaming is flushed after the loop and dispatched with truncated arguments.

I ran the real mistralai 3.5.0 client against a local HTTP server that ends the event stream at different points:

Response body start_stream_async
ends cleanly between two events ends quietly, events ['conversation.response.started', 'message.output.delta']
ends cleanly inside an event ends quietly, the partial event is dropped
ends cleanly with no events ends quietly

And through the plugin on main, a stream of message.output.delta + a function.call.delta with arguments='{"ci', then end of body:

TEXT: partial
TOOL CALL EMITTED: get_weather '{"ci'
stream finished with no error

#8705 fixed the same gap in the OpenAI Responses engine; #8728 (Anthropic) and #8787 (Bedrock) are the open counterparts.

Fix

  • Raise APIConnectionError if the stream ends before ResponseDoneEvent, before the pending function calls are flushed. It uses the existing retryable flag, so it is retried only while nothing has reached the caller.
  • Let that error pass through the generic except Exception instead of being wrapped again.

conversation.response.error already raises from _parse_event and is unaffected. pending_fnc_calls is local to each attempt, so a retry cannot leak the cut call.

Tests

uv run pytest --plugin mistralai tests/test_plugin_mistralai_llm.py -q gives 16 passed.

  • test_stream_ending_before_response_done_is_retried: conversation.response.started alone, 3 attempts with max_retry=2, then raises.
  • test_stream_ending_before_response_done_after_output_is_an_error: text then a partial function call, 1 attempt, the text is kept, no tool call is emitted, raises with retryable=False.

With llm.py from main, both fail with DID NOT RAISE APIConnectionError.

The existing fakes ended without conversation.response.done, which the real API always sends, so _no_events and test_thinking_chunks_are_not_emitted now end with one. That is the only change to existing tests.

Also ran:

  • ruff format --check and ruff check on both files: clean.
  • mypy (-p livekit.agents -p livekit.plugins.mistralai): no issues in 252 source files.
  • uv run pytest --unit --audio_eot -q: 7552 passed, 4 skipped. One earlier run on the same diff had a single failure outside the Mistral plugin, and the next full run passed.

Scope

  • Behaviour change: a reply cut after text now ends in a non-retryable error instead of ending silently, the same as every other error after output in this _run, and the same as fix(openai): raise when a Responses HTTP stream ends before a terminal eventΒ #8705's default.
  • On a truncated stream the usage chunk is not emitted, the same as any other mid-stream error.
  • Only livekit-plugins-mistralai/.../llm.py and its test file change.

….done

The mistralai SDK stops iterating without an error when the SSE body
ends, so a stream cut before conversation.response.done was treated as
a complete reply, and a function call still being streamed was
dispatched with truncated arguments. Raise APIConnectionError instead,
retryable only while nothing has reached the caller.
@ANDROIDHASSAN
ANDROIDHASSAN requested a review from a team as a code owner November 26, 2026 18:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: βœ… Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-11-26T18:54:57.436387Z 0a414e2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with πŸ‘€ while any review is running, comments if it has suggestions, and reacts with πŸ‘ once all reviews finish with no findings.

@devin-ai-integration devin-ai-integration Bot 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.

πŸ” Devin Review: 1 flag

Not posted on this PR by your GitHub settings β€” view it in Devin Review. (Configure)

Devin Review

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