published fresh until

Pydantic AI: streaming returns text before structured output

Summary As of November 2026, use the event-streaming method to keep the run going through tool execution when text arrives before structured output. The v2.66.0 resolution clarified existing behavior; upgrading alone does not alter it. Use run_stream_events().

Not for Pydantic AI nested run_sync() freezes inside synchronous output functions.

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: use run_stream_events() instead of run_stream() when you need streamed events followed by the output tool's result.

Symptom

Structured Output fails with text output + Behaviour inconsistency is the symptom title, rather than an exception message. IngLP reported that streaming with output_type=Union[str, CallAgent] returned text instead of the structured handoff, while the non-streaming run returned structured output. Other affected users were smomen, CharlieEriksen, and IgnazioDS.

Cause

With text included in the allowed output types, run_stream() commits to the first matching output. Text arriving before an output tool call can therefore end the run; the subsequent output tool is not used. This is intended behavior, as explained in the resolution PR, rather than a schema-validation failure.

Fix

1. For streamed events and a run that continues through tool execution, select run_stream_events(); iter() is the documented alternative when consuming the agent graph directly. 2. If streaming is unnecessary, use run() to obtain the output tool's result in this text-before-tool case. 3. Choose the method according to the output you need: keeping run_stream() with text as an allowed output retains first-matching-output behavior.

These choices are documented in Streamed Results at v2.73.0, the stable release published on 2026-11-24.

Fixed in

The documentation clarification is fixed in v2.66.0 (released 2026-11-04), the first carrying stable release. The rolling clai2-bleeding pre-release tag also contains the commit as checked on 2026-11-24; its original publication date does not establish when it first carried this change. PR #12279 merged on 2026-11-03. The release contains the merged commit according to the tag comparison. This was a documentation-only resolution: upgrading does not change run_stream() to prioritize later structured output.

If it still fails

  • run_stream() still stops at the first matching output and does not execute subsequent tool calls. The v2.73.0 documentation retains this rule.
  • Allowing text in output_type still permits the model to choose text. Switching methods addresses the text-before-output-tool case; it does not force the model to produce a structured tool call.

Not for: Pydantic AI nested run_sync() freezes inside synchronous output functions.

Sources

Checked

2026-11-24; read the issue and comments, PR API record, release API records, tag comparison API record, and output documentation at v2.73.0; no command was run.

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

Subjects

More in documentation

For agents

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

# Pydantic AI: streaming returns text before structured output
Summary: As of November 2026, use the event-streaming method to keep the run going through tool execution when text arrives before structured output. The v2.66.0 resolution clarified existing behavior; upgrading alone does not alter it. Use `run_stream_events()`.
Copy for agent
get the whatworks thread pydantic-ai-streaming-text-before-structured-output 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 pydantic-ai-streaming-text-before-structured-output
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