Skip to content

feat: add conversation history to ToolContext#2745

Draft
HuxleyHu98 wants to merge 1 commit intoopenai:mainfrom
HuxleyHu98:feature/toolcontext-conversation-history-904
Draft

feat: add conversation history to ToolContext#2745
HuxleyHu98 wants to merge 1 commit intoopenai:mainfrom
HuxleyHu98:feature/toolcontext-conversation-history-904

Conversation

@HuxleyHu98
Copy link

@HuxleyHu98 HuxleyHu98 commented Mar 21, 2026

Summary

This change adds conversation_history to ToolContext so local function tools can access the visible conversation history at invocation time in non-streaming runs.

Previously, tools could access the current tool call metadata and turn_input, but not the accumulated visible history that led up to the tool invocation. This made some tool patterns awkward in non-streaming mode, especially tools that need to inspect parent-agent context or delegate to another model.

This PR keeps the scope intentionally narrow:

  • adds conversation_history: list[TResponseInputItem] to ToolContext
  • populates it for local function tools during the current non-streaming execution path
  • builds it from the current input plus prior visible run items that can be represented as model input
  • skips internal bookkeeping items (for example approval placeholders) that are not valid input items
  • documents the new ToolContext.conversation_history field in docs/context.md

The implementation threads the history snapshot through the current runtime pipeline rather than reviving the old _run_impl.py path.

Test plan

Ran focused verification for the changed runtime path and related ToolContext behavior:

  • uv run ruff check src/agents/tool_context.py src/agents/run_internal/tool_execution.py src/agents/run_internal/tool_planning.py src/agents/run_internal/turn_resolution.py tests/test_tool_context.py tests/test_agent_runner.py
  • uv run mypy src/agents/tool_context.py src/agents/run_internal/tool_execution.py src/agents/run_internal/tool_planning.py src/agents/run_internal/turn_resolution.py tests/test_tool_context.py tests/test_agent_runner.py
  • uv run pytest -q tests/test_tool_context.py tests/test_agent_runner.py tests/test_agent_as_tool.py

Also verified:

  • single-turn tool invocation exposes conversation_history
  • multi-turn/session history is visible in tool context
  • resume / approval / handoff related paths continue to pass in the exercised test suite

Issue number

Closes #904

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

@github-actions github-actions bot added enhancement New feature or request feature:core labels Mar 21, 2026
@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from bd2a003 to 5c7d961 Compare March 21, 2026 05:12
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Mar 21, 2026
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

Here are some automated review suggestions for this pull request.

Reviewed commit: bd2a003792

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

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from 5c7d961 to f23f1ec Compare March 21, 2026 05:17
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

Here are some automated review suggestions for this pull request.

Reviewed commit: f23f1ec8c8

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

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from f23f1ec to 6a81776 Compare March 21, 2026 05:26
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

Here are some automated review suggestions for this pull request.

Reviewed commit: 6a817768a1

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

This intentionally includes only items that can be represented as model input.
Internal bookkeeping items such as approval placeholders are skipped.
"""
history = ItemHelpers.input_to_new_input_list(original_input)

Choose a reason for hiding this comment

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

P2 Badge Build tool history from filtered turn input

The history snapshot is seeded from original_input, but model calls may first transform input via call_model_input_filter in get_new_response. That means ToolContext.conversation_history can include unfiltered/redacted-away items that the model never saw, so the new field no longer reliably represents the visible context that led to the tool call and can unintentionally surface filtered content when tools reuse the snapshot.

Useful? React with 👍 / 👎.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed. The history snapshot now seeds from the actual filtered turn input sent to the model rather than the raw original_input. get_new_response() stores the deduplicated post-call_model_input_filter input on the run context, and turn_resolution builds conversation_history from that snapshot.

I also added a regression test that applies call_model_input_filter and verifies the tool-visible history contains the redacted/filtered input rather than the original unfiltered value.

Separately, I pulled in and formatted the newly-added .agents/skills/runtime-behavior-probe/templates/python_probe.py from main so make lint is green again on the current merge base.

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch 2 times, most recently from 05d5ee9 to 871cd79 Compare March 21, 2026 05:40
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

Here are some automated review suggestions for this pull request.

Reviewed commit: 871cd7929d

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

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from 871cd79 to afdf550 Compare March 21, 2026 05:49
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

Here are some automated review suggestions for this pull request.

Reviewed commit: afdf5509e5

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

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from afdf550 to 5adf0e5 Compare March 21, 2026 06:02
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

Here are some automated review suggestions for this pull request.

Reviewed commit: 5adf0e594f

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

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch 2 times, most recently from 251ec48 to adb7877 Compare March 21, 2026 06:17
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

Here are some automated review suggestions for this pull request.

Reviewed commit: adb78779cf

ℹ️ 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 +1130 to +1133
filtered_model_input = await maybe_filter_model_input(
agent=agent,
run_config=run_config,
context_wrapper=context_wrapper,

Choose a reason for hiding this comment

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

P1 Badge Preserve original input-filtered snapshot on resume

Resume rebuilding now re-runs maybe_filter_model_input using the current run_config, not the filter output captured when the interrupted model call originally ran. If a run started with call_model_input_filter and later resumes via Runner.run(agent, state) without re-supplying the same filter, conversation_history for approved function tools is reconstructed from unfiltered/changed input, which can reintroduce redacted content and diverge from replay-safe behavior.

Useful? React with 👍 / 👎.

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from adb7877 to 7c1d78d Compare March 21, 2026 06:26
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

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c1d78d2f1

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

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch 3 times, most recently from 7e86e99 to 8c353f9 Compare March 21, 2026 07:07
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

Here are some automated review suggestions for this pull request.

Reviewed commit: 8c353f91bb

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

"""

