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
Copy file name to clipboardExpand all lines: docs/running_agents.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,9 +62,67 @@ The `run_config` parameter lets you configure some global settings for the agent
62
62
-[`session_input_callback`][agents.run.RunConfig.session_input_callback]: Customize how new user input is merged with session history before each turn when using Sessions.
63
63
-[`call_model_input_filter`][agents.run.RunConfig.call_model_input_filter]: Hook to edit the fully prepared model input (instructions and input items) immediately before the model call, e.g., to trim history or inject a system prompt.
64
64
-[`tool_error_formatter`][agents.run.RunConfig.tool_error_formatter]: Customize the model-visible message when a tool call is rejected during approval flows.
65
+
-[`reasoning_item_id_policy`][agents.run.RunConfig.reasoning_item_id_policy]: Control whether reasoning item IDs are preserved or omitted when the runner converts prior outputs into next-turn model input.
65
66
66
67
Nested handoffs are available as an opt-in beta. Enable the collapsed-transcript behavior by passing `RunConfig(nest_handoff_history=True)` or set `handoff(..., nest_handoff_history=True)` to turn it on for a specific handoff. If you prefer to keep the raw transcript (the default), leave the flag unset or provide a `handoff_input_filter` (or `handoff_history_mapper`) that forwards the conversation exactly as you need. To change the wrapper text used in the generated summary without writing a custom mapper, call [`set_conversation_history_wrappers`][agents.handoffs.set_conversation_history_wrappers] (and [`reset_conversation_history_wrappers`][agents.handoffs.reset_conversation_history_wrappers] to restore the defaults).
67
68
69
+
### Run config details
70
+
71
+
#### `tool_error_formatter`
72
+
73
+
Use `tool_error_formatter` to customize the message that is returned to the model when a tool call is rejected in an approval flow.
74
+
75
+
The formatter receives [`ToolErrorFormatterArgs`][agents.run_config.ToolErrorFormatterArgs] with:
76
+
77
+
-`kind`: The error category. Today this is `"approval_rejected"`.
78
+
-`tool_type`: The tool runtime (`"function"`, `"computer"`, `"shell"`, or `"apply_patch"`).
79
+
-`tool_name`: The tool name.
80
+
-`call_id`: The tool call ID.
81
+
-`default_message`: The SDK's default model-visible message.
82
+
-`run_context`: The active run context wrapper.
83
+
84
+
Return a string to replace the message, or `None` to use the SDK default.
85
+
86
+
```python
87
+
from agents import Agent, RunConfig, Runner, ToolErrorFormatterArgs
`reasoning_item_id_policy` controls how reasoning items are converted into next-turn model input when the runner carries history forward (for example, when using `RunResult.to_input_list()` or session-backed runs).
110
+
111
+
-`None` or `"preserve"` (default): Keep reasoning item IDs.
112
+
-`"omit"`: Strip reasoning item IDs from the generated next-turn input.
113
+
114
+
Use `"omit"` primarily as an opt-in mitigation for a class of Responses API 400 errors where a reasoning item is sent with an `id` but without the required following item (for example, `Item 'rs_...' of type 'reasoning' was provided without its required following item.`).
115
+
116
+
This can happen in multi-turn agent runs when the SDK constructs follow-up input from prior outputs (including session persistence, server-managed conversation deltas, streamed/non-streamed follow-up turns, and resume paths) and a reasoning item ID is preserved but the provider requires that ID to remain paired with its corresponding following item.
117
+
118
+
Setting `reasoning_item_id_policy="omit"` keeps the reasoning content but strips the reasoning item `id`, which avoids triggering that API invariant in SDK-generated follow-up inputs.
119
+
120
+
Scope notes:
121
+
122
+
- This only changes reasoning items generated/forwarded by the SDK when it builds follow-up input.
123
+
- It does not rewrite user-supplied initial input items.
124
+
-`call_model_input_filter` can still intentionally reintroduce reasoning IDs after this policy is applied.
125
+
68
126
## Conversations/chat threads
69
127
70
128
Calling any of the run methods can result in one or more agents running (and hence one or more LLM calls), but it represents a single logical turn in a chat conversation. For example:
@@ -193,6 +251,16 @@ async def main():
193
251
print(f"Assistant: {result.final_output}")
194
252
```
195
253
254
+
!!! note
255
+
256
+
The SDK automatically retries `conversation_locked` errors with backoff. In server-managed
257
+
conversation runs, it rewinds the internal conversation-tracker input before retrying so the
258
+
same prepared items can be resent cleanly.
259
+
260
+
In local session-based runs (which cannot be combined with `conversation_id`,
261
+
`previous_response_id`, or `auto_previous_response_id`), the SDK also performs a best-effort
262
+
rollback of recently persisted input items to reduce duplicate history entries after a retry.
263
+
196
264
## Call model input filter
197
265
198
266
Use `call_model_input_filter` to edit the model input right before the model call. The hook receives the current agent, context, and the combined input items (including session history when present) and returns a new `ModelInputData`.
Copy file name to clipboardExpand all lines: docs/sessions/index.md
+61Lines changed: 61 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,67 @@ When session memory is enabled:
53
53
54
54
This eliminates the need to manually call `.to_input_list()` and manage conversation state between runs.
55
55
56
+
## Customizing prepared input
57
+
58
+
When you pass a session, the runner normally prepares model input as:
59
+
60
+
1. Session history (retrieved from `session.get_items(...)`)
61
+
2. New turn input
62
+
63
+
Use [`RunConfig.session_input_callback`][agents.run.RunConfig.session_input_callback] to customize that merge step before the model call. The callback receives two lists:
64
+
65
+
-`history`: The retrieved session history (already normalized into input-item format)
66
+
-`new_input`: The current turn's new input items
67
+
68
+
Return the final list of input items that should be sent to the model.
69
+
70
+
```python
71
+
from agents import Agent, RunConfig, Runner, SQLiteSession
72
+
73
+
74
+
defkeep_recent_history(history, new_input):
75
+
# Keep only the last 10 history items, then append the new turn.
If your session implementation exposes default session settings, `RunConfig.session_settings` overrides any non-`None` values for that run. This is useful for long conversations where you want to cap retrieval size without changing the session's default behavior.
0 commit comments