Skip to content

feat: expose conversation history to tools via ToolContext.conversation_history#2905

Closed
Nik-Reddy wants to merge 1 commit intoopenai:mainfrom
Nik-Reddy:feat/conversation-history-in-tool-context
Closed

feat: expose conversation history to tools via ToolContext.conversation_history#2905
Nik-Reddy wants to merge 1 commit intoopenai:mainfrom
Nik-Reddy:feat/conversation-history-in-tool-context

Conversation

@Nik-Reddy
Copy link
Copy Markdown

@Nik-Reddy Nik-Reddy commented Apr 16, 2026

Fixes #904

I ran into this while building an agent that needed tools to look at what happened earlier in the conversation, for example, summarizing prior tool outputs or deduplicating work. Right now there's no clean way to do that from inside a tool function.

This adds a conversation_history property to ToolContext that gives tools read-only access to the items generated so far in the current run. It's a copy, so tools can't accidentally mutate run state.

Changes:

  • RunContextWrapper gets an internal _generated_items list (populated in turn_resolution.py right before tool execution)
  • ToolContext exposes it as a read-only conversation_history property
  • Added ToolContext to the package's public API (__init__.py / __all__)
  • 4 new tests covering: empty default, population from wrapper, copy semantics, constructor path

Kept it minimal, didn't touch streaming or any other context classes. Happy to adjust the approach if maintainers prefer a different pattern.

…on_history

Add a conversation_history property to ToolContext that gives tools
read-only access to the items generated so far in the current agent run.
This enables tools to inspect prior messages, tool calls, and other
conversation items during execution without requiring streaming mode.

Changes:
- Add _generated_items field to RunContextWrapper (internal, not
  part of the public constructor contract)
- Add conversation_history read-only property to ToolContext that
  returns a defensive copy of the generated items
- Populate _generated_items on the context wrapper in
  get_single_step_result_from_response before tool execution, which
  covers both streaming and non-streaming paths
- Export ToolContext from the top-level �gents package
- Add 4 tests covering default empty state, population from wrapper,
  copy semantics, and direct constructor usage

Fixes openai#904
@github-actions github-actions bot added enhancement New feature or request feature:core labels Apr 16, 2026
Copy link
Copy Markdown

@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: f1a65c2647

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


# Expose the conversation history so tools can inspect prior items via
# ToolContext.conversation_history (addresses #904).
context_wrapper._generated_items = list(pre_step_items)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Set conversation history for resumed interruption turns

This history assignment only runs in get_single_step_result_from_response, but resumed approval flows execute tools through resolve_interrupted_turn and can use a rebuilt/overridden RunContextWrapper (for example after deserializing RunState) where _generated_items is empty. In that path, ToolContext.from_agent_context copies stale history, so tools resumed after an interruption see missing prior conversation items; initialize _generated_items in the resume path before tool execution as well.

Useful? React with 👍 / 👎.

allowing tools to inspect prior messages, tool calls, and other items produced
during the run. Returns a copy so that mutations do not affect the run state.
"""
return list(self._generated_items)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prevent item-level mutation through conversation_history

Returning list(self._generated_items) only copies the outer list, so each RunItem object is still shared with run state; tool code can mutate conversation_history entries (for example raw_item dict/model fields) and unintentionally alter subsequent model input/persistence. That contradicts the read-only guarantee in this property docstring and can corrupt run behavior; expose immutable/deep-copied entries instead.

Useful? React with 👍 / 👎.

@seratch
Copy link
Copy Markdown
Member

seratch commented Apr 16, 2026

Hi @Nik-Reddy, thanks for taking the time to work on this. We appreciate the interest here, but this PR is not aligned with the direction we want to take for the issue.

The original request in #904 was not intended to broaden tool access to run history, and we would like to avoid exposing this kind of data through tools. We also do not want to carry this data on RunContextWrapper, even as a private or internal field.

The main reason #904 has remained open for a while is that it requires careful interface design to preserve the SDK's current philosophy and boundaries. Because of that, we do not think this approach is the right one for us to pursue.

To avoid spending more time on a direction we are unlikely to merge, we are going to close this PR for now. We may revisit #904 and resolve it in a different way in the future, but this is not the path we want to take. Thanks again for the contribution and for taking the time to explore this.

@seratch seratch closed this Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants