Backtestsopenai-agents-pythonrun-20261024-184640#2020
Item with id rs_ not found
The merged fix was judged better than our patch.
In this judgment, Patch A = our agent, Patch B = the merged fix.
The issue is specifically about SQLiteSession replay: a triage agent's empty reasoning item is persisted, every stored item is sent back each turn, and a later turn 404s on the stale rs_ id. The existing `reasoning_item_id_policy="omit"` only applied when the SDK built follow-up input and (per Patch B's comment) on session write, so history already in the database still carried ids and the mitigation could not rescue an existing conversation. Patch B closes exactly that gap: it threads the resolved policy into `prepare_input_with_session` (both `Runner.run` and the streamed path, including the resumed-state branch), strips ids from converted session history on read, and correctly moves the policy resolution ahead of session preparation in run.py so it is available when needed. Its tests simulate a realistic server-side 404 for a revoked id, store the item in a session before opting in, and verify the replayed input no longer carries the id, in both streamed and non-streamed modes. The independent session-replay tests fail on base and Patch A and pass only with B, matching the issue's stated configuration. Patch A instead threads the policy through nested handoff history (which only forwards raw reasoning items when a custom `handoff_history_mapper` is supplied) and into error-handler `history`/`output` data; neither is the path the issue describes (default session management, no custom mapper), and the error-handler data is not sent to the model at all. A's tests are solid and it fixes a real adjacent gap, but it leaves the reported session scenario broken, as the independent tests confirm. B is also smaller and more targeted. Neither patch changes user-supplied initial input (the issue's literal repro), but that is documented behaviour and applies equally to both.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
10m 18s
Wall-clock time in the container
Full test suite
No new failures
1,698 passed · 1 skipped · 1,699 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
263vs 217 merged
Changed lines, ours vs merged
The issue
View on GitHubPlease read this first
- Have you read the docs? Agents SDK docs
- Have you searched for related issues? Others may have faced similar issues.
Describe the bug
In certain scenarios, the Agents SDK returns the following error:
Example:
openai.NotFoundError: Error code: 404 - {'error': {'message': "Item with id 'rs_3c4caff36e34c099006900f0e3c66481909c15ac262a15b060' not found.", 'type': 'invalid_request_error', 'param': 'input', 'code': None}}
Configuration:
- Store is not set to
False - Using default session management:
SQLiteSession - All items stored in the database are sent back to the agent for each turn
- Multi-agent system architecture with handoffs:
- Agent 1:
gpt-5with minimal reasoning (acts as the triage agent) - Agent 2:
gpt-5with medium reasoning - Agent 3:
gpt-5with low reasoning
Issue:
Agent 1 generates an empty reasoning item as shown below:
8|f00b2f3b-770b-4f40-9ca3-ae7700940a19_6555bacd-f286-42b3-9f25-e7a9ae94e3de|{"id":"rs_3c4caff36e34c099006900f0e3c66481909c15ac262a15b060","summary":[],"type":"reasoning"}|2025-10-28 16:36:16
After several steps (including outputs produced by Agent 2 and Agent 3), when Agent 1 regains control and any new message is passed, the error above is triggered.
Debug information
- Agents SDK version:
0.2.8 - Python version: Python 3.11.6
Repro steps
While the code that triggers this error in our system involves multiple agents and handoffs, the following simplified code reproduces the same error:
import asyncio
import random
from agents import (
Agent,
ItemHelpers,
Runner,
enable_verbose_stdout_logging,
function_tool,
)
enable_verbose_stdout_logging()
@function_tool
def how_many_jokes() -> int:
"""Return a random integer of jokes to tell between 1 and 10 (inclusive)."""
return random.randint(1, 10)
ORIGINAL_RS_ID = "rs_07a11282cddc7074006905172505c88198bd41b4889a8d5649"
def mangle_rs_id(original_rs_id: str) -> str:
return original_rs_id[:-2] + "ff"
async def main():
agent = Agent(
name="Joker",
instructions="Call the `how_many_jokes` tool once, and tell that many jokes.",
tools=[how_many_jokes],
model="gpt-5-nano",
)
rs_id = mangle_rs_id(ORIGINAL_RS_ID)
input_list = [
{"content": "We want to hear jokes", "role": "user"},
{
"id": rs_id,
"summary": [],
"type": "reasoning",
},
{
"arguments": "{}",
"call_id": "call_xkkWToQpzfvn0asJfDPo06Wj",
"name": "how_many_jokes",
"type": "function_call",
"id": "fc_07a11282cddc707400690517253a148198a2dad52c744c22ce",
"status": "completed",
},
{
"call_id": "call_xkkWToQpzfvn0asJfDPo06Wj",
"output": "7",
"type": "function_call_output",
},
]
result = Runner.run_streamed(
agent,
input=input_list,
)
async for event in result.stream_events():
if (
event.type == "run_item_stream_event"
and event.item.type == "message_output_item"
):
print(f"-- Message output:\n {ItemHelpers.text_message_output(event.item)}")
if __name__ == "__main__":
asyncio.run(main())
Expected behavior
The SDK should not error out, as we are not modifying or altering any reasoning items.
The two patches
docs/running_agents.mdindex 3b0643ee..7484788c 100644@@ -265,7 +265,7 @@ Setting `reasoning_item_id_policy="omit"` keeps the reasoning content but strips Scope notes: -- This only changes reasoning items generated/forwarded by the SDK when it builds follow-up input.+- This only changes reasoning items generated/forwarded by the SDK when it builds follow-up input, including nested handoff transcripts passed to `handoff_history_mapper` and the `history`/`output` passed to error handlers. - It does not rewrite user-supplied initial input items. - `call_model_input_filter` can still intentionally reintroduce reasoning IDs after this policy is applied. src/agents/handoffs/history.pyindex 9b14dbc7..2b2ba373 100644@@ -15,7 +15,7 @@ from ..items import ( ) if TYPE_CHECKING:- from ..run_internal.items import NestedHistoryOwnedItem+ from ..run_internal.items import NestedHistoryOwnedItem, ReasoningItemIdPolicy from . import HandoffHistoryMapper, HandoffInputData __all__ = [@@ -84,12 +84,14 @@ def nest_handoff_history( handoff_input_data: HandoffInputData, *, history_mapper: HandoffHistoryMapper | None = None,+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None, ) -> HandoffInputData: """Summarize the previous transcript for the next agent.""" nested, _ = _nest_handoff_history_with_provenance( handoff_input_data, history_mapper=history_mapper,+ reasoning_item_id_policy=reasoning_item_id_policy, ) return nested @@ -98,6 +100,7 @@ def _nest_handoff_history_with_provenance( handoff_input_data: HandoffInputData, *, history_mapper: HandoffHistoryMapper | None = None,+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None, ) -> tuple[HandoffInputData, tuple[NestedHistoryOwnedItem, ...]]: """Return nested input and exact provenance for items moved into default history.""" @@ -112,7 +115,7 @@ def _nest_handoff_history_with_provenance( for run_item in handoff_input_data.pre_handoff_items: if isinstance(run_item, ToolApprovalItem): continue- plain_input = _run_item_to_plain_input(run_item)+ plain_input = _run_item_to_plain_input(run_item, reasoning_item_id_policy) forward_verbatim = _should_forward_pre_item(plain_input) normalized_pre_items.append((plain_input, forward_verbatim, run_item)) @@ -120,7 +123,7 @@ def _nest_handoff_history_with_provenance( for run_item in handoff_input_data.new_items: if isinstance(run_item, ToolApprovalItem): continue- plain_input = _run_item_to_plain_input(run_item)+ plain_input = _run_item_to_plain_input(run_item, reasoning_item_id_policy) forward_verbatim = _should_forward_new_item(plain_input) normalized_new_items.append((plain_input, forward_verbatim, run_item)) @@ -325,10 +328,13 @@ def _normalize_input_history( return [deepcopy(item) for item in input_history] -def _run_item_to_plain_input(run_item: RunItem) -> TResponseInputItem:+def _run_item_to_plain_input(+ run_item: RunItem,+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None,+) -> TResponseInputItem: from ..run_internal.items import run_item_to_input_item - input_item = run_item_to_input_item(run_item)+ input_item = run_item_to_input_item(run_item, reasoning_item_id_policy) if input_item is None: raise TypeError(f"Unsupported nested handoff run item: {run_item.type}") return deepcopy(input_item)src/agents/run_internal/run_loop.pyindex 1429270c..38edea09 100644@@ -1913,6 +1913,7 @@ async def run_single_turn_streamed( tool_use_tracker=tool_use_tracker, server_manages_conversation=server_conversation_tracker is not None, event_queue=streamed_result._event_queue,+ reasoning_item_id_policy=reasoning_item_id_policy, before_side_effects=raise_if_input_guardrail_tripwire_known, ) @@ -2060,6 +2061,7 @@ async def run_single_turn( error_handlers=error_handlers, tool_use_tracker=tool_use_tracker, server_manages_conversation=server_conversation_tracker is not None,+ reasoning_item_id_policy=reasoning_item_id_policy, ) src/agents/run_internal/turn_resolution.pyindex a30372f7..2a095523 100644@@ -128,6 +128,7 @@ from .error_handlers import ( from .items import ( REJECTION_MESSAGE, NestedHistoryOwnedItem,+ ReasoningItemIdPolicy, apply_patch_rejection_item, extract_mcp_request_id_from_run, function_rejection_item,@@ -422,6 +423,7 @@ async def _resolve_invalid_final_output( new_response: ModelResponse, new_items: list[RunItem], context_wrapper: RunContextWrapper[TContext],+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None, ) -> tuple[Any, MessageOutputItem | None] | None: redacted = _is_error_data_redacted(error) or _debug.DONT_LOG_MODEL_DATA run_error_data = build_run_error_data(@@ -429,6 +431,7 @@ async def _resolve_invalid_final_output( new_items=new_items, raw_responses=[new_response], last_agent=public_agent,+ reasoning_item_id_policy=reasoning_item_id_policy, ) _detach_data_redacted_error_traceback(error) safe_error: UserError | None = None@@ -529,6 +532,7 @@ async def execute_handoffs( nest_handoff_history_fn: Callable[..., HandoffInputData] | None = None, tool_input_guardrail_results: list[ToolInputGuardrailResult] | None = None, tool_output_guardrail_results: list[ToolOutputGuardrailResult] | None = None,+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None, ) -> SingleStepResult: """Execute a handoff and prepare the next turn for the new agent.""" @@ -543,6 +547,7 @@ async def execute_handoffs( nested, history_owned_items = _nest_handoff_history_with_provenance( data, history_mapper=mapper,+ reasoning_item_id_policy=reasoning_item_id_policy, ) return nested, list(history_owned_items) if nest_handoff_history_fn is not None:@@ -771,6 +776,7 @@ async def execute_tools_and_side_effects( run_config: RunConfig, error_handlers: RunErrorHandlers[TContext] | None = None, server_manages_conversation: bool = False,+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None, ) -> SingleStepResult: """Run one turn of the loop, coordinating tools, approvals, guardrails, and handoffs.""" public_agent = bindings.public_agent@@ -875,6 +881,7 @@ async def execute_tools_and_side_effects( server_manages_conversation=server_manages_conversation, tool_input_guardrail_results=tool_input_guardrail_results, tool_output_guardrail_results=tool_output_guardrail_results,+ reasoning_item_id_policy=reasoning_item_id_policy, ) tool_final_output = await _maybe_finalize_from_tool_results(@@ -910,6 +917,7 @@ async def execute_tools_and_side_effects( new_items=pre_step_items + new_step_items, raw_responses=[new_response], last_agent=public_agent,+ reasoning_item_id_policy=reasoning_item_id_policy, ) handler_result = await resolve_run_error_handler_result( error_handlers=error_handlers,@@ -956,6 +964,7 @@ async def execute_tools_and_side_effects( new_response=new_response, new_items=pre_step_items + new_step_items, context_wrapper=context_wrapper,+ reasoning_item_id_policy=reasoning_item_id_policy, ) if resolved_handler_output is None: raise@@ -972,6 +981,7 @@ async def execute_tools_and_side_effects( new_response=new_response, new_items=pre_step_items + new_step_items, context_wrapper=context_wrapper,+ reasoning_item_id_policy=reasoning_item_id_policy, ) if resolved_handler_output is None: raise validation_error@@ -989,6 +999,7 @@ async def execute_tools_and_side_effects( new_response=new_response, new_items=pre_step_items + new_step_items, context_wrapper=context_wrapper,+ reasoning_item_id_policy=reasoning_item_id_policy, ) if resolved_handler_output is None: return SingleStepResult(@@ -2220,6 +2231,11 @@ async def resolve_interrupted_turn( nest_handoff_history_fn=nest_handoff_history_fn, tool_input_guardrail_results=tool_input_guardrail_results, tool_output_guardrail_results=tool_output_guardrail_results,+ reasoning_item_id_policy=(+ run_state._reasoning_item_id_policy+ if run_state is not None+ else run_config.reasoning_item_id_policy+ ), ) ) @@ -2991,6 +3007,7 @@ async def get_single_step_result_from_response( server_manages_conversation: bool = False, event_queue: asyncio.Queue[StreamEvent | QueueCompleteSentinel] | None = None, before_side_effects: Callable[[], Awaitable[None]] | None = None,+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None, ) -> SingleStepResult: item_agent = bindings.public_agent processed_response = process_model_response(@@ -3033,4 +3050,5 @@ async def get_single_step_result_from_response( run_config=run_config, error_handlers=error_handlers, server_manages_conversation=server_manages_conversation,+ reasoning_item_id_policy=reasoning_item_id_policy, )tests/test_reasoning_item_id_policy_paths.pynew file mode 100644index 00000000..a7f1b8fa@@ -0,0 +1,225 @@+"""Regression tests: ``reasoning_item_id_policy="omit"`` must apply on every path that turns+prior reasoning run items into model input or error-handler history.++A reasoning item replayed with its ``rs_...`` id is resolved server-side by that id, which fails+with ``Item with id 'rs_...' not found`` when the provider no longer has it.+"""++from __future__ import annotations++import json+from typing import Any, cast++import pytest+from openai.types.responses import ResponseReasoningItem+from pydantic import BaseModel++from agents import (+ Agent,+ RunConfig,+ RunErrorHandlerInput,+ Runner,+ RunState,+ TResponseInputItem,+ function_tool,+)++from .fake_model import FakeModel+from .test_responses import (+ get_function_tool_call,+ get_handoff_tool_call,+ get_refusal_message,+ get_text_message,+)+++def _reasoning_items(items: Any) -> list[dict[str, Any]]:+ if not isinstance(items, list):+ return []+ return [+ cast(dict[str, Any], item)+ for item in items+ if isinstance(item, dict) and item.get("type") == "reasoning"+ ]+++def _passthrough_mapper(transcript: list[TResponseInputItem]) -> list[TResponseInputItem]:+ return list(transcript)+++def _empty_reasoning(item_id: str) -> ResponseReasoningItem:+ # Mirrors the empty reasoning item minimal-effort models emit before a handoff.+ return ResponseReasoningItem(id=item_id, type="reasoning", summary=[])+++async def _run(agent: Agent[Any], input: Any, streamed: bool, **kwargs: Any) -> Any:+ if streamed:+ result = Runner.run_streamed(agent, input, **kwargs)+ async for _ in result.stream_events():+ pass+ return result+ return await Runner.run(agent, input, **kwargs)+++@pytest.mark.parametrize("streamed", [False, True])+@pytest.mark.asyncio+async def test_nested_handoff_history_mapper_omits_reasoning_ids(streamed: bool) -> None:+ target_model = FakeModel()+ target_model.set_next_output([get_text_message("done")])+ target = Agent(name="target", model=target_model)++ triage_model = FakeModel()+ triage = Agent(name="triage", model=triage_model, handoffs=[target])+ triage_model.set_next_output([_empty_reasoning("rs_triage"), get_handoff_tool_call(target)])++ result = await _run(+ triage,+ "hello",+ streamed,+ run_config=RunConfig(+ nest_handoff_history=True,+ handoff_history_mapper=_passthrough_mapper,+ reasoning_item_id_policy="omit",+ ),+ )++ assert result.final_output == "done"+ reasoning = _reasoning_items(target_model.last_turn_args["input"])+ assert reasoning == [{"summary": [], "type": "reasoning"}]+++@pytest.mark.parametrize("streamed", [False, True])+@pytest.mark.asyncio+async def test_nested_handoff_history_mapper_preserves_reasoning_ids_by_default(+ streamed: bool,+) -> None:+ target_model = FakeModel()+ target_model.set_next_output([get_text_message("done")])+ target = Agent(name="target", model=target_model)++ triage_model = FakeModel()+ triage = Agent(name="triage", model=triage_model, handoffs=[target])+ triage_model.set_next_output([_empty_reasoning("rs_triage"), get_handoff_tool_call(target)])++ result = await _run(+ triage,+ "hello",+ streamed,+ run_config=RunConfig(+ nest_handoff_history=True,+ handoff_history_mapper=_passthrough_mapper,+ ),+ )++ assert result.final_output == "done"+ reasoning = _reasoning_items(target_model.last_turn_args["input"])+ assert [item.get("id") for item in reasoning] == ["rs_triage"]+++@pytest.mark.asyncio+async def test_resumed_handoff_uses_serialized_reasoning_item_id_policy() -> None:+ @function_tool(name_override="approval_tool", needs_approval=True)+ def approval_tool() -> str:+ return "ok"++ target_model = FakeModel()+ target_model.set_next_output([get_text_message("done")])+ target = Agent(name="target", model=target_model)++ triage_model = FakeModel()+ triage = Agent(name="triage", model=triage_model, tools=[approval_tool], handoffs=[target])+ triage_model.set_next_output(+ [+ _empty_reasoning("rs_resume"),+ get_function_tool_call("approval_tool", json.dumps({}), call_id="call_resume"),+ get_handoff_tool_call(target),+ ]+ )++ first_run = await Runner.run(+ triage,+ "hello",+ run_config=RunConfig(+ nest_handoff_history=True,+ handoff_history_mapper=_passthrough_mapper,+ reasoning_item_id_policy="omit",+ ),+ )+ assert len(first_run.interruptions) == 1++ state = first_run.to_state()+ state.approve(first_run.interruptions[0])+ restored_state = await RunState.from_string(triage, state.to_string())++ # The resumed run does not repeat the policy; it must come from the serialized state.+ resumed = await Runner.run(+ triage,+ restored_state,+ run_config=RunConfig(+ nest_handoff_history=True,+ handoff_history_mapper=_passthrough_mapper,+ ),+ )++ assert resumed.final_output == "done"+ reasoning = _reasoning_items(target_model.last_turn_args["input"])+ assert len(reasoning) == 1+ assert "id" not in reasoning[0]+++@pytest.mark.parametrize("streamed", [False, True])+@pytest.mark.asyncio+async def test_model_refusal_error_data_omits_reasoning_ids(streamed: bool) -> None:+ model = FakeModel()+ model.set_next_output([_empty_reasoning("rs_refusal"), get_refusal_message("no")])+ agent = Agent(name="agent", model=model)+ captured: list[RunErrorHandlerInput[Any]] = []++ def on_refusal(data: RunErrorHandlerInput[Any]) -> str:+ captured.append(data)+ return "fallback"++ result = await _run(+ agent,+ "hello",+ streamed,+ run_config=RunConfig(reasoning_item_id_policy="omit"),+ error_handlers={"model_refusal": on_refusal},+ )++ assert result.final_output == "fallback"+ assert len(captured) == 1+ run_data = captured[0].run_data+ for items in (run_data.history, run_data.output):+ assert _reasoning_items(items) == [{"summary": [], "type": "reasoning"}]+++class _Answer(BaseModel):+ answer: str+++@pytest.mark.parametrize("streamed", [False, True])+@pytest.mark.asyncio+async def test_invalid_final_output_error_data_omits_reasoning_ids(streamed: bool) -> None:+ model = FakeModel()+ model.set_next_output([_empty_reasoning("rs_invalid"), get_text_message("not json")])+ agent = Agent(name="agent", model=model, output_type=_Answer)+ captured: list[RunErrorHandlerInput[Any]] = []++ def on_invalid(data: RunErrorHandlerInput[Any]) -> _Answer:+ captured.append(data)+ return _Answer(answer="fallback")++ result = await _run(+ agent,+ "hello",+ streamed,+ run_config=RunConfig(reasoning_item_id_policy="omit"),+ error_handlers={"invalid_final_output": on_invalid},+ )++ assert result.final_output == _Answer(answer="fallback")+ assert len(captured) == 1+ run_data = captured[0].run_data+ for items in (run_data.history, run_data.output):+ assert _reasoning_items(items) == [{"summary": [], "type": "reasoning"}]
src/agents/run.pyindex 7ac81be07..6831334bf 100644@@ -523,7 +523,16 @@ class AgentRunner: run_config = RunConfig() if run_config is None else _coerce_run_config(run_config) is_resumed_state = isinstance(input, RunState)- run_state: RunState[TContext] | None = None+ run_state: RunState[TContext] | None = (+ cast(RunState[TContext], input) if is_resumed_state else None+ )+ resolved_reasoning_item_id_policy: ReasoningItemIdPolicy | None = (+ run_config.reasoning_item_id_policy+ if run_config.reasoning_item_id_policy is not None+ else (run_state._reasoning_item_id_policy if run_state is not None else None)+ )+ if run_state is not None:+ run_state._reasoning_item_id_policy = resolved_reasoning_item_id_policy starting_input = input if not is_resumed_state else None original_user_input: str | list[TResponseInputItem] | None = None session_input_items_for_persistence: list[TResponseInputItem] | None = (@@ -533,8 +542,7 @@ class AgentRunner: # exactly those items (and not the full history). last_saved_input_snapshot_for_rewind: list[TResponseInputItem] | None = None - if is_resumed_state:- run_state = cast(RunState[TContext], input)+ if is_resumed_state and run_state is not None: ( conversation_id, previous_response_id,@@ -590,6 +598,7 @@ class AgentRunner: run_config.session_settings, include_history_in_prepared_input=False, preserve_dropped_new_items=True,+ reasoning_item_id_policy=resolved_reasoning_item_id_policy, wrapper=context_wrapper, ) original_input_for_state = raw_input@@ -603,18 +612,11 @@ class AgentRunner: session, run_config.session_input_callback, run_config.session_settings,+ reasoning_item_id_policy=resolved_reasoning_item_id_policy, wrapper=context_wrapper, ) original_input_for_state = prepared_input - resolved_reasoning_item_id_policy: ReasoningItemIdPolicy | None = (- run_config.reasoning_item_id_policy- if run_config.reasoning_item_id_policy is not None- else (run_state._reasoning_item_id_policy if run_state is not None else None)- )- if run_state is not None:- run_state._reasoning_item_id_policy = resolved_reasoning_item_id_policy- # Check whether to enable OpenAI server-managed conversation if ( conversation_id is not Nonesrc/agents/run_internal/items.pyindex 9a4c0ea6b..ad9bb25ca 100644@@ -60,6 +60,7 @@ __all__ = [ "REJECTION_MESSAGE", "TOOL_CALL_SESSION_DESCRIPTION_KEY", "TOOL_CALL_SESSION_TITLE_KEY",+ "apply_reasoning_item_id_policy", "copy_input_items", "drop_orphan_function_calls", "ensure_input_item_format",@@ -697,6 +698,16 @@ def strip_internal_input_item_metadata(item: TResponseInputItem) -> TResponseInp return cast(TResponseInputItem, cleaned) +def apply_reasoning_item_id_policy(+ items: list[TResponseInputItem],+ reasoning_item_id_policy: ReasoningItemIdPolicy | None,+) -> list[TResponseInputItem]:+ """Apply the reasoning item ID policy to already-converted input items."""+ if not _should_omit_reasoning_item_ids(reasoning_item_id_policy):+ return items+ return [_without_reasoning_item_id(item) for item in items]++ def _should_omit_reasoning_item_ids(reasoning_item_id_policy: ReasoningItemIdPolicy | None) -> bool: return reasoning_item_id_policy == "omit" src/agents/run_internal/run_loop.pyindex 1429270c2..c2d1c08ea 100644@@ -779,6 +779,7 @@ async def start_streaming( run_config.session_settings, include_history_in_prepared_input=not server_manages_conversation, preserve_dropped_new_items=True,+ reasoning_item_id_policy=resolved_reasoning_item_id_policy, wrapper=context_wrapper, ) streamed_result.input = prepared_inputsrc/agents/run_internal/session_persistence.pyindex ae4a369a5..fee753b40 100644@@ -36,6 +36,7 @@ from .items import ( NestedHistoryOwnedItem, NestedHistoryOwnedItemRef, ReasoningItemIdPolicy,+ apply_reasoning_item_id_policy, copy_input_items, deduplicate_input_items_preferring_latest, digest_input_item,@@ -206,6 +207,7 @@ async def prepare_input_with_session( *, include_history_in_prepared_input: bool = True, preserve_dropped_new_items: bool = False,+ reasoning_item_id_policy: ReasoningItemIdPolicy | None = None, wrapper: RunContextWrapper[Any] | None = None, ) -> tuple[str | list[TResponseInputItem], list[TResponseInputItem]]: """Prepare model input from session history plus the new turn input.@@ -242,6 +244,13 @@ async def prepare_input_with_session( converted_history = [ strip_internal_input_item_metadata(ensure_input_item_format(item)) for item in history ]+ if not is_openai_conversation_session:+ # History written before the caller opted into "omit" still carries server-assigned+ # reasoning IDs. Apply the policy on read too, the same way `save_result_to_session`+ # applies it on write, so replaying that history cannot 404 on a stale `rs_...` ID.+ converted_history = apply_reasoning_item_id_policy(+ converted_history, reasoning_item_id_policy+ ) new_input_list = [ ensure_input_item_format(item) for item in ItemHelpers.input_to_new_input_list(input)tests/test_agent_runner.pyindex d37e8cf60..b2512b6ab 100644@@ -12,7 +12,7 @@ from unittest.mock import patch import httpx import pytest-from openai import APIConnectionError, BadRequestError+from openai import APIConnectionError, BadRequestError, NotFoundError from openai.types.responses import ResponseFunctionToolCall from openai.types.responses.response_output_item import McpApprovalRequest from openai.types.responses.response_output_text import AnnotationFileCitation, ResponseOutputText@@ -1231,6 +1231,89 @@ async def test_call_model_input_filter_can_reintroduce_reasoning_ids() -> None: assert "id" not in history_reasoning +class _RevokedReasoningIdModel(FakeModel):+ """FakeModel that 404s like the Responses API when a revoked reasoning ID is replayed."""++ def __init__(self) -> None:+ super().__init__()+ self.revoked_reasoning_ids: set[str] = set()++ async def get_response(+ self,+ system_instructions: str | None,+ input: str | list[TResponseInputItem],+ *args: Any,+ **kwargs: Any,+ ) -> ModelResponse:+ if isinstance(input, list):+ for item in input:+ if not isinstance(item, dict) or item.get("type") != "reasoning":+ continue+ item_id = item.get("id")+ if item_id in self.revoked_reasoning_ids:+ message = f"Item with id '{item_id}' not found."+ body = {"error": {"message": message, "type": "invalid_request_error"}}+ raise NotFoundError(+ message,+ response=httpx.Response(+ 404,+ request=httpx.Request("POST", "https://api.openai.com/v1/responses"),+ json=body,+ ),+ body=body,+ )+ return await super().get_response(system_instructions, input, *args, **kwargs)+++@pytest.mark.asyncio+async def test_omit_policy_strips_reasoning_ids_already_stored_in_the_session() -> None:+ """Adopting `omit` must also cover reasoning IDs a session recorded before it was set.++ Reproduces https://github.com/openai/openai-agents-python/issues/2020: a triage agent hands+ off, its empty-summary reasoning item is persisted to the session, the server later drops the+ item, and every later turn of that conversation fails with+ `404 Item with id 'rs_...' not found`.+ """+ model = _RevokedReasoningIdModel()+ specialist = Agent(name="specialist", model=model)+ triage = Agent(name="triage", model=model, handoffs=[specialist])++ session = SQLiteSession("issue-2020")++ # Turn 1 predates the mitigation, so the session records the reasoning ID.+ model.add_multiple_turn_outputs(+ [+ [+ ResponseReasoningItem(id="rs_triage", type="reasoning", summary=[]),+ get_handoff_tool_call(specialist),+ ],+ [get_text_message("handled")],+ ]+ )+ first = await Runner.run(triage, input="hello", session=session)+ assert first.final_output == "handled"+ stored_reasoning = _find_reasoning_input_item(await session.get_items())+ assert stored_reasoning is not None+ assert stored_reasoning.get("id") == "rs_triage"++ # The server no longer resolves that reasoning item.+ model.revoked_reasoning_ids.add("rs_triage")++ # Turn 2 opts into the documented mitigation for this failure.+ model.add_multiple_turn_outputs([[get_text_message("done")]])+ second = await Runner.run(+ triage,+ input="anything else?",+ session=session,+ run_config=RunConfig(reasoning_item_id_policy="omit"),+ )++ assert second.final_output == "done"+ replayed_reasoning = _find_reasoning_input_item(model.last_turn_args.get("input"))+ assert replayed_reasoning is not None+ assert "id" not in replayed_reasoning++ @pytest.mark.asyncio async def test_resumed_run_uses_serialized_reasoning_item_id_policy() -> None: model = FakeModel()tests/test_agent_runner_streamed.pyindex 2a3c60581..c09ccb957 100644@@ -8,7 +8,7 @@ from typing import Any, cast import httpx import pytest-from openai import APIConnectionError, BadRequestError+from openai import APIConnectionError, BadRequestError, NotFoundError from openai.types.responses import ( ResponseCompletedEvent, ResponseErrorEvent,@@ -36,6 +36,7 @@ from agents import ( OutputGuardrailTripwireTriggered, RunContextWrapper, Runner,+ SQLiteSession, ToolGuardrailFunctionOutput, ToolInputGuardrailData, ToolOutputGuardrailData,@@ -830,6 +831,90 @@ async def test_streamed_reasoning_item_id_policy_omits_follow_up_reasoning_ids() assert "id" not in history_reasoning +class _StreamedRevokedReasoningIdModel(FakeModel):+ """FakeModel that 404s like the Responses API when a revoked reasoning ID is replayed."""++ def __init__(self) -> None:+ super().__init__()+ self.revoked_reasoning_ids: set[str] = set()++ def stream_response(+ self,+ system_instructions: str | None,+ input: str | list[TResponseInputItem],+ *args: Any,+ **kwargs: Any,+ ) -> AsyncIterator[TResponseStreamEvent]:+ if isinstance(input, list):+ for item in input:+ if not isinstance(item, dict) or item.get("type") != "reasoning":+ continue+ item_id = item.get("id")+ if item_id in self.revoked_reasoning_ids:+ message = f"Item with id '{item_id}' not found."+ body = {"error": {"message": message, "type": "invalid_request_error"}}+ raise NotFoundError(+ message,+ response=httpx.Response(+ 404,+ request=httpx.Request("POST", "https://api.openai.com/v1/responses"),+ json=body,+ ),+ body=body,+ )+ return super().stream_response(system_instructions, input, *args, **kwargs)+++@pytest.mark.asyncio+async def test_streamed_omit_policy_strips_reasoning_ids_already_stored_in_the_session() -> None:+ """Adopting `omit` must also cover reasoning IDs a session recorded before it was set.++ Streaming counterpart of the non-streamed regression test for+ https://github.com/openai/openai-agents-python/issues/2020.+ """+ model = _StreamedRevokedReasoningIdModel()+ specialist = Agent(name="specialist", model=model)+ triage = Agent(name="triage", model=model, handoffs=[specialist])+ session = SQLiteSession("issue-2020-streamed")++ # Turn 1 predates the mitigation, so the session records the reasoning ID.+ model.add_multiple_turn_outputs(+ [+ [+ ResponseReasoningItem(id="rs_triage", type="reasoning", summary=[]),+ get_handoff_tool_call(specialist),+ ],+ [get_text_message("handled")],+ ]+ )+ first = Runner.run_streamed(triage, input="hello", session=session)+ async for _ in first.stream_events():+ pass+ assert first.final_output == "handled"+ stored_reasoning = _find_reasoning_input_item(await session.get_items())+ assert stored_reasoning is not None+ assert stored_reasoning.get("id") == "rs_triage"++ # The server no longer resolves that reasoning item.+ model.revoked_reasoning_ids.add("rs_triage")++ # Turn 2 opts into the documented mitigation for this failure.+ model.add_multiple_turn_outputs([[get_text_message("done")]])+ second = Runner.run_streamed(+ triage,+ input="anything else?",+ session=session,+ run_config=RunConfig(reasoning_item_id_policy="omit"),+ )+ async for _ in second.stream_events():+ pass++ assert second.final_output == "done"+ replayed_reasoning = _find_reasoning_input_item(model.last_turn_args.get("input"))+ assert replayed_reasoning is not None+ assert "id" not in replayed_reasoning++ @pytest.mark.asyncio async def test_streamed_run_again_persists_tool_items_to_session(): model = FakeModel()