pi_llm.utils.event_stream¶
event_stream
¶
Push-based async iterable event stream.
Provides EventStream, a generic producer/consumer bridge for streaming
LLM responses as typed events.
EventStream(is_complete, extract_result)
¶
Bases: Generic[T, R]
Push-based async iterable stream with a final result.
The producer calls push(event) to emit events and end() to close
the stream. The consumer uses async for event in stream to iterate
over events and await stream.result() to get the final result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_complete
|
Callable[[T], bool]
|
Predicate that returns |
required |
extract_result
|
Callable[[T], R]
|
Extracts the final result from the terminal event. |
required |
push(event)
¶
Push an event into the stream. Silently drops events after end().
end(result=None)
¶
Signal end of stream. Sets result if not already set.
__aiter__()
async
¶
Async iterate over events until sentinel.
result()
async
¶
Await the final result. Resolves on completion event or end(result).
AssistantMessageEventStream()
¶
Bases: EventStream[AssistantMessageEvent, AssistantMessage]
Event stream specialized for assistant message streaming.
create_assistant_message_event_stream()
¶
Factory function for AssistantMessageEventStream.