Skip to content

latent.wire.core.events

Normalized event types for provider-agnostic agent streaming.

This module defines the core event types that all agent providers map to. Events flow through the system in this order:

  1. Raw provider events (e.g., Strands streaming events)
  2. Normalized events (this module) - provider-agnostic representation
  3. AI SDK parts (encoders/) - AI SDK UI protocol format
  4. SSE transport (core/sse.py) - wire protocol

The normalized event types form the contract between providers and encoders. Each provider implements a converter that maps its raw events to these types. Each encoder consumes these normalized events and produces output format.

This design ensures providers are independent and composable.

Classes

DataLatent

DataLatent(name: str, payload: dict = dict())

Carries an AgentEvent verbatim so the codec round-trips it exactly.

On the codec path only TextDelta, ReasoningDelta, StepBoundary and Error map onto native AI SDK parts. Every other AgentEvent — token usage, LLM/retriever spans, journey/phase markers, verification results, guardrail/context violations, AND ToolCall/ToolResult (whose native-part shapes are lossy: reworked arguments, a dropped error, a fabricated orphan call) — rides a single AI SDK custom data part (data-latent-<name>) instead. This makes the ADR-003 D2 / ADR-009 D5 bijection genuinely hold: decode(encode([e])) == [e] for every AgentEvent type, field for field.

Exactly one DataLatent is emitted per such event (never alongside a native part), so the decoder reconstructs the canonical event without double-counting.

Attributes: name: The canonical AgentEvent subclass name (e.g. "Usage"), used by the decoder to look the class up in latent.events (and, for events defined elsewhere, the guardrails/context event modules). payload: The event's serialized fields, from which the original instance is rebuilt.

Error

Error(error_message: str)

Represents an error that occurred during agent processing.

This event is emitted when the agent encounters an error that prevents normal processing from continuing. Errors can occur at any point: - Provider connection failures - LLM inference errors - Tool execution failures - Parsing or validation errors - Timeout or rate limit errors

Error semantics: - Errors are terminal: Once an Error event is emitted, no more content events (text, reasoning, tool) should follow - Cleanup still required: After Error, the stream should emit FinishStep (if a step was active), FinishMessage, and [DONE] to properly close - Partial content is preserved: Any events emitted before the error remain valid and should not be retracted - Error details are included: error_message provides context for debugging

Lifecycle: Error can be emitted at any point in the message stream. It terminates content events but does not terminate the overall message lifecycle—cleanup events (FinishStep, FinishMessage) must still follow.

Invariant: After Error is emitted, no TextDelta, ReasoningDelta, ToolInputDelta, or other content events should follow. Only cleanup events are allowed.

Attributes: error_message: Human-readable description of the error that occurred

FinishStep

FinishStep()

Marks the completion of a processing step in the agent's workflow.

This event is emitted when the agent completes a discrete step in its processing pipeline. It pairs with a preceding StartStep event to bracket a logical phase of execution.

Steps typically end when: - The agent finishes an LLM inference call and transitions to tool usage - A tool execution sequence completes - The agent produces its final response and completes processing

Lifecycle: FinishStep closes the step boundary opened by the preceding StartStep event.

Invariant: Every StartStep must eventually be followed by FinishStep to maintain proper step lifecycle boundaries. This invariant must hold even if an Error event occurred during the step.

Note: Steps are optional in the AI SDK protocol, but if StartStep is emitted, FinishStep must always be emitted.

NormalizedEvent

NormalizedEvent()

Base class for all normalized events.

All events in the system inherit from this class. Using frozen=True ensures events are immutable, which prevents accidental mutations during streaming.

Subclasses add specific fields for each event type (e.g., TextDelta adds 'text_id' and 'delta').

This design provides: - Type safety: Events can be type-checked and validated - Immutability: Safe to pass through async pipelines - Extensibility: New event types inherit the structure

ReasoningDelta

ReasoningDelta(reasoning_id: str, delta: str)

Represents an incremental chunk of reasoning/thinking content.

This event is emitted multiple times to deliver reasoning content incrementally. Each delta contains a fragment of the agent's internal thought process. All ReasoningDelta events for a given reasoning block share the same reasoning_id established by the preceding ReasoningStart event.

