feat: add on_turn_end callback for inter-turn state changes#3589
feat: add on_turn_end callback for inter-turn state changes#3589Oxygen56 wants to merge 1 commit into
Conversation
Adds on_turn_end callback to RunHooksBase, AgentHooksBase, and RunConfig, firing after each agent turn to allow state inspection and dynamic modification. The callback is invoked at all turn boundaries (handoffs and run-again steps) in both the standard and streaming execution paths. - RunHooksBase.on_turn_end: lifecycle hook for run-level turn completion - AgentHooksBase.on_turn_end: per-agent lifecycle hook for turn completion - RunConfig.on_turn_end: standalone callback via TurnEndData - TurnEndData: dataclass carrying agent, context, and turn number Closes openai#2671
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6c17b31eac
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| For example, you can use this to add a system prompt to the input. | ||
| """ | ||
|
|
||
| on_turn_end: OnTurnEndCallback | None = None |
There was a problem hiding this comment.
Append the RunConfig callback to preserve positional calls
Adding on_turn_end here shifts every existing public RunConfig positional argument after call_model_input_filter: callers that currently pass tool_error_formatter, session_settings, reasoning_item_id_policy, sandbox, or later fields positionally will now bind those values to the wrong field. This violates the repo's public API positional-compatibility rule for exported dataclasses and breaks existing patterns covered by tests/test_source_compat_constructors.py; append the new optional field after the current fields or add a compatibility layer.
Useful? React with 👍 / 👎.
Adds
on_turn_endcallback for inspecting/modifying agent state between turns.Changes
RunHooksBaseandAgentHooksBase(lifecycle.py)on_turn_endlifecycle hook method that fires after each turn completesRunConfig(run_config.py)TurnEndDatadataclass carrying the current agent, run context, and turn numberOnTurnEndCallbacktype alias for the callback signatureon_turn_endfield toRunConfigas an optional callbackTurn loop wiring (run.py, run_internal/run_loop.py)
on_turn_endat all turn boundaries in both the standard and streaming execution paths:NextStepHandoff(after handoff, before next agent starts)NextStepRunAgain(after tool execution, before next model call)Use cases
Closes #2671
🤖 Generated with Claude Code