Skip to content

Commit 3eb0228

Browse files
mjschockclaude
andcommitted
feat: add to_state() method to RunResult for resuming runs
This commit adds a method to convert a RunResult back into a RunState, enabling the resume workflow for interrupted runs. **Changes:** 1. **to_state() Method** (result.py:125-165) - Added method to RunResult class - Creates a new RunState from the result's data - Populates generated_items, model_responses, and guardrail results - Includes comprehensive docstring with usage example **How to Use:** ```python # Run agent until it needs approval result = await Runner.run(agent, "Use the delete_file tool") if result.interruptions: # Convert result to state state = result.to_state() # Approve the tool call state.approve(result.interruptions[0]) # Resume the run result = await Runner.run(agent, state) ``` **Complete HITL Flow:** 1. Run agent with tool that needs_approval=True 2. Run pauses, returns RunResult with interruptions 3. User calls result.to_state() to get RunState 4. User calls state.approve() or state.reject() 5. User passes state back to Runner.run() to resume 6. Run continues from where it left off **Remaining Work:** - Add comprehensive tests - Create example demonstrating HITL - Add documentation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent de986ef commit 3eb0228

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/agents/result.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,48 @@ def last_agent(self) -> Agent[Any]:
122122
"""The last agent that was run."""
123123
return self._last_agent
124124

125+
def to_state(self) -> Any:
126+
"""Create a RunState from this result to resume execution.
127+
128+
This is useful when the run was interrupted (e.g., for tool approval). You can
129+
approve or reject the tool calls on the returned state, then pass it back to
130+
`Runner.run()` to continue execution.
131+
132+
Returns:
133+
A RunState that can be used to resume the run.
134+
135+
Example:
136+
```python
137+
# Run agent until it needs approval
138+
result = await Runner.run(agent, "Use the delete_file tool")
139+
140+
if result.interruptions:
141+
# Approve the tool call
142+
state = result.to_state()
143+
state.approve(result.interruptions[0])
144+
145+
# Resume the run
146+
result = await Runner.run(agent, state)
147+
```
148+
"""
149+
from .run_state import RunState
150+
151+
# Create a RunState from the current result
152+
state = RunState(
153+
context=self.context_wrapper,
154+
original_input=self.input,
155+
starting_agent=self.last_agent,
156+
max_turns=10, # This will be overridden by the runner
157+
)
158+
159+
# Populate the state with data from the result
160+
state._generated_items = self.new_items
161+
state._model_responses = self.raw_responses
162+
state._input_guardrail_results = self.input_guardrail_results
163+
state._output_guardrail_results = self.output_guardrail_results
164+
165+
return state
166+
125167
def __str__(self) -> str:
126168
return pretty_print_result(self)
127169

0 commit comments

Comments
 (0)