feat: expose conversation history to tools via ToolContext.conversation_history#2905
feat: expose conversation history to tools via ToolContext.conversation_history#2905Nik-Reddy wants to merge 1 commit intoopenai:mainfrom
Conversation
…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
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 👍 / 👎.
|
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 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. |
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_historyproperty toToolContextthat 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:
RunContextWrappergets an internal_generated_itemslist (populated inturn_resolution.pyright before tool execution)ToolContextexposes it as a read-onlyconversation_historypropertyToolContextto the package's public API (__init__.py/__all__)Kept it minimal, didn't touch streaming or any other context classes. Happy to adjust the approach if maintainers prefer a different pattern.