DynamicWorkflow: let the workflow script declare a per-call output schema for sub-agents
Transferred from pydantic/pydantic-ai-harness#624 after harness moved into this repo. Harness paths map to
src/pydantic_ai_harness/pydantic_ai_harness/,tests/harness/anddocs/harness/.
Today: typed handoffs are author-declared only
In DynamicWorkflow, structured hand-offs between sub-agents exist, but their shape is fixed by whoever registers the catalog: each sub-agent's output_type is set on the Agent at registration, the catalog renders it to the model as a TypedDict, and every call inside the script has the fixed signature (*, task: str) (_toolset.py). The orchestrating model can choreograph freely, but it cannot say "research this and give me the result in THIS shape" — the shape was decided by the code author.
Feature: dynamic output types
Let the workflow script pass a schema per call, e.g.:
class Finding(TypedDict):
claim: str
source_url: str
confidence: float
results = await asyncio.gather(*[
researcher(task=q, output_schema=Finding) for q in sub_questions
])The script-defined TypedDict (or a plain dict-shape literal) becomes that run's output_type, so the orchestrator can invent the format that downstream sub-agents consume — typed data flowing agent→agent in a format the model designed for the question at hand.
Why it's feasible
- Core already supports per-run output overrides:
Agent.run(..., output_type=...)— the host side needs no new machinery. - The work is the sandbox→host transfer: recognize an
output_schemakwarg in the Monty script, serialize the TypedDict/dict-shape to a JSON schema, and build the run's output type from it host-side (validated dict output; no need to materialize a Python class). - Return values already cross the boundary as dicts read by subscript, so script-side ergonomics don't change.
Origin & relation
- Douwe, docs-strategy braindump 14 (8/13) + ruling 8/14: the researcher fan-out with structured hand-offs is the shape that matches broad research; pydantic/pydantic-ai-harness#609's open half (should
ResearchercomposeDynamicWorkflow?) can ship with author-defined findings-with-sources types first — this issue is the generalization that removes the author from the loop. - Complements pydantic/pydantic-ai-harness#353 (infer return schemas from first tool response, CodeMode side).
Source: pydantic/pydantic-ai