Streaming¶
Helpers for consuming a flow as a stream of events and adapting stream chunks to SSE or WebSocket transports.
The StreamChunk payload type itself is documented under
Messages & types.
streaming
¶
Utilities for streaming chunk integrations.
stream_flow
async
¶
stream_flow(flow: PenguiFlow, parent_msg: Message, *, to: Node | Sequence[Node] | None = None, timeout: float | None = None, include_final: bool = False) -> AsyncIterator[Any]
Yield streamed payloads from a running flow.
This helper emits each StreamChunk produced downstream. When include_final
is True the first non-chunk payload encountered after a terminal chunk is also
yielded before the generator stops. The caller is responsible for stopping the flow
when finished.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
PenguiFlow
|
The running |
required |
parent_msg
|
Message
|
The message to emit as the starting point of the run. |
required |
to
|
Node | Sequence[Node] | None
|
Optional node or sequence of nodes to route |
None
|
timeout
|
float | None
|
Optional per-fetch timeout in seconds; raises on expiry via
|
None
|
include_final
|
bool
|
When |
False
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[Any]
|
Each |
AsyncIterator[Any]
|
payload when |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If |
emit_stream_events
async
¶
emit_stream_events(source: AsyncIterable[Any], ctx: Context, parent_msg: Message, *, adapter: EventAdapter | None = None, to: Node | Sequence[Node] | None = None, final_meta: dict[str, Any] | None = None) -> None
Bridge an async iterable of provider events into StreamChunk emissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
AsyncIterable[Any]
|
An async iterable of provider-specific event objects. |
required |
ctx
|
Context
|
The |
required |
parent_msg
|
Message
|
The parent message associated with the emitted chunks. |
required |
adapter
|
EventAdapter | None
|
Optional callable converting a provider event into a
|
None
|
to
|
Node | Sequence[Node] | None
|
Optional node or sequence of nodes to route emitted chunks to. |
None
|
final_meta
|
dict[str, Any] | None
|
Metadata to attach to a synthesized terminal chunk if the source
completes without ever emitting a chunk with |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None. Chunks are emitted as a side effect via |
chunk_to_ws_json
¶
chunk_to_ws_json(chunk: StreamChunk, *, extra: dict[str, Any] | None = None) -> str
Serialize a StreamChunk as a JSON WebSocket payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
StreamChunk
|
The stream chunk to serialize. |
required |
extra
|
dict[str, Any] | None
|
Additional key/value pairs to merge into the JSON payload. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A JSON-encoded string with |
str
|
|
format_sse_event
¶
format_sse_event(chunk: StreamChunk, *, event_name: str | None = None, retry_ms: int | None = None) -> str
Render a StreamChunk as an SSE event string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
StreamChunk
|
The stream chunk to render. |
required |
event_name
|
str | None
|
Optional SSE |
None
|
retry_ms
|
int | None
|
Optional SSE |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A newline-terminated SSE event string (including the trailing blank line). |