Give `AgentRunError` the failed run's `conversation`, and add `capture_run_conversation()`, so a failed turn can be stored
Merged
DO
17d ago+782+782+012345678901234567890123456789-100-100-012345678901234567890123456789
Part of #11846, the half about keeping a failed run so it can be stored, not the wrap-up design. Store-backed server-held UI history (#11031) needs it, so a client can see what a failed turn did.
What
AgentRunError.conversation. A run that hit a usage limit, a model error or a cancellation can now be stored:await store.save(error.conversation)appends the failed turn to aConversationStore.- It's the run's conversation as far as it got, including a response or request cut short (
state='interrupted'). - It's built the same way
AgentRunResult.conversationis, so it carries therevisionand knows which messages are new. So the save is checked for conflicts, and appends just what the run added. - Before this, only
RunCancelledcarried its messages, and nothing carried the conversation.
- It's the run's conversation as far as it got, including a response or request cut short (
- A nested run's error carries the outer run's conversation. That's the one its caller can store, matching how a nested
RunCancelledis already stamped again. capture_run_conversation()works likecapture_run_messages(), but yields aCapturedConversationwhose.conversationcovers any exception, including one raised by a tool's own code, which can't carry an attribute of ours. It's exported frompydantic_aiandpydantic_ai.agent.- Refactor:
AgentRunResult.conversation's logic moves to a module-levelconversation_from_state, shared with the failed-run path. Its behavior is unchanged.
Scope
- Plain
Agentusers see no change in behavior. Errors gain an attribute, built once when the error passes the run's edge. The attaching lives in a small context manager rather than more branches in_PreparedAgentRun.open, which is already over the complexity limit. - Realtime sessions aren't wired. Their errors'
conversationisNone, as documented.
Tests
tests/test_conversation_store.py:
- Usage limit: a run on a loaded conversation that hits its usage limit saves just the new turn (the prompt, the tool call and its return), and reloads in full.
- Tool exception: a tool that raises leaves a capture whose conversation stores the prompt, the call and the cut-short request.
- Cancellation: a cancelled run's
conversationmatchesRunCancelled.all_messages(). - Nested run: a nested run's usage-limit error reaches the outer caller carrying the outer conversation.
Undoing each wiring point fails a test: attaching on errors, on cancellation, the capture registration, and outer-wins stamping. The new docstring example runs and prints what it documents.
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.
๐ค Generated with Claude Code
Summary by cubic
AgentRunError now carries the failed run's conversation, so a turn that hit a usage limit, model error, or cancellation can still be stored with store.save(error.conversation).
conversationis built likeAgentRunResult.conversation, so a save appends just what the run added; a resumed run that fails before adding a message keeps its conversation's pending tool calls (as a copy, so the original isn't mutated), while one that fails after running approved calls leaves none waiting.- A nested run's error carries the outer run's conversation, since that's the one its caller can store. A nested run's
RunCancelledkeeps its own conversation, available as the outer error's cause. For batched runs, each run gets its own copy of the error โ when a batch submission fails, and when a run joins another run's shared check โ so one failed run's conversation isn't another's; an error that can't be copied is shared instead, so the other requests don't wait forever. - New
capture_run_conversation()captures the same conversation for any exception, including a tool's own or aFallbackExceptionGroup; nested contexts capture the innermost run, and it's exported frompydantic_aiandpydantic_ai.agent. CapturedConversation.conversationis built on first read and cached once the run ends: a read during the run returns a fresh snapshot, while a saved conversation keeps its new revision on later reads.- The conversation-building logic moves to a private
_run_conversationmodule shared with the success path; behavior is unchanged for plainAgentusers, andconversationisNonefor realtime errors and for anAgentRunErrorcaught inside aniterorrun_streamblock.RunCancellednow carries it too, including on an externally cancelled event stream. - Any
AgentRunErrorkeeps its conversation when pickled, as durable engines like DBOS do. - Docs now note that
conversation=error.conversationcarries the conversation's usage forward, so limits check the total of both runs, that a Temporal client gets a failed workflow run's error without itsconversationso the workflow saves it inside the workflow, and that DBOS records a failed workflow run's conversation with its error.