Add `RefineOutput` harness capability and `clai2` `/refine` command
- Fixes #12995
Why we make these changes
#12995 asks for a refinement capability, exposed in clai2 as /refine, and leaves the scope to whoever picks it up. Chosen scope: self-review only. The same model reviews its own output and answers again. There is no critic model, scoring or stop condition, because TrajectoryJudge and output guardrails already cover review by a second model or a rule.
New public surface
pydantic_ai_harness.RefineOutput(rounds=1, instructions=DEFAULT_REFINE_OUTPUT_INSTRUCTIONS): when a run would end with an output, it adds a system prompt that tells the model to review its answer and give it again, revised, up toroundstimes per run.pydantic_ai_harness.DEFAULT_REFINE_OUTPUT_INSTRUCTIONS.- clai2 built-in plugin
refinement(on by default) with/refine [FOCUS]: submits the same instructions, plus the focus, as the next turn.
User-visible behavior
agent = Agent('anthropic:claude-opus-5-5', capabilities=[RefineOutput()])
result = await agent.run('...') # one more model request; result.output is the revised answer
In clai2, type /refine check the error handling after an answer. To review every turn: /plugins add refine_output pydantic_ai_harness.refine_output:RefineOutput.
Not reviewed: Agent.run_stream(), which ends at the first output it streams, runs that end with DeferredToolRequests or PendingModelResponse, and an output no model produced, such as an InputGuardrail refusal (folded in from #13121, so a review never sends a blocked prompt to the model).
Choices made
- Name:
RefineOutput, notRefinement. Harness naming asks for a verb phrase on a hooks-only capability. - Round count: per-run state (
for_run), not message metadata, so compaction cannot restart the rounds. Each instance counts its own. /refine: a built-in plugin like/compact, not code incommands.py. It sends a one-off prompt and does not attachRefineOutput.tests/append_only.py: the replay transport's sent list is trimmed before each replay.RefineOutputis the first capability whose replay sends two requests, and response IDs count sent requests.
Verification
- Capability:
test_refine_output.py; durable replay intest_temporal.py,test_dbos.py,test_prefect.py; append-only history intest_history_append_only.py. - Command:
test_refinement.py. - Live in clai2 on
claude-code:claude-fable-5-2:/refine <focus>returned a revised answer.RefineOutput()bound to the stock agent streamed a first answer and then a complete restated one, on two turns. - Live probes still outstanding, for a maintainer to run or waive before merging: another provider's frontier model, structured output, and a model on the
<system>-fallback rendering.agent_docs/message-history-and-caching.mdasks for them, and this session had no credentials for them.
What changes for existing users
Nothing in the harness. clai2 gains the /refine command from the built-in refinement plugin, on by default (/plugins disable refinement opts out); the API check's DEFAULT_PLUGINS change is waived like herdr in #11661.
PR decisions
2026-11-10 · scope: self-review capability, no critic model · iter -
- Decision: RefineOutput re-prompts the same model with a SystemPromptPart when the run would end, for rounds=1 by default; no second model, scoring, or stop condition
- Why: The issue leaves scope open and Mike asked for lean; TrajectoryJudge and guardrails already cover second-model review
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
2026-11-10 · name: RefineOutput, package refine_output · iter -
- Decision: Named RefineOutput rather than Refinement
- Why: Harness naming rule: 'Never invent a nominalization for an action'; hooks-only capabilities take a verb phrase
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
2026-11-10 · stateless round count · iter -
- Decision: Rounds are counted from the current run's SystemPromptPart metadata (origin + round), not instance state; no for_run
- Why: Per-run state derivable from ctx needs no for_run and no durable-engine test matrix; several instances share the count
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
2026-11-10 · /refine is a built-in plugin, not a commands.py command · iter -
- Decision: /refine ships as built-in plugin 'refinement' (default on) that submits DEFAULT_REFINE_OUTPUT_INSTRUCTIONS plus focus via host.submit_prompt; it does not attach RefineOutput
- Why: clai2 rule 'Built-ins are declarations, not code paths'; /compact is the precedent; an on-demand round over a finished answer is a new prompt, and always-on review is one /plugins add away
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
2026-11-10 · tests/append_only.py replay IDs · iter -
- Decision: run_conversation drops each replay's requests from transport.sent so both replays get the same scripted response IDs
- Why: Response IDs count requests sent; RefineOutput is the first capability whose replay sends two requests, so the determinism check compared msg_N with msg_N+2
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
2026-11-10 · live probe not attempted · iter -
- Decision: The SystemPromptPart steering was not live-probed per provider; stated in the PR body
- Why: No provider API keys in this environment; clai2's stored keys are not mine to read from a script
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
2026-11-10 · pre-push review 1 of 3 at 10be02fa45964467855bc1c95c13b2b49316cb1a · iter -
- Decision: reserved; outcome follows under the same anchor
- Why: tree 746277e8241bb3a3163f355f6f188889c0b84734; scope bbd5327dcfff884a6626bef3ebf32beac265b749..10be02fa45964467855bc1c95c13b2b49316cb1a, full branch; first review. Reviewer is the read-only Explore sub-agent: delegate_task takes no working directory, so it starts in the candidate worktree and is told to read only the policy-base checkout and the bundle by absolute path
- Source: https://github.com/pydantic/pydantic-ai/tree/agent/clai2-12995
2026-11-10 · pre-push review 1 of 3 at 10be02fa45964467855bc1c95c13b2b49316cb1a · iter -
- Decision: not run: the read-only Explore reviewer cannot read outside the project root, so it returned no review; counted against the budget
- Why: tree 746277e8241bb3a3163f355f6f188889c0b84734; nothing was reviewed
- Source: https://github.com/pydantic/pydantic-ai/tree/agent/clai2-12995
2026-11-10 · pre-push review 2 of 3 at 10be02fa45964467855bc1c95c13b2b49316cb1a · iter -
- Decision: reserved; outcome follows under the same anchor
- Why: tree 746277e8241bb3a3163f355f6f188889c0b84734; scope bbd5327dcfff884a6626bef3ebf32beac265b749..10be02fa45964467855bc1c95c13b2b49316cb1a, full branch. The policy-base (git archive of 180e33c63e4d5885d9df7adf7c07a9cde2e9522d) and the bundle are mirrored under git-ignored local-notes/review-12995/ because the read-only reviewer reads only inside the project root; the candidate tree stays clean
- Source: https://github.com/pydantic/pydantic-ai/tree/agent/clai2-12995
2026-11-10 · per-run round count via for_run · iter -
- Decision: RefineOutput counts rounds in per-run instance state (_rounds_started, for_run returns replace(self)) instead of counting marker parts in the run's messages. Each instance counts its own rounds. Durability tests added for Temporal, DBOS and Prefect.
- Why: Pre-push review 2 finding 4: a repro showed compaction dropping the marker SystemPromptPart restarted the rounds until UsageLimits was hit. The hook runs in workflow code, so replay recounts deterministically.
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
- Supersedes: stateless round count
2026-11-10 · live probe of RefineOutput on one provider · iter -
- Decision: RefineOutput was probed live through the stock clai2 CLI with the capability bound in-process (scratch launcher, no config change) on claude-code:claude-fable-5-2. Both turns streamed a draft then a full restated answer; the model never replied 'no changes needed'. Not probed: other providers (no credentials), structured output, the -fallback rendering. Stated in the PR body.
- Why: Pre-push review 2 finding 3: agent_docs/message-history-and-caching.md:50 asks for a live probe of new steering before merge; only the stored Claude Code login was available.
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
- Supersedes: live probe not attempted
2026-11-10 · pre-push review 2 of 3 at 10be02fa45964467855bc1c95c13b2b49316cb1a: outcome · iter -
- Decision: 8 findings. Fixed: run_stream guard (CallToolsNode), PendingModelResponse skip, compaction restart (per-run state), refine_output in clai2 _HARNESS_DOCS, SCENARIOS['RefineOutput'], docs links and streaming note, helper inlined. Finding 3 (live probe) done on one provider only.
- Why: Each finding was reproduced or checked against the cited rule before changing code.
- Source: https://github.com/pydantic/pydantic-ai/issues/12995
2026-11-10 · pre-push review 3 of 3 at 478340acf911cd1948406362e2fc8410daf336a1 · iter -
- Decision: reserved: dispatch 3 of 3
- Why: candidate tree 2e59246b14561307bf524109a558771a68ac6113; follow-up scope 10be02fa..b2cc3038 plus a conflict-free merge of origin/main b59c45e0; since review 2: per-run round count, run_stream and PendingModelResponse guards, durability tests, docs
- Source: https://github.com/pydantic/pydantic-ai/tree/agent/clai2-12995
2026-11-10 · pre-push review 3 of 3 at 478340acf911cd1948406362e2fc8410daf336a1: outcome · iter -
- Decision: remediated: 3 findings. PendingModelResponse bullet widened to batch and background requests (README and docs page); clai2 reference says each turn shows the first answer then the revised one; outstanding live probes listed in the PR body for Mike to run or waive. Remediation commit 21bf1bb6f7449b4f79f93566a6ef89f72ec36883, docs prose only. Budget exhausted; no fourth review.
- Why: reviewed tree 2e59246b14561307bf524109a558771a68ac6113; prior findings 1, 2, 4-8 confirmed fixed; remediation tree d73bbcc03fcd67ce70d016418af4a0c18364197a
- Source: https://github.com/pydantic/pydantic-ai/tree/agent/clai2-12995
2026-11-10 · input-guardrail refusal is not reviewed (stacked PR) · iter -
- Decision: RefineOutput skips an End whose response has no model_id, the documented mark of a response no model request produced (SkipModelRequest). Shipped as a stacked PR on agent/clai2-12995-input-block, base agent/clai2-12995, per Mike's workflow of not adding commits to a PR already open for review. Commit 376c543f48862f330133ab5b5868678fe38d80d4.
- Why: veria-ai finding on #13120 reproduced: with InputGuardrail(guard=lambda _: False) and RefineOutput, in either order, the provider was called once with the blocked prompt. Test fails without the guard (2 failed) and passes with it. Pre-push review budget is exhausted (3 of 3), so this runtime delta goes to hosted review without a local one.
- Source: https://github.com/pydantic/pydantic-ai/pull/13120#discussion_r4531641478
2026-11-11 · CI repair on #13120 at 1790ab45f456eee694356faa2d373168f70c8123 (exempt delta from reviewed 21bf1bb6) · iter -
- Decision: Pushed to the parent rather than stacked: an API-compatibility waiver for DEFAULT_PLUGINS (against v2.66.0, herdr #11661 precedent, label 'compatibility impact') and '# pragma: no cover' on an unreachable test line. Parent CI cannot go green from a stacked PR, and Mike merges bottom-up.
- Why: Exempt deltas: test-harness repair (coverage of a never-called tool body) and machine data the API gate asks for; no runtime change.
- Source: https://github.com/pydantic/pydantic-ai/pull/13120
2026-11-11 · parallel guardrail refusal: signal is model_name, not model_id (#13121 at 75ef53c) · iter -
- Decision: RefineOutput skips a response whose model_name is None. Stacked PR #13121 merged agent/clai2-12995 (no conflicts) and changed the check.
- Why: veria-ai on #13121 reproduced: InputGuardrail(parallel=True) refusal carries model_id because the request started; model_name is None in both modes. Test fails without the change (4 failed). Review budget exhausted.
- Source: https://github.com/pydantic/pydantic-ai/pull/13121
2026-11-11 · fold #13121 into agent/clai2-12995 and enqueue #13120 · iter -
- Decision: Squash-merged stacked #13121 into agent/clai2-12995 (09daebbc), merged origin/main (27da5915), pushed normally. Once approved, green and CLEAN, #13120 goes to the merge queue with gh pr merge --squash --auto.
- Why: Mike approved merging #13120 once green (Nov 11 10:56 AM ET) and asked for #13121 to be folded in rather than merged to main separately.
- Source: https://github.com/pydantic/pydantic-ai/pull/13120
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.