Skip to main content
AgentTurn represents one live execution of an Agent. Create one with agent.turn(...). Do not instantiate AgentTurn directly unless you are extending the framework.

Import

Constructor

Normally called internally by:

Properties

str
Unique turn ID.
str
ID of the owning agent.
Agent[Any]
Owning agent instance.
Message
Initial user message for the turn.
TurnOptions
Per-turn options.
bool
True after the turn has started.
bool
True after the turn finishes, fails, or is aborted.
AbortSignal
Public abort signal for tools and other work tied to this turn.
tuple[SteeringEntry, ...]
Immutable ordered snapshot of steering messages waiting for a safe step boundary.

events

Starts the turn if needed and yields typed lifecycle events.
Common event types: Returns:
AsyncIterator[TurnEvent]
Stream of turn events. The iterator ends after turn.finished or turn.failed.

text_stream

Convenience stream over events() that yields only text.delta text.

result

Starts the turn if needed and waits for the structured result. Returns:
TurnResult
Final structured result.
Raises:
Exception
Raised if the turn is aborted.
Exception
Propagates provider, tool, or runtime failures.

wait

Alias for result().

send

Queues a steering message for the next safe step boundary and returns its stable ID. Parameters:
Content
required
Message content to inject into the active turn.
Literal['user', 'system']
Role for the injected message. Defaults to "user".
Notes:
  • send() does not mutate a provider request already in flight.
  • queued messages are injected before the next model call.
  • queued messages are scoped to this turn.

steer

Alias for send().

cancel_steering

Removes a queued steering entry by its stable ID. Returns False if the entry was already claimed or canceled.

abort

Aborts the turn. Parameters:
str | None
Optional abort reason.

TurnOptions

Fields:
ToolSet | None
Per-turn tools. Defaults to the agent’s tools.
StopCondition | list[StopCondition] | None
Per-turn stop conditions. Defaults to the agent’s stop_when.
ReasoningConfig | None
Per-turn reasoning controls. Defaults to the agent’s reasoning.
ProviderOptions | None
Per-turn provider options. Defaults to the agent’s provider_options.
RetryPolicy | None
Per-turn provider-call retry policy. Defaults to the agent’s retry.
AbortSignal | None
External abort signal that also aborts the turn.
AgentHooks | None
Per-turn hook overrides. These merge over agent-level default hooks.
dict[str, Any]
Application metadata. Not sent to the provider by default.

TurnResult

TurnEvent

TurnStarted

TextDelta

ReasoningDelta

StepStarted

StepFinished

usage is the provider-reported usage for this step.

StepRetrying

Tool lifecycle events

AgentTurn forwards the same ToolCallStartedEvent, ToolCallDeltaEvent, ToolCallReadyEvent, ToolExecutionStartedEvent, ToolExecutionProgressEvent, ToolExecutionFinishedEvent, and ToolResultEvent objects exposed by stream_text().event_stream. This preserves tool call IDs, provider indexes, execution completion order, and normalized results across core and agent APIs.

TurnFinished

TurnFailed

TurnResult.termination and TurnFailed.termination expose the same structured diagnostic used by core generation and streaming. Aborted and failed turns also attach it to the exception raised by result().

Wire Codec

turn_event_to_dict(event) converts every public TurnEvent, including nested messages, steps, reasoning, tools, results, usage, and final TurnResult, to a stable JSON-compatible object. turn_event_from_dict(payload) reconstructs the matching event dataclass. The stock AgentServer uses this codec for POST /agent/{id}/chat?stream=events. Byte content is represented with an ai-query wire marker and restored as bytes. ToolOutput image and file bytes use the same JSON-safe round trip. Other tool result values must be JSON-compatible. Structured termination diagnostics round-trip on both turn.finished and turn.failed.

Example

See Also