The delta field contains raw reasoning text, typically extracted from thinking tags or explicit reasoning events from the agent framework.

Lifecycle: ReasoningDelta events are emitted between ReasoningStart and ReasoningEnd for the same reasoning_id.

Invariant: reasoning_id must match a preceding ReasoningStart event for the same reasoning block.

Attributes: reasoning_id: Unique identifier linking this delta to its reasoning block delta: The incremental reasoning text fragment

ReasoningEnd

ReasoningEnd(reasoning_id: str)

Marks the completion of a reasoning/thinking content block.

This event is emitted once after all ReasoningDelta events for a given reasoning block have been completed. It establishes a clear boundary: no more ReasoningDelta events will follow for this reasoning_id.

A single message may contain multiple reasoning blocks interleaved with text blocks. This allows the event stream to capture multiple thinking → output cycles.

Lifecycle: ReasoningEnd is emitted after the final ReasoningDelta for the reasoning block.

Invariant: If ReasoningStart was emitted for a reasoning_id, ReasoningEnd must eventually be emitted for the same reasoning_id to maintain the Start → Deltas → End lifecycle.

Attributes: reasoning_id: Unique identifier for the reasoning block being completed

ReasoningStart

ReasoningStart(reasoning_id: str)

Marks the beginning of a reasoning/thinking content block.

This event is emitted once before any ReasoningDelta events are sent for a new reasoning block. It provides a unique identifier that will be referenced by all subsequent ReasoningDelta events for this block.

Reasoning blocks represent the agent's internal thought process (as opposed to visible text output). This distinction allows different handling of visible versus thinking content.

Reasoning content typically originates from: - Thinking tags in agent output (e.g., ...) - Explicit reasoning flags from the agent framework - Chain-of-thought steps before producing final output

Lifecycle: ReasoningStart establishes a reasoning_id for the subsequent deltas.

Attributes: reasoning_id: Unique identifier for this reasoning block (e.g., "reasoning_abc123")

StartStep

StartStep(step: int | None = None, label: str | None = None)

Marks the beginning of a processing step in the agent's workflow.

This event is emitted when the agent begins a discrete step in its processing pipeline. Steps represent logical boundaries in the agent's execution flow, such as: - Beginning of an LLM inference call - Start of a tool execution sequence - Initiation of a reasoning phase

Steps in Strands agents track different phases of execution: - A step starts when the agent begins processing (first token from LLM) - A step ends when transitioning to a tool call - A step ends when the final assistant response is complete

Lifecycle: StartStep establishes a step boundary. Content events (text, reasoning, tool) emitted after StartStep are considered part of this step.

Note: Steps are optional in the AI SDK protocol. Agents that don't track execution phases can omit these events.

Invariant: If StartStep is emitted, a corresponding FinishStep must eventually be emitted to maintain proper step lifecycle boundaries.

step and label carry latent fidelity from a native StepBoundary so the wire round-trips exactly; they are None for framework producers that only mark step boundaries without a canonical index/label.

TextDelta

TextDelta(text_id: str, delta: str)

Represents an incremental chunk of text content.

This event is emitted multiple times to deliver text content incrementally. Each delta contains a fragment of the complete text output. All TextDelta events for a given text block share the same text_id established by the preceding TextStart event.

The delta field contains raw text without special formatting or tags. Thinking/reasoning tags (like ...) are extracted during provider mapping and emitted as ReasoningDelta events instead.

Lifecycle: TextDelta events are emitted between TextStart and TextEnd for the same text_id.

Invariant: text_id must match a preceding TextStart event for the same text block.

Attributes: text_id: Unique identifier linking this delta to its text block delta: The incremental text fragment

TextEnd

TextEnd(text_id: str)

Marks the completion of a text content block.

This event is emitted once after all TextDelta events for a given text block have been completed. It establishes a clear boundary: no more TextDelta events will follow for this text_id.

Lifecycle: TextEnd is emitted after the final TextDelta for the text block.

Invariant: If TextStart was emitted for a text_id, TextEnd must eventually be emitted for the same text_id to maintain the Start → Deltas → End lifecycle.

Attributes: text_id: Unique identifier for the text block being completed

TextStart

TextStart(text_id: str)

