Skip to content

feat(mistralai): native content-block streaming events#38022

Open
Nick Hollon (nick-hollon-lc) wants to merge 1 commit into
nh/native-content-block-streamingfrom
nh/native-content-block-streaming-mistralai
Open

feat(mistralai): native content-block streaming events#38022
Nick Hollon (nick-hollon-lc) wants to merge 1 commit into
nh/native-content-block-streamingfrom
nh/native-content-block-streaming-mistralai

Conversation

@nick-hollon-lc

Copy link
Copy Markdown
Contributor

Stacked on #37995 (core BlockStreamTracker + Anthropic), which it branches from. It depends only on the core primitive, not the OpenAI PRs, so it's a sibling of that chain. Retargets to master once #37995 lands.

Gives ChatMistralAI native _stream_chat_model_events / _astream_chat_model_events hooks so stream_events(version="v3") maps Mistral's stream directly to content-block events.

How it works

A bespoke converter (convert_mistral_stream / async twin) mirrors _stream exactly — threading (index, index_type, default_class) through the existing _convert_chunk_to_message_chunk helper (injected to avoid a circular import) and skipping no-choices chunks — then feeds each resulting AIMessageChunk's content blocks into the shared BlockStreamTracker. This surfaces text, tool calls, and (under output_version="v1") reasoning blocks, accumulates usage, and labels model_provider as "mistralai". message-start carries the stream's LangChain run id threaded from core, consistent with the other native paths and the bridge.

Worth careful review

  • The converter is a faithful, parity-clean twin of _stream/_astream (same index/index_type threading and no-choices skip), so it can't double-count blocks or drop the usage that Mistral attaches to a content chunk.
  • message_id is keyword-only on the hooks, kept out of the request **kwargs.
  • Tests cover the lifecycle (text accumulation, tool-call finalization, usage) and a v1 case asserting the thinking → text type change advances into distinct reasoning and text blocks; model-level tests drive stream_events(version="v3") through the native hook. Live-model reasoning specifics are covered by the existing magistral integration test.

@github-actions github-actions Bot added feature For PRs that implement a new feature; NOT A FEATURE REQUEST integration PR made that is related to a provider partner package integration internal mistralai `langchain-mistralai` package issues & PRs size: L 500-999 LOC labels Jun 10, 2026

@open-swe open-swe 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.

Open SWE Review found 2 potential issues.

Open in WebView Open SWE trace

Comment thread libs/partners/mistralai/langchain_mistralai/_stream_events.py
Comment on lines +97 to +98
for key, block in iter_protocol_blocks(new_chunk):
yield from tracker.feed(key, block)

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.

🟠 Unindexed tool calls overwrite text blocks

This feeds Mistral chunks into BlockStreamTracker using the generic iter_protocol_blocks keys. For the text-then-tool stream added in the tests, neither the text blocks nor the tool-call chunk carry an index, so iter_protocol_blocks gives both source key 0. The tracker then treats the tool call as an update to the already-open text block: no content-block-start is emitted for the tool call, the accumulated text is replaced, and content-block-finish only returns the tool call. Users streaming a response that contains text before an unindexed tool call lose the finalized text block and get an inconsistent block lifecycle. The native converter should assign stable distinct keys for Mistral text vs tool-call chunks (or ensure tool calls get their own index) before feeding the tracker.

(Refers to lines 97-98)


Your feedback helps Open SWE learn. React with 👍 or 👎 to tell us if this review comment was useful.

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

Labels

feature For PRs that implement a new feature; NOT A FEATURE REQUEST integration PR made that is related to a provider partner package integration internal mistralai `langchain-mistralai` package issues & PRs size: L 500-999 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant