Principle:Mistralai Client python Stream Event Processing
| Knowledge Sources | |
|---|---|
| Domains | Streaming, Event_Processing |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
An event stream consumption pattern that iterates over Server-Sent Events to extract incremental response data from a streaming API connection.
Description
Stream Event Processing is the pattern of iterating over an open SSE connection to process individual events as they arrive. Each event contains a data payload (CompletionChunk) with a partial response. The consumer must handle the delta content, detect the finish condition, and aggregate tokens if a full response is needed. The stream implements Python's iterator protocol (__iter__ / __aiter__) for natural loop-based consumption.
Usage
Use this principle when consuming streaming responses from chat.stream() or chat.stream_async(). The EventStream wrapper handles SSE parsing, JSON deserialization, and Pydantic model creation for each chunk.
Theoretical Basis
SSE event processing follows the iterator pattern:
- The EventStream reads raw bytes from the HTTP response
- Lines are split by \\n\\n (SSE event boundary)
- Each data: line is JSON-parsed into a typed event object
- The iterator yields events until the stream ends or an error occurs
- [DONE] sentinel or connection close signals stream end