Marks the beginning of a text content block.

This event is emitted once before any TextDelta events are sent for a new text block. It provides a unique identifier that will be referenced by all subsequent TextDelta events for this block.

Text blocks represent visible assistant output content (as opposed to reasoning, which may be hidden or displayed separately).

Lifecycle: TextStart establishes a text_id for the subsequent deltas.

Attributes: text_id: Unique identifier for this text block (e.g., "txt_abc123")

ToolInputAvailable

ToolInputAvailable(tool_call_id: str, tool_name: str, input: dict)

Provides the complete, structured tool input.

This event is emitted once when the full tool input is available. It contains the complete, parsed input as a structured dictionary.

Unlike ToolInputDelta events (which provide raw incremental fragments), this event provides the final, validated tool input that will be passed to the tool implementation. This is the definitive record of what parameters the agent decided to use.

The input field is a dictionary representing the complete tool arguments. This is typically JSON-serializable data (strings, numbers, booleans, lists, nested dicts) matching the tool's parameter schema.

The tool_name is included again to allow different handling of different tool types.

Protocol invariant: This event is always emitted, even if ToolInputDelta events were emitted earlier.

Lifecycle: ToolInputAvailable is emitted after ToolInputStart (and any ToolInputDelta events) for the same tool_call_id, before ToolOutputAvailable.

Invariant: tool_call_id must match a preceding ToolInputStart event.

Attributes: tool_call_id: Unique identifier for this tool call tool_name: Name of the tool being invoked input: Complete structured tool input as a dictionary

ToolInputDelta

ToolInputDelta(tool_call_id: str, delta: str)

Represents an incremental chunk of tool input data.

This event is emitted multiple times to deliver tool input incrementally. Each delta contains a fragment of the complete tool input, typically as partial JSON. All ToolInputDelta events for a given tool call share the same tool_call_id established by the preceding ToolInputStart event.

The delta field contains raw tool input fragments, often partial JSON strings that must be concatenated to form the complete input.

Protocol invariant: ToolInputStart must be emitted before any ToolInputDelta for the same tool_call_id.

Note: Not all agent frameworks emit input deltas. Some only provide the complete input via ToolInputAvailable. Providers should map accordingly.

Lifecycle: ToolInputDelta events are emitted between ToolInputStart and ToolInputAvailable for the same tool_call_id.

Invariant: tool_call_id must match a preceding ToolInputStart event.

Attributes: tool_call_id: Unique identifier linking this delta to its tool call delta: The incremental input fragment (often partial JSON)

ToolInputStart

ToolInputStart(tool_call_id: str, tool_name: str)

Marks the beginning of a tool call input.

This event is emitted once when the agent begins invoking a tool. It provides a unique identifier for this tool call and the name of the tool being invoked.

Protocol invariant: This event MUST be emitted before any ToolInputDelta or ToolInputAvailable events for the same tool_call_id. Violating this ordering breaks the AI SDK UI protocol.

Lifecycle: ToolInputStart establishes the tool_call_id for the subsequent input and output events.

The tool_call_id can be generated fresh or preserved from the upstream provider (e.g., Strands toolUseId). The tool_name is the exact name as known by the agent framework.

Attributes: tool_call_id: Unique identifier for this tool call (e.g., "call_abc123") tool_name: Name of the tool being invoked (e.g., "search_database")

ToolOutputAvailable

ToolOutputAvailable(tool_call_id: str, output: dict)

Provides the complete tool execution output.

This event is emitted once when a tool has finished executing and returned its output. It contains the complete result, including execution status and structured output content.

The output field is a dictionary with two required keys: - status: Either "success" or "error" indicating execution outcome - content: List of content items returned by the tool

The content list may contain various content types (text, json, image, document).

Protocol note: The AI SDK UI protocol does NOT support incremental tool output (tool-output-delta). This event always contains the complete output, even if the upstream agent framework streams execution progress. Providers must buffer and emit only the final output here.

Lifecycle: ToolOutputAvailable is emitted after ToolInputAvailable for the same tool_call_id.

Invariant: tool_call_id must match a preceding ToolInputStart event.

Attributes: tool_call_id: Unique identifier for this tool call output: Structured output dictionary with 'status' and 'content' keys