Backtestsopenai-agents-pythonrun-20261024-184640#4322
SessionSettings(limit=...) can pass an unmatched function_call_output to the model
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the reported scenario at the right layer (drop_orphan_function_calls, gated on a configured limit, restricted to history-origin indexes so caller-provided outputs survive), and both pass every independent test. The differences are at the edges. Patch A adds a single additive flag (`drop_orphan_outputs`) and leaves the existing filtering loop intact; orphan outputs are dropped only for items already in `pruning_indexes`, which the existing code computes for both the no-callback path and the session_input_callback path. That makes output pruning consistent with the pre-existing orphan-call pruning: a user who combines `SessionSettings(limit=n)` with a `session_input_callback` still gets the fix, and A tests that explicitly. Its unit tests also cover type-mismatched call_ids, hosted tool_search pairs, and the interaction with reasoning-item dropping. Patch B deliberately disables output pruning when a session_input_callback is present (`session_input_callback is None and limit is not None`) and adds a test locking that in, so the issue's bug still reproduces for limit + callback, even though the same function already prunes orphan calls from callback history. B does handle one extra edge better (an output whose call was dropped alongside an orphan program, via excluding dropped indexes when computing available call ids), but that scenario arises from an interrupted run rather than limit truncation, which is the issue's subject. B also rewrites the main loop (removing the `filtered` accumulator and changing the private reasoning-helper signature) and rewrites an existing test, which is more churn than the fix needs. A's minor gap (present call ids are computed before orphan-call dropping) only matters in the program-owned edge case outside the issue. Weighing correctness in the issue's scenarios, test coverage of the limit-related paths, and minimal scope, A is the better fix by a modest margin.
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
12m 2s
Wall-clock time in the container
Full test suite
No new failures
909 passed · 0 skipped · 909 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
255vs 311 merged
Changed lines, ours vs merged
The issue
View on GitHubDescribe the bug
When SessionSettings(limit=n) truncates stored history between a function_call and its corresponding function_call_output, the Runner can pass the output to the model without the matching call.
For example, given this stored history:
- user message
function_callwithcall_id="call_1"function_call_outputwithcall_id="call_1"- assistant message
Using SessionSettings(limit=2) correctly retrieves the last two stored items, but the prepared model input still contains the unmatched function_call_output.
The existing model-input sanitization removes a retained call when its output is absent, but does not remove a retained output when its call is absent.
This report does not propose changing the exact item-count semantics of Session.get_items(limit=n) or introducing run/turn-aware session storage. The issue is limited to ensuring that the model input does not contain one side of a required call/output pair after history pruning.
Debug information
- Agents SDK version: 0.19.4
- Related library versions:
openai==2.45.0,pydantic==2.12.3 - Python version: 3.12.9
- Operating system: macOS 26.6
- Model and model provider: Local
CaptureModel; no external provider is required to observe the prepared input - Does the issue reproduce with the latest Agents SDK release? Yes
- Does the issue occur consistently or intermittently? Consistently when the limit boundary excludes the call but retains its output
No exception is raised by the local capture model. The malformed dependency is visible in the input prepared for the model:
[
{
"type": "function_call_output",
"call_id": "call_1",
"output": "sunny"
},
{
"type": "message",
"role": "assistant",
"content": [...]
},
{
"content": "What about tomorrow?",
"role": "user"
}
]
Repro steps
This script uses a local capture model and makes no external API requests.
import asyncio
import json
from collections.abc import AsyncIterator
from typing import Any
from openai.types.responses import ResponseOutputMessage, ResponseOutputText
from agents import (
Agent,
RunConfig,
Runner,
SessionSettings,
SQLiteSession,
set_tracing_disabled,
)
from agents.items import ModelResponse, TResponseInputItem, TResponseStreamEvent
from agents.models.interface import Model
from agents.usage import Usage
class CaptureModel(Model):
def __init__(self) -> None:
self.input: str | list[TResponseInputItem] | None = None
async def get_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
*args: Any,
**kwargs: Any,
) -> ModelResponse:
self.input = input
return ModelResponse(
output=[
ResponseOutputMessage(
id="msg_1",
type="message",
role="assistant",
status="completed",
content=[
ResponseOutputText(
type="output_text",
text="ok",
annotations=[],
logprobs=[],
)
],
)
],
usage=Usage(),
response_id="resp_1",
)
async def stream_response(
self,
*args: Any,
**kwargs: Any,
) -> AsyncIterator[TResponseStreamEvent]:
raise NotImplementedError
yield
async def main() -> None:
set_tracing_disabled(True)
session = SQLiteSession("limit-tool-pair-repro")
await session.add_items(
[
{
"role": "user",
"content": "What is the weather?",
},
{
"type": "function_call",
"call_id": "call_1",
"name": "get_weather",
"arguments": "{}",
},
{
"type": "function_call_output",
"call_id": "call_1",
"output": "sunny",
},
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "It is sunny.",
"annotations": [],
}
],
},
]
)
model = CaptureModel()
await Runner.run(
Agent(name="Example agent", model=model),
"What about tomorrow?",
session=session,
run_config=RunConfig(
session_settings=SessionSettings(limit=2),
),
)
print(json.dumps(model.input, indent=2))
session.close()
asyncio.run(main())
The first history item printed is:
{
"type": "function_call_output",
"call_id": "call_1",
"output": "sunny"
}
Its corresponding function_call is not present because it falls outside the two-item history window.
Expected behavior
Session.get_items(limit=2) should continue returning exactly the latest two stored items.
Before sending those items to the model, model-input sanitization should remove a retained tool output when its required call is not present in the retrieved history. It should not expand the history beyond the configured limit, change persisted session data, or remove caller-provided items from the current input.
For this reproduction, print(json.dumps(model.input, indent=2)) should produce:
[
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "It is sunny.",
"annotations": []
}
]
},
{
"content": "What about tomorrow?",
"role": "user"
}
]
The unmatched function_call_output should not be sent to the model.
The two patches
src/agents/run_internal/items.pyindex 9cacc505..7efa9af0 100644@@ -172,11 +172,16 @@ def drop_orphan_function_calls( items: list[TResponseInputItem], *, pruning_indexes: set[int] | None = None,+ drop_orphan_outputs: bool = False, ) -> list[TResponseInputItem]: """ Remove tool and program call items that do not have corresponding outputs so resumptions or- retries do not replay stale calls. Program-owned items are removed with an orphan program,- while programs with retained hosted calls or tool outputs remain available for continuation.+ retries do not replay stale calls. When ``drop_orphan_outputs`` is true, tool outputs whose+ call is absent from ``items`` are removed too, e.g. when a session history limit cuts the+ history between a call and its output. Leave it off for server-managed conversations, where+ the matching call may already live server-side. Program-owned items are removed with an+ orphan program, while programs with retained hosted calls or tool outputs remain available+ for continuation. Reasoning items that immediately precede a call dropped by this pass are also removed, since the Responses API rejects reasoning items that are not followed by their associated model-emitted item (``Item 'rs_...' of type 'reasoning' was provided without its required@@ -184,6 +189,7 @@ def drop_orphan_function_calls( """ completed_call_ids = _completed_call_ids_by_type(items)+ present_call_ids = _present_call_ids_by_output_type(items) if drop_orphan_outputs else None matched_anonymous_tool_search_calls = _matched_anonymous_tool_search_call_indexes(items) active_program_call_ids: set[str] = set() orphan_program_call_ids: set[str] = set()@@ -208,6 +214,7 @@ def drop_orphan_function_calls( orphan_program_call_ids.add(call_id) dropped_indexes: set[int] = set()+ dropped_output_indexes: set[int] = set() filtered: list[TResponseInputItem] = [] for index, entry in enumerate(items): if not isinstance(entry, dict):@@ -226,6 +233,15 @@ def drop_orphan_function_calls( continue output_type = _TOOL_CALL_TO_OUTPUT_TYPE.get(entry_type) if output_type is None:+ if present_call_ids is not None and entry_type in present_call_ids:+ output_call_id = entry.get("call_id")+ if (+ isinstance(output_call_id, str)+ and output_call_id not in present_call_ids[entry_type]+ ):+ # Output whose call is missing; the Responses API rejects it.+ dropped_output_indexes.add(index)+ continue filtered.append(entry) continue call_id = entry.get("call_id")@@ -250,15 +266,22 @@ def drop_orphan_function_calls( if not dropped_indexes: return filtered- return _drop_reasoning_items_preceding_dropped_calls(items, dropped_indexes)+ return _drop_reasoning_items_preceding_dropped_calls(+ items, dropped_indexes, excluded_indexes=dropped_output_indexes+ ) def _drop_reasoning_items_preceding_dropped_calls( items: list[TResponseInputItem], dropped_indexes: set[int],+ *,+ excluded_indexes: set[int] | None = None, ) -> list[TResponseInputItem]: """Drop reasoning items whose tied tool call was just dropped as orphan. + ``excluded_indexes`` are additionally removed from the result without affecting which+ reasoning items are considered tied to a dropped call.+ A reasoning item is considered tied to the next non-reasoning model-emitted item. If that item was dropped, the reasoning item is now dangling and would be rejected by the Responses API with ``reasoning was provided without its required following item``.@@ -281,7 +304,7 @@ def _drop_reasoning_items_preceding_dropped_calls( if next_index in dropped_indexes: drop_reasoning.add(index) break- excluded = dropped_indexes | drop_reasoning+ excluded = dropped_indexes | drop_reasoning | (excluded_indexes or set()) return [entry for idx, entry in enumerate(items) if idx not in excluded] @@ -959,6 +982,26 @@ def _completed_call_ids_by_type(payload: list[TResponseInputItem]) -> dict[str, return completed +def _present_call_ids_by_output_type(payload: list[TResponseInputItem]) -> dict[str, set[str]]:+ """Return call ids of call items present in the payload, grouped by expected output type."""+ present: dict[str, set[str]] = {+ output_type: set() for output_type in _TOOL_CALL_TO_OUTPUT_TYPE.values()+ }+ for entry in payload:+ if not isinstance(entry, dict):+ continue+ item_type = entry.get("type")+ if not isinstance(item_type, str):+ continue+ output_type = _TOOL_CALL_TO_OUTPUT_TYPE.get(item_type)+ if output_type is None:+ continue+ call_id = entry.get("call_id")+ if isinstance(call_id, str):+ present[output_type].add(call_id)+ return present++ def _get_program_caller_id(entry: TResponseInputItem) -> str | None: """Return the owning program call id for a program-issued item.""" if not isinstance(entry, dict):src/agents/run_internal/session_persistence.pyindex df4c84a7..061b4fb2 100644@@ -355,6 +355,10 @@ async def prepare_input_with_session( ) else: history = await _session_get_items(session, wrapper=wrapper)+ # A limit window may have cut stored history between a tool call and its output. Backends can+ # return fewer than ``limit`` items even after cutting (e.g. skipping undecodable rows), so+ # do not infer truncation from the returned length.+ history_may_be_truncated = resolved_settings.limit is not None is_openai_conversation_session = isinstance(session, OpenAIConversationsSession) converted_history = [ strip_internal_input_item_metadata(ensure_input_item_format(item)) for item in history@@ -465,6 +469,7 @@ async def prepare_input_with_session( filtered = drop_orphan_function_calls( prepared_as_inputs, pruning_indexes=prune_history_indexes,+ drop_orphan_outputs=history_may_be_truncated, ) normalized = normalize_input_items_for_api(filtered) deduplicated = deduplicate_input_items_preferring_latest(normalized)tests/memory/test_session_limit.pyindex 5b908ee9..10aa6365 100644@@ -2,10 +2,12 @@ import tempfile from pathlib import Path+from typing import Any, cast import pytest from agents import Agent, RunConfig, SQLiteSession+from agents.items import TResponseInputItem from agents.memory import SessionSettings from tests.fake_model import FakeModel from tests.memory.test_session import run_agent_async@@ -174,3 +176,135 @@ async def test_session_limit_larger_than_history(runner_method): assert last_input[2].get("content") == "Message 2" session.close()+++def _tool_pair_history() -> list[TResponseInputItem]:+ items: list[dict[str, Any]] = [+ {"role": "user", "content": "What is the weather?"},+ {+ "type": "function_call",+ "call_id": "call_1",+ "name": "get_weather",+ "arguments": "{}",+ },+ {"type": "function_call_output", "call_id": "call_1", "output": "sunny"},+ {+ "type": "message",+ "role": "assistant",+ "content": [{"type": "output_text", "text": "It is sunny.", "annotations": []}],+ },+ ]+ return cast(list[TResponseInputItem], items)+++@pytest.mark.parametrize("runner_method", ["run", "run_sync", "run_streamed"])+@pytest.mark.asyncio+async def test_session_limit_drops_tool_output_whose_call_was_truncated(runner_method):+ """A limit boundary between a call and its output must not send the unmatched output."""+ with tempfile.TemporaryDirectory() as temp_dir:+ session = SQLiteSession("limit_tool_pair", Path(temp_dir) / "test.db")+ stored = _tool_pair_history()+ await session.add_items(stored)++ model = FakeModel()+ agent = Agent(name="test", model=model)+ model.set_next_output([get_text_message("ok")])+ await run_agent_async(+ runner_method,+ agent,+ "What about tomorrow?",+ session=session,+ run_config=RunConfig(session_settings=SessionSettings(limit=2)),+ )++ last_input = model.last_turn_args["input"]+ assert last_input == [stored[3], {"content": "What about tomorrow?", "role": "user"}]++ # Persisted history is untouched and the limit still returns exactly two items.+ all_items = await session.get_items()+ assert all_items[:4] == stored+ assert await session.get_items(limit=2) == all_items[-2:]++ session.close()+++@pytest.mark.parametrize("runner_method", ["run", "run_streamed"])+@pytest.mark.asyncio+async def test_session_limit_drops_truncated_tool_output_with_session_input_callback(+ runner_method,+):+ """History returned through a session_input_callback is sanitized the same way."""+ with tempfile.TemporaryDirectory() as temp_dir:+ session = SQLiteSession("limit_tool_pair_cb", Path(temp_dir) / "test.db")+ stored = _tool_pair_history()+ await session.add_items(stored)++ model = FakeModel()+ agent = Agent(name="test", model=model)+ model.set_next_output([get_text_message("ok")])+ await run_agent_async(+ runner_method,+ agent,+ "What about tomorrow?",+ session=session,+ run_config=RunConfig(+ session_settings=SessionSettings(limit=2),+ session_input_callback=lambda history, new: history + new,+ ),+ )++ last_input = model.last_turn_args["input"]+ assert last_input == [stored[3], {"content": "What about tomorrow?", "role": "user"}]++ session.close()+++@pytest.mark.asyncio+async def test_session_limit_keeps_tool_pair_when_both_sides_retained():+ """Outputs whose call is inside the limit window are still sent."""+ with tempfile.TemporaryDirectory() as temp_dir:+ session = SQLiteSession("limit_tool_pair_kept", Path(temp_dir) / "test.db")+ stored = _tool_pair_history()+ await session.add_items(stored)++ model = FakeModel()+ agent = Agent(name="test", model=model)+ model.set_next_output([get_text_message("ok")])+ await run_agent_async(+ "run",+ agent,+ "What about tomorrow?",+ session=session,+ run_config=RunConfig(session_settings=SessionSettings(limit=3)),+ )++ last_input = model.last_turn_args["input"]+ assert last_input[:3] == stored[1:]++ session.close()+++@pytest.mark.asyncio+async def test_session_limit_preserves_caller_provided_tool_output():+ """Caller-provided input items are never pruned, even if their call is not in history."""+ with tempfile.TemporaryDirectory() as temp_dir:+ session = SQLiteSession("limit_tool_pair_caller", Path(temp_dir) / "test.db")+ stored = _tool_pair_history()+ await session.add_items(stored)++ model = FakeModel()+ agent = Agent(name="test", model=model)+ model.set_next_output([get_text_message("ok")])+ caller_output = {"type": "function_call_output", "call_id": "call_2", "output": "rain"}+ await run_agent_async(+ "run",+ agent,+ [caller_output],+ session=session,+ run_config=RunConfig(session_settings=SessionSettings(limit=2)),+ )++ last_input = model.last_turn_args["input"]+ assert last_input == [stored[3], caller_output]++ session.close()tests/test_run_internal_items.pyindex 2b991ea3..7df14d7a 100644@@ -1168,3 +1168,68 @@ def test_deduplicate_input_items_preferring_latest_leaves_unique_items_untouched ] assert run_items.deduplicate_input_items_preferring_latest(items) == items+++def test_drop_orphan_function_calls_keeps_orphan_outputs_by_default() -> None:+ payload: list[TResponseInputItem] = [+ cast(TResponseInputItem, {"type": "function_call_output", "call_id": "c1", "output": "x"}),+ ]++ assert run_items.drop_orphan_function_calls(payload) == payload+++def test_drop_orphan_function_calls_drops_orphan_outputs_when_requested() -> None:+ payload = [+ {"type": "function_call_output", "call_id": "orphan", "output": "x"},+ {"type": "computer_call_output", "call_id": "orphan_cc", "output": {}},+ {"type": "custom_tool_call", "call_id": "c2", "name": "t", "input": ""},+ {"type": "custom_tool_call_output", "call_id": "c2", "output": "y"},+ # Same call_id but a different call type does not satisfy the output.+ {"type": "shell_call_output", "call_id": "c2", "output": []},+ {"type": "tool_search_call", "arguments": {}, "status": "completed"},+ {"type": "tool_search_output", "tools": [], "status": "completed"},+ {"type": "message", "role": "assistant", "content": "done"},+ ]++ filtered = run_items.drop_orphan_function_calls(+ cast(list[TResponseInputItem], payload),+ drop_orphan_outputs=True,+ )++ assert [cast(dict[str, Any], item)["type"] for item in filtered] == [+ "custom_tool_call",+ "custom_tool_call_output",+ "tool_search_call",+ "tool_search_output",+ "message",+ ]+++def test_drop_orphan_function_calls_only_drops_orphan_outputs_in_pruning_indexes() -> None:+ history_output = {"type": "function_call_output", "call_id": "old", "output": "x"}+ caller_output = {"type": "function_call_output", "call_id": "new", "output": "y"}+ payload = cast(list[TResponseInputItem], [history_output, caller_output])++ filtered = run_items.drop_orphan_function_calls(+ payload,+ pruning_indexes={0},+ drop_orphan_outputs=True,+ )++ assert filtered == [caller_output]+++def test_drop_orphan_function_calls_drops_orphan_output_and_orphan_call_together() -> None:+ payload = [+ {"type": "function_call_output", "call_id": "old", "output": "x"},+ {"type": "reasoning", "id": "rs_1", "summary": []},+ {"type": "function_call", "call_id": "pending", "name": "t", "arguments": "{}"},+ {"type": "message", "role": "assistant", "content": "done"},+ ]++ filtered = run_items.drop_orphan_function_calls(+ cast(list[TResponseInputItem], payload),+ drop_orphan_outputs=True,+ )++ assert filtered == [payload[3]]
src/agents/run_internal/items.pyindex 9cacc5050..976bdc866 100644@@ -172,15 +172,17 @@ def drop_orphan_function_calls( items: list[TResponseInputItem], *, pruning_indexes: set[int] | None = None,+ output_pruning_indexes: set[int] | None = None, ) -> list[TResponseInputItem]: """ Remove tool and program call items that do not have corresponding outputs so resumptions or- retries do not replay stale calls. Program-owned items are removed with an orphan program,- while programs with retained hosted calls or tool outputs remain available for continuation.- Reasoning items that immediately precede a call dropped by this pass are also removed, since- the Responses API rejects reasoning items that are not followed by their associated- model-emitted item (``Item 'rs_...' of type 'reasoning' was provided without its required- following item``).+ retries do not replay stale calls. When ``output_pruning_indexes`` identifies unambiguous+ stored history, also remove tool outputs whose corresponding calls are no longer present after+ history pruning. Program-owned items are removed with an orphan program, while programs with+ retained hosted calls or tool outputs remain available for continuation. Reasoning items that+ immediately precede a call dropped by this pass are also removed, since the Responses API+ rejects reasoning items that are not followed by their associated model-emitted item (``Item+ 'rs_...' of type 'reasoning' was provided without its required following item``). """ completed_call_ids = _completed_call_ids_by_type(items)@@ -208,54 +210,71 @@ def drop_orphan_function_calls( orphan_program_call_ids.add(call_id) dropped_indexes: set[int] = set()- filtered: list[TResponseInputItem] = []+ reasoning_trigger_indexes: set[int] = set() for index, entry in enumerate(items): if not isinstance(entry, dict):- filtered.append(entry) continue entry_type = entry.get("type") if not isinstance(entry_type, str):- filtered.append(entry) continue if pruning_indexes is not None and index not in pruning_indexes:- filtered.append(entry) continue program_caller_id = _get_program_caller_id(entry) if program_caller_id is not None and program_caller_id in orphan_program_call_ids: dropped_indexes.add(index)+ reasoning_trigger_indexes.add(index) continue+ call_id = entry.get("call_id") output_type = _TOOL_CALL_TO_OUTPUT_TYPE.get(entry_type) if output_type is None:- filtered.append(entry) continue- call_id = entry.get("call_id") if program_caller_id is not None and _is_pending_hosted_shell_call(entry):- filtered.append(entry) continue if entry_type == "program" and call_id in active_program_call_ids:- filtered.append(entry) continue if isinstance(call_id, str) and call_id in completed_call_ids.get(output_type, set()):- filtered.append(entry) continue if ( entry_type == "tool_search_call" and not isinstance(call_id, str) and index in matched_anonymous_tool_search_calls ):- filtered.append(entry) continue # Tool call entry will be dropped; record so we can also drop preceding reasoning items. dropped_indexes.add(index)+ reasoning_trigger_indexes.add(index)++ available_call_ids = _available_call_ids_by_output_type(+ items,+ excluded_indexes=dropped_indexes,+ )+ if output_pruning_indexes is not None:+ for index in output_pruning_indexes:+ if index in dropped_indexes or index < 0 or index >= len(items):+ continue+ entry = items[index]+ if not isinstance(entry, dict):+ continue+ entry_type = entry.get("type")+ if not isinstance(entry_type, str) or entry_type not in available_call_ids:+ continue+ call_id = entry.get("call_id")+ if isinstance(call_id, str) and call_id not in available_call_ids[entry_type]:+ dropped_indexes.add(index) if not dropped_indexes:- return filtered- return _drop_reasoning_items_preceding_dropped_calls(items, dropped_indexes)+ return list(items)+ return _drop_reasoning_items_preceding_dropped_calls(+ items,+ dropped_indexes,+ reasoning_trigger_indexes,+ ) def _drop_reasoning_items_preceding_dropped_calls( items: list[TResponseInputItem], dropped_indexes: set[int],+ reasoning_trigger_indexes: set[int], ) -> list[TResponseInputItem]: """Drop reasoning items whose tied tool call was just dropped as orphan. @@ -278,7 +297,7 @@ def _drop_reasoning_items_preceding_dropped_calls( next_entry = items[next_index] if isinstance(next_entry, dict) and next_entry.get("type") == "reasoning": continue- if next_index in dropped_indexes:+ if next_index in reasoning_trigger_indexes: drop_reasoning.add(index) break excluded = dropped_indexes | drop_reasoning@@ -959,6 +978,32 @@ def _completed_call_ids_by_type(payload: list[TResponseInputItem]) -> dict[str, return completed +def _available_call_ids_by_output_type(+ payload: list[TResponseInputItem],+ *,+ excluded_indexes: set[int],+) -> dict[str, set[str]]:+ """Return retained call ids grouped by their required output type."""+ available: dict[str, set[str]] = {+ output_type: set() for output_type in _TOOL_CALL_TO_OUTPUT_TYPE.values()+ }+ for index, entry in enumerate(payload):+ if index in excluded_indexes:+ continue+ if not isinstance(entry, dict):+ continue+ item_type = entry.get("type")+ if not isinstance(item_type, str):+ continue+ output_type = _TOOL_CALL_TO_OUTPUT_TYPE.get(item_type)+ if output_type is None:+ continue+ call_id = entry.get("call_id")+ if isinstance(call_id, str):+ available[output_type].add(call_id)+ return available++ def _get_program_caller_id(entry: TResponseInputItem) -> str | None: """Return the owning program call id for a program-issued item.""" if not isinstance(entry, dict):src/agents/run_internal/session_persistence.pyindex df4c84a7a..44c39d5ce 100644@@ -372,6 +372,7 @@ async def prepare_input_with_session( ] prune_history_indexes: set[int] = set()+ output_pruning_indexes: set[int] | None = None if session_input_callback is None or not include_history_in_prepared_input: prepared_items_raw: list[TResponseInputItem] = (@@ -382,6 +383,8 @@ async def prepare_input_with_session( appended_items = list(new_input_list) if include_history_in_prepared_input: prune_history_indexes = set(range(len(converted_history)))+ if session_input_callback is None and resolved_settings.limit is not None:+ output_pruning_indexes = set(prune_history_indexes) else: if not callable(session_input_callback): raise UserError(@@ -465,6 +468,7 @@ async def prepare_input_with_session( filtered = drop_orphan_function_calls( prepared_as_inputs, pruning_indexes=prune_history_indexes,+ output_pruning_indexes=output_pruning_indexes, ) normalized = normalize_input_items_for_api(filtered) deduplicated = deduplicate_input_items_preferring_latest(normalized)tests/memory/test_session_limit.pyindex 5b908ee96..3a2311d4b 100644@@ -2,10 +2,12 @@ import tempfile from pathlib import Path+from typing import cast import pytest from agents import Agent, RunConfig, SQLiteSession+from agents.items import TResponseInputItem from agents.memory import SessionSettings from tests.fake_model import FakeModel from tests.memory.test_session import run_agent_async@@ -62,6 +64,63 @@ async def test_session_limit_parameter(runner_method): session.close() +@pytest.mark.parametrize("runner_method", ["run", "run_sync", "run_streamed"])+@pytest.mark.asyncio+async def test_session_limit_drops_unmatched_history_function_call_output(runner_method):+ """A limit boundary must not pass an output whose matching call was excluded."""+ with tempfile.TemporaryDirectory() as temp_dir:+ session = SQLiteSession("limit_tool_pair", Path(temp_dir) / "test_limit_tool_pair.db")+ history = cast(+ list[TResponseInputItem],+ [+ {"role": "user", "content": "What is the weather?"},+ {+ "type": "function_call",+ "call_id": "call_1",+ "name": "get_weather",+ "arguments": "{}",+ },+ {+ "type": "function_call_output",+ "call_id": "call_1",+ "output": "sunny",+ },+ {+ "type": "message",+ "role": "assistant",+ "content": [+ {+ "type": "output_text",+ "text": "It is sunny.",+ "annotations": [],+ }+ ],+ },+ ],+ )+ await session.add_items(history)++ assert await session.get_items(limit=2) == history[-2:]++ model = FakeModel()+ model.set_next_output([get_text_message("Tomorrow is sunny too.")])+ agent = Agent(name="test", model=model)++ await run_agent_async(+ runner_method,+ agent,+ "What about tomorrow?",+ session=session,+ run_config=RunConfig(session_settings=SessionSettings(limit=2)),+ )++ assert model.last_turn_args["input"] == [+ history[-1],+ {"role": "user", "content": "What about tomorrow?"},+ ]+ session.close()++ @pytest.mark.parametrize("runner_method", ["run", "run_sync", "run_streamed"]) @pytest.mark.asyncio async def test_session_limit_zero(runner_method):tests/test_agent_runner.pyindex f654aead6..1cce41035 100644@@ -67,6 +67,7 @@ from agents.items import ( TResponseInputItem, ) from agents.lifecycle import RunHooks+from agents.memory import SessionSettings from agents.models.fake_id import FAKE_RESPONSES_ID from agents.run import AgentRunner, get_default_agent_runner, set_default_agent_runner from agents.run_config import _default_trace_include_sensitive_data@@ -2516,7 +2517,7 @@ async def test_input_guardrail_tripwire_does_not_save_assistant_message_to_sessi @pytest.mark.asyncio-async def test_prepare_input_with_session_keeps_function_call_outputs():+async def test_prepare_input_with_session_keeps_orphan_output_without_limit(): history_item = cast( TResponseInputItem, {@@ -2529,14 +2530,160 @@ async def test_prepare_input_with_session_keeps_function_call_outputs(): prepared_input, session_items = await prepare_input_with_session("hello", session, None) - assert isinstance(prepared_input, list)- assert len(session_items) == 1- assert cast(dict[str, Any], session_items[0]).get("role") == "user"- first_item = cast(dict[str, Any], prepared_input[0])- last_item = cast(dict[str, Any], prepared_input[-1])- assert first_item["type"] == "function_call_output"- assert last_item["role"] == "user"- assert last_item["content"] == "hello"+ assert prepared_input == [history_item, {"role": "user", "content": "hello"}]+ assert session_items == [{"role": "user", "content": "hello"}]+++@pytest.mark.asyncio+async def test_prepare_input_with_session_drops_limited_orphan_history_function_call_outputs():+ history_item = cast(+ TResponseInputItem,+ {+ "type": "function_call_output",+ "call_id": "call_prepare",+ "output": "ok",+ },+ )+ session = SimpleListSession(history=[history_item])++ prepared_input, session_items = await prepare_input_with_session(+ "hello",+ session,+ None,+ SessionSettings(limit=1),+ )++ assert prepared_input == [{"role": "user", "content": "hello"}]+ assert session_items == [{"role": "user", "content": "hello"}]+++@pytest.mark.asyncio+async def test_prepare_input_with_session_preserves_new_function_call_outputs():+ new_output = cast(+ TResponseInputItem,+ {+ "type": "function_call_output",+ "call_id": "call_prepare",+ "output": "ok",+ },+ )+ session = SimpleListSession()++ prepared_input, session_items = await prepare_input_with_session(+ [new_output],+ session,+ None,+ SessionSettings(limit=1),+ )++ assert prepared_input == [new_output]+ assert session_items == [new_output]+++@pytest.mark.asyncio+async def test_prepare_input_with_session_leaves_custom_callback_output_unchanged():+ history_output = cast(+ TResponseInputItem,+ {+ "type": "function_call_output",+ "call_id": "call_callback",+ "output": "ok",+ },+ )+ session = SimpleListSession(history=[history_output])++ def callback(+ history: list[TResponseInputItem], new_input: list[TResponseInputItem]+ ) -> list[TResponseInputItem]:+ return history + new_input++ prepared_input, session_items = await prepare_input_with_session(+ "hello",+ session,+ callback,+ SessionSettings(limit=1),+ )++ assert prepared_input == [history_output, {"role": "user", "content": "hello"}]+ assert session_items == [{"role": "user", "content": "hello"}]+++@pytest.mark.asyncio+async def test_prepare_input_with_session_drops_output_for_program_owned_call_pruned_with_parent():+ program = cast(+ TResponseInputItem,+ {+ "type": "program",+ "call_id": "program_orphan",+ "code": "return await tools.lookup({});",+ "fingerprint": "fingerprint:orphan",+ },+ )+ function_call = cast(+ TResponseInputItem,+ {+ "type": "function_call",+ "call_id": "call_orphan",+ "name": "lookup",+ "arguments": "{}",+ "caller": {"type": "program", "caller_id": "program_orphan"},+ },+ )+ function_output = cast(+ TResponseInputItem,+ {+ "type": "function_call_output",+ "call_id": "call_orphan",+ "output": "ok",+ },+ )+ session = SimpleListSession(history=[program, function_call, function_output])++ prepared_input, session_items = await prepare_input_with_session(+ "hello",+ session,+ None,+ SessionSettings(limit=3),+ )++ assert prepared_input == [{"role": "user", "content": "hello"}]+ assert session_items == [{"role": "user", "content": "hello"}]+++@pytest.mark.asyncio+async def test_prepare_input_with_session_keeps_paired_history_function_call_outputs():+ function_call = cast(+ TResponseInputItem,+ {+ "type": "function_call",+ "call_id": "call_prepare",+ "name": "lookup",+ "arguments": "{}",+ },+ )+ function_call_output = cast(+ TResponseInputItem,+ {+ "type": "function_call_output",+ "call_id": "call_prepare",+ "output": "ok",+ },+ )+ session = SimpleListSession(history=[function_call, function_call_output])++ prepared_input, session_items = await prepare_input_with_session(+ "hello",+ session,+ None,+ SessionSettings(limit=2),+ )++ assert prepared_input == [+ function_call,+ function_call_output,+ {"role": "user", "content": "hello"},+ ]+ assert session_items == [{"role": "user", "content": "hello"}] @pytest.mark.asyncio