Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Mistralai Client python Streaming Chat Completion

From Leeroopedia
Revision as of 11:05, 16 February 2026 by Admin (talk | contribs) (Auto-imported from workflows/Mistralai_Client_python_Streaming_Chat_Completion.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains LLMs, Chat_Completion, Streaming, Python_SDK
Last Updated 2026-02-15 14:00 GMT

Overview

End-to-end process for streaming chat completion responses token-by-token from the Mistral AI API using Server-Sent Events (SSE).

Description

This workflow demonstrates how to receive model-generated text incrementally as it is produced, rather than waiting for the full response. The SDK uses Server-Sent Events to deliver tokens as they become available, enabling real-time display of model output. The streaming response is exposed as a Python generator that can be consumed with a for loop and used as a context manager for proper resource cleanup.

Usage

Execute this workflow when building interactive applications that benefit from displaying model output in real-time, such as chat interfaces, live coding assistants, or any scenario where perceived latency should be minimized. Streaming is particularly valuable for long-form generation where the user should see progress incrementally.

Execution Steps

Step 1: Install SDK and Configure Authentication

Install the mistralai Python package and set up the MISTRAL_API_KEY environment variable for authentication. The streaming API uses the same authentication mechanism as non-streaming requests.

Key considerations:

  • Same installation and authentication as non-streaming workflow
  • Streaming requires no additional dependencies

Step 2: Initialize the Mistral Client

Create a Mistral client instance using the context manager pattern. The client manages the underlying HTTP connections that will be used for the streaming SSE connection.

Key considerations:

  • Context manager pattern is especially important for streaming to ensure connections are properly closed
  • The client handles both sync and async streaming

Step 3: Construct the Message Payload

Build the conversation messages list with role and content for each message. The message format is identical to non-streaming chat completion.

Key considerations:

  • Message structure is the same as non-streaming requests
  • All message types (system, user, assistant, tool) are supported

Step 4: Open the Streaming Request

Call the chat.stream() method (sync) or chat.stream_async() method (async) with the model, messages, and optional generation parameters. This returns an EventStream generator object rather than a completed response.

Key considerations:

  • The stream method returns an EventStream generator, not a complete response
  • Use the EventStream as a context manager (with statement) for proper cleanup
  • The SSE connection stays open until the model finishes generating or the context is exited

Step 5: Consume Stream Chunks

Iterate over the EventStream using a for loop. Each chunk contains a CompletionEvent with delta content representing the incremental tokens generated by the model. Concatenate or display each delta to build the full response.

Key considerations:

  • Each chunk has structure: chunk.data.choices[0].delta.content
  • The delta content may be None for non-content events (e.g., tool calls)
  • The stream terminates automatically when the model finishes (data: [DONE] event)
  • Print with end="" to display tokens as a continuous stream

Step 6: Finalize and Close

After the stream is exhausted, the context manager automatically closes the underlying SSE connection and releases resources. Optionally aggregate the full response from collected chunks for further processing.

Key considerations:

  • The context manager handles cleanup automatically
  • If you need the complete response text, concatenate delta.content values during iteration
  • Usage statistics may be available in the final chunk

Execution Diagram

GitHub URL

Workflow Repository