Skip to content

Conversation

@AnkanMisra
Copy link
Contributor

Fixes #2171

Problem

When nest_handoff_history is enabled, the next agent receives duplicate conversation items:

Before this fix:

  1. Summary message with <CONVERSATION HISTORY> containing tool calls as text
  2. Raw function_call items (duplicated)
  3. Raw function_call_output items (duplicated)

This causes the model to see the same information twice, wasting tokens and potentially causing confusion.

Solution

Introduced separation between items sent to the model vs items preserved for session history:

  • Model input: Filtered - excludes function_call and function_call_output items already in the summary
  • Session history: Unfiltered - preserves all items for observability and session saving

Key changes:

  • Added input_items field to HandoffInputData for filtered model input
  • Added session_step_items field to SingleStepResult for full session history
  • Updated generated_items property to use session items when available
  • Streaming events now use unfiltered items for full observability

Behavior

Scenario Before After
Model receives Summary + raw tool items (duplicated) Summary only (no duplicates)
Session saves All items All items (unchanged)
Streaming events All items All items (unchanged)

Testing

  • Added 6 dedicated tests covering the fix
  • Updated existing test assertions to reflect new expected counts
  • All 1111 tests pass

AnkanMisra and others added 2 commits January 3, 2026 00:38
…ation-2171

fix(handoffs): filter duplicate items from model input when nest_handoff_history is enabled (openai#2171)
@AnkanMisra
Copy link
Contributor Author

@seratch Please review for any fixes

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

guardrail_result.output.tripwire_triggered
for guardrail_result in input_guardrail_results
):
await self._save_result_to_session(
session, [], turn_result.new_step_items

P1 Badge Session save drops unfiltered handoff items

For handoffs, new_step_items now holds the filtered model input while session_step_items keeps the full turn, but _save_result_to_session is still called with turn_result.new_step_items. With nest_handoff_history enabled this means tool call/output items removed from input_items are never persisted to the session even though they’re retained in session_step_items, contradicting the stated goal that session history remains unfiltered.

ℹ️ 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".

Comment on lines +266 to +269
items = (
self.session_step_items if self.session_step_items is not None else self.new_step_items
)
return self.pre_step_items + items

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep filtered items in next turn model input

When nest_handoff_history/input filters supply input_items, execute_handoffs sets new_step_items to the filtered items but generated_items now prefers session_step_items (unfiltered). The next agent’s prompt is built by appending turn_result.generated_items to the summarized original_input (see run.py lines 1621-1624), so raw tool call and handoff output items are still sent to the model alongside the summary, leaving the duplication this change aims to remove.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Next agent receives duplicate history (summary + raw tool calls) when nest_handoff_history is enabled

1 participant