turn_input: list[TResponseInputItem] = field(default_factory=list)
_tool_history_input: list[TResponseInputItem] = field(default_factory=list, repr=False)

Choose a reason for hiding this comment

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

P2 Badge Preserve RunContextWrapper positional constructor order

Adding _tool_history_input between turn_input and _approvals changes the dataclass-generated RunContextWrapper.__init__ positional binding, so existing calls like RunContextWrapper(ctx, usage, turn_input, approvals) now pass approvals into history and leave _approvals at its default. This is a source-compatibility regression for exported API consumers and can silently break approval behavior on resumed/tool-approval flows; the new field should be appended in a compatibility-safe way (for example init=False or a custom constructor preserving prior positional slots).

Useful? React with 👍 / 👎.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed. _tool_history_input is now a private, init=False field, so RunContextWrapper keeps its existing positional constructor order. I also added a source-compat regression that exercises the legacy positional constructor shape (RunContextWrapper(ctx, usage, turn_input, approvals)) and verifies approvals still bind to _approvals rather than the new history storage.

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from 8c353f9 to f64bc96 Compare March 21, 2026 07:31
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

Here are some automated review suggestions for this pull request.

Reviewed commit: f64bc9639e

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

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch 2 times, most recently from eb1f152 to 91fb87f Compare March 21, 2026 08:25
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

Here are some automated review suggestions for this pull request.

Reviewed commit: 91fb87f8a0

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

@discopops
Copy link

Prepared a minimal formatting-only fix for the failing run (23375138860) on head SHA .

Patch commit: discopops@aa9531f

This commit only formats:

Validation run on that exact SHA context:

  • (pass)

If helpful, you can cherry-pick:

@discopops
Copy link

Prepared a minimal formatting-only fix for failing Tests run 23375138860 on head SHA eb1f152.

Patch commit:
discopops@aa9531f

This commit only formats:

  • tests/test_agent_runner.py
  • tests/test_run_state.py

Validation on that exact SHA context:

  • uv run ruff format --check tests/test_agent_runner.py tests/test_run_state.py (pass)

Cherry-pick command:

git cherry-pick aa9531f

@HuxleyHu98 HuxleyHu98 force-pushed the feature/toolcontext-conversation-history-904 branch from 91fb87f to 7302f1f Compare March 21, 2026 11:54
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

Here are some automated review suggestions for this pull request.

Reviewed commit: 7302f1f01c

ℹ️ 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 +573 to +576
preserve_tool_history = (
conversation_id is not None
and context_wrapper._tool_history_conversation_id == conversation_id
)

Choose a reason for hiding this comment

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

P2 Badge Preserve history for previous_response_id server runs

This reset condition preserves _tool_history_input only when an explicit conversation_id matches, so reused RunContextWrapper instances in previous_response_id/auto_previous_response_id flows always get cleared at the start of each new Runner.run. In run_internal/run_loop.py, _tool_history_conversation_id is populated from server_conversation_tracker.conversation_id, which is None in those flows, so follow-up tool calls receive only the latest delta while the model still has full server-managed history; that makes ToolContext.conversation_history diverge from the context that actually produced the tool call.

Useful? React with 👍 / 👎.

Comment on lines +161 to +165
def _build_function_tool_conversation_history(
turn_input: Sequence[TResponseInputItem],
) -> list[TResponseInputItem]:
"""Build the visible history snapshot for a local function tool invocation."""
return list(turn_input)

Choose a reason for hiding this comment

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

P2 Badge Deep-copy conversation history before passing to tools

This helper builds conversation_history with only a shallow list copy, so each history item object is still shared with context_wrapper._tool_history_input. If a tool mutates a history item (for example editing content or IDs), it mutates the runner’s internal snapshot that is later reused for interruption state and server-history accumulation, which can corrupt subsequent model input and resume behavior even though the field is documented as a snapshot.

Useful? React with 👍 / 👎.

@seratch
Copy link
Member

seratch commented Mar 21, 2026

Hi, thanks for sharing this idea. I just wanted to let you know that, since this PR brings lots of changes to run internals and these could conflict with our ongoing priorities, we won't consider this in the short term even if it's good to go.

@seratch seratch marked this pull request as draft March 21, 2026 12:06
@HuxleyHu98
Copy link
Author

HuxleyHu98 commented Mar 21, 2026

Thanks for the clarification — that makes sense.
I understand this touches quite a bit of run internals and may not fit the current short-term priorities.
I’m happy to leave this here for future reference, and I can revisit it later if the timing becomes better or if a much smaller scoped variant would be more useful.

@discopops
Copy link

[P2][Owner:openai-agents-python maintainers] Escalation for CI-OPENAI-AGENTS-001: this finding has been open since 2026-03-11 (>72h). A validated format fix is already published on \ (commit ). Please cherry-pick/apply and re-run the Tests workflow to close the carry-forward blocker.

@discopops
Copy link

[P2][Owner:openai-agents-python maintainers] Escalation for CI-OPENAI-AGENTS-001: this finding has been open since 2026-03-11 (>72h). A validated format fix is published on branch discopops:ci/fix-format-eb1f152c (commit aa9531f). Please cherry-pick/apply and re-run the Tests workflow to close the carry-forward blocker.

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

Labels

documentation Improvements or additions to documentation enhancement New feature or request feature:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When running in non-streaming mode, retrieve past history during tool execution.

3 participants