Implementation:Mistralai Client python Chat Stream
| Knowledge Sources | |
|---|---|
| Domains | NLP, Streaming, LLM_Inference |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Concrete tool for sending streaming chat completion requests and receiving token-by-token responses via SSE provided by the mistralai SDK.
Description
The Chat.stream() and Chat.stream_async() methods open a streaming HTTP connection to the Mistral API and return an EventStream (sync) or EventStreamAsync (async) iterator. Each iteration yields a CompletionEvent containing a CompletionChunk with incremental response data. The stream must be consumed within a context manager (with / async with) to ensure proper resource cleanup.
Usage
Call client.chat.stream() for synchronous streaming or client.chat.stream_async() with async for for async streaming. Always use within a context manager or ensure the stream is properly closed after consumption.
Code Reference
Source Location
- Repository: client-python
- File: src/mistralai/client/chat.py
- Lines: L462-649 (sync stream), L651-838 (async stream)
Signature
class Chat:
def stream(
self,
*,
model: str,
messages: List[ChatCompletionStreamRequestMessage],
temperature: Optional[float] = None,
top_p: Optional[float] = None,
max_tokens: Optional[int] = None,
stream: Optional[bool] = True,
response_format: Optional[ResponseFormat] = None,
tools: Optional[List[Tool]] = None,
safe_prompt: Optional[bool] = None,
random_seed: Optional[int] = None,
) -> EventStream[CompletionEvent]:
...
async def stream_async(
self,
*,
model: str,
messages: List[ChatCompletionStreamRequestMessage],
# Same parameters as stream()
) -> EventStreamAsync[CompletionEvent]:
...
Import
from mistralai import Mistral
# Access via: client.chat.stream(...)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | Yes | Model ID (e.g., "mistral-large-latest") |
| messages | List[ChatCompletionStreamRequestMessage] | Yes | Conversation messages |
| temperature | Optional[float] | No | Sampling temperature |
| max_tokens | Optional[int] | No | Maximum output tokens |
| response_format | Optional[ResponseFormat] | No | Output format |
Outputs
| Name | Type | Description |
|---|---|---|
| stream | EventStream[CompletionEvent] | Iterable SSE stream of completion chunks |
| chunk.data | CompletionChunk | Individual chunk with choices[0].delta.content |
| chunk.data.choices[0].finish_reason | Optional[str] | Signals stream completion |
Usage Examples
Synchronous Streaming
import os
from mistralai import Mistral
from mistralai.models import UserMessage
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
with client.chat.stream(
model="mistral-large-latest",
messages=[UserMessage(content="Write a short poem about coding.")],
) as stream:
for chunk in stream:
content = chunk.data.choices[0].delta.content
if content:
print(content, end="", flush=True)
print() # Final newline
Async Streaming
import asyncio
import os
from mistralai import Mistral
from mistralai.models import UserMessage
async def main():
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
async with client.chat.stream_async(
model="mistral-large-latest",
messages=[UserMessage(content="Explain machine learning.")],
) as stream:
async for chunk in stream:
content = chunk.data.choices[0].delta.content
if content:
print(content, end="", flush=True)
print()
asyncio.run(main())