You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add RunState parameter support to Runner.run() methods
This commit integrates RunState into the Runner API, allowing runs to be
resumed from a saved state. This is the final piece needed to make
human-in-the-loop (HITL) tool approval fully functional.
**Changes:**
1. **Import NextStepInterruption** (run.py:21-32)
- Added NextStepInterruption to imports from _run_impl
- Added RunState import
2. **Updated Method Signatures** (run.py:285-444)
- Runner.run(): Added `RunState[TContext]` to input union type
- Runner.run_sync(): Added `RunState[TContext]` to input union type
- Runner.run_streamed(): Added `RunState[TContext]` to input union type
- AgentRunner.run(): Added `RunState[TContext]` to input union type
- AgentRunner.run_sync(): Added `RunState[TContext]` to input union type
- AgentRunner.run_streamed(): Added `RunState[TContext]` to input union type
3. **RunState Resumption Logic** (run.py:524-584)
- Check if input is RunState instance
- Extract state fields when resuming: current_turn, original_input,
generated_items, model_responses, context_wrapper
- Prime server conversation tracker from model_responses if resuming
- Cast context_wrapper to correct type after extraction
4. **Interruption Handling** (run.py:689-726)
- Added `interruptions=[]` to successful RunResult creation
- Added elif branch for NextStepInterruption
- Return RunResult with interruptions when tool approval needed
- Set final_output to None for interrupted runs
5. **RunResultStreaming Support** (run.py:879-918)
- Handle RunState input for streaming runs
- Added `interruptions=[]` field to RunResultStreaming creation
- Extract original_input from RunState for result
**How It Works:**
When resuming from RunState:
```python
# User approves/rejects tool calls on the state
run_state.approve(approval_item)
# Resume the run from where it left off
result = await Runner.run(agent, run_state)
```
When a tool needs approval:
1. Run pauses at tool execution
2. Returns RunResult with interruptions=[ToolApprovalItem(...)]
3. User can inspect interruptions and approve/reject
4. User resumes by passing RunResult back to Runner.run()
**Remaining Work:**
- Add `state` property to RunResult for creating RunState from results
- Add comprehensive tests
- Add documentation/examples
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
0 commit comments