Skip to content

latent.wire.stream

Stream orchestrator for the AI SDK UI data stream protocol.

Drives the message lifecycle (start → content → finish → [DONE]) for a :class:BoundProviderAdapter, guaranteeing termination even on agent crash or client disconnect. Protocol reference: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol

Functions

stream_ui_message

stream_ui_message(messages: list, metadata: dict | None = None, provider: BoundProviderAdapter, config: dict | None = None, max_retries: int = 0) -> AsyncIterator[bytes]

Stream agent responses as the AI SDK UI data stream protocol.

Lifecycle: generate a message id → emit start → stream content parts via the provider → emit finish (or error + finish on failure) → always emit [DONE]. The try/except/finally guarantees termination so a client never waits indefinitely. The caller is responsible for the x-vercel-ai-ui-message-stream: v1 response header.

Args: messages: Full conversation history as a list of Message objects. metadata: Optional metadata merged into the start part. provider: Bound adapter supplying state, raw event iteration, and conversion to normalized events. config: Optional provider/framework config. max_retries: Retries for transient pre-content failures (see _is_retryable).

Yields: Bytes in SSE format (data: {JSON}\n\n) ready to stream to the client.