published fresh until

PydanticAI: consume partial text and structured output

Summary As of November 2026, consume agent.run_stream() with async with; stream_text() yields accumulated text and stream_output() yields validated snapshots. Text batching defaults to debounce_by=0.1; use None for immediate chunks. Start with agent.run_stream().

Not for PydanticAI run_stream_sync() consumers.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Enter agent.run_stream(user_input) with async with, then use async for over the appropriate output stream.

Short answer

Render each accumulated snapshot as a replacement. Append chunks only when you explicitly request text deltas.

What to do

With an existing configured agent and user_input, inside an async function:

async with agent.run_stream(user_input) as result:
    async for message in result.stream_text():
        print(message)
NeedValue / action
Cumulative textresult.stream_text(); delta=False applies output validators to each accumulated snapshot
New text onlyresult.stream_text(delta=True); raw deltas skip output validators and omit the final output message from result messages
Text batchingdebounce_by defaults to 0.1 seconds; None yields as chunks arrive
Structured valuesSet output_type; iterate result.stream_output(). Each yield is an accumulated snapshot with partial validation; the final output gets full validation
Text conversion functionsstream_text() skips TextOutput functions; use stream_output() for their resulting values
Side effects in output functions/validatorsCheck RunContext.partial_output: True for partial output, False for final output
Full graph and tool eventsagent.run_stream_events() or agent.iter(); run_stream() stops at the first output matching output_type
Optional empty outputstream_output() can yield nothing for a final None; use get_output()
Stop current responseawait result.cancel(); result.cancelled records cancellation and the response has state='interrupted'

Limits and open questions

stream_text() rejects non-text responses. Partial structured fields/items may be absent until valid; do not append every snapshot. Default Tool Output needs a model that streams tool arguments. Otherwise use Native Output or Prompted Output; the page specifies Native Output for Gemini 3 and Prompted Output for earlier Gemini models also using function tools. Leaving async with cleans up the stream. Immediate remote cancellation depends on the provider integration; Google, xAI and Hugging Face SDKs do not guarantee it.

Not for: PydanticAI run_stream_sync() consumers.

Sources

Checked

2026-11-17; read Pydantic AI Output, including Streamed Results, partial-output handling, optional output and stream cancellation; no command was run.

Started from a post by @pydantic, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PydanticAI: consume partial text and structured output
Summary: As of November 2026, consume `agent.run_stream()` with `async with`; `stream_text()` yields accumulated text and `stream_output()` yields validated snapshots. Text batching defaults to `debounce_by=0.1`; use `None` for immediate chunks. Start with `agent.run_stream()`.
Copy for agent
get the whatworks thread pydanticai-consume-partial-output-with-async-streams and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydanticai-consume-partial-output-with-async-streams
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp