Skip to content

Commit 176dde9

Browse files
authored
docs: add missing changes (#2526)
1 parent 2fee7ed commit 176dde9

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

docs/running_agents.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,67 @@ The `run_config` parameter lets you configure some global settings for the agent
6262
- [`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.
6363
- [`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.
6464
- [`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.
6566

6667
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).
6768

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
88+
89+
90+
def format_rejection(args: ToolErrorFormatterArgs[None]) -> str | None:
91+
if args.kind == "approval_rejected":
92+
return (
93+
f"Tool call '{args.tool_name}' was rejected by a human reviewer. "
94+
"Ask for confirmation or propose a safer alternative."
95+
)
96+
return None
97+
98+
99+
agent = Agent(name="Assistant")
100+
result = Runner.run_sync(
101+
agent,
102+
"Please delete the production database.",
103+
run_config=RunConfig(tool_error_formatter=format_rejection),
104+
)
105+
```
106+
107+
#### `reasoning_item_id_policy`
108+
109+
`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+
68126
## Conversations/chat threads
69127

70128
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():
193251
print(f"Assistant: {result.final_output}")
194252
```
195253

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+
196264
## Call model input filter
197265

198266
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`.

docs/sessions/index.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,67 @@ When session memory is enabled:
5353

5454
This eliminates the need to manually call `.to_input_list()` and manage conversation state between runs.
5555

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+
def keep_recent_history(history, new_input):
75+
# Keep only the last 10 history items, then append the new turn.
76+
return history[-10:] + new_input
77+
78+
79+
agent = Agent(name="Assistant")
80+
session = SQLiteSession("conversation_123")
81+
82+
result = await Runner.run(
83+
agent,
84+
"Continue from the latest updates only.",
85+
session=session,
86+
run_config=RunConfig(session_input_callback=keep_recent_history),
87+
)
88+
```
89+
90+
Use this when you need custom pruning, reordering, or selective inclusion of history without changing how the session stores items.
91+
92+
## Limiting retrieved history
93+
94+
Use [`SessionSettings`][agents.memory.SessionSettings] to control how much history is fetched before each run.
95+
96+
- `SessionSettings(limit=None)` (default): retrieve all available session items
97+
- `SessionSettings(limit=N)`: retrieve only the most recent `N` items
98+
99+
You can apply this per run via [`RunConfig.session_settings`][agents.run.RunConfig.session_settings]:
100+
101+
```python
102+
from agents import Agent, RunConfig, Runner, SessionSettings, SQLiteSession
103+
104+
agent = Agent(name="Assistant")
105+
session = SQLiteSession("conversation_123")
106+
107+
result = await Runner.run(
108+
agent,
109+
"Summarize our recent discussion.",
110+
session=session,
111+
run_config=RunConfig(session_settings=SessionSettings(limit=50)),
112+
)
113+
```
114+
115+
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.
116+
56117
## Memory operations
57118

58119
### Basic operations

0 commit comments

Comments
 (0)