4242from ..agent import Agent , ToolsToFinalOutputResult
4343from ..agent_output import AgentOutputSchemaBase
4444from ..agent_tool_state import get_agent_tool_state_scope , peek_agent_tool_run_result
45- from ..exceptions import ModelBehaviorError , UserError
45+ from ..exceptions import AgentsException , ModelBehaviorError , UserError
4646from ..handoffs import Handoff , HandoffInputData , nest_handoff_history
4747from ..items import (
4848 CompactionItem ,
153153]
154154
155155
156+ def _build_function_tool_conversation_history (
157+ original_input : str | list [TResponseInputItem ],
158+ pre_step_items : Sequence [RunItem ],
159+ ) -> list [TResponseInputItem ]:
160+ """Build the visible history snapshot for a local function tool invocation.
161+
162+ This intentionally includes only items that can be represented as model input.
163+ Internal bookkeeping items such as approval placeholders are skipped.
164+ """
165+ history = ItemHelpers .input_to_new_input_list (original_input )
166+ for item in pre_step_items :
167+ try :
168+ history .append (item .to_input_item ())
169+ except AgentsException :
170+ continue
171+ return history
172+
173+
156174async def _maybe_finalize_from_tool_results (
157175 * ,
158176 agent : Agent [TContext ],
@@ -528,6 +546,11 @@ async def execute_tools_and_side_effects(
528546 new_items = processed_response .new_items ,
529547 )
530548
549+ conversation_history = _build_function_tool_conversation_history (
550+ original_input ,
551+ pre_step_items ,
552+ )
553+
531554 (
532555 function_results ,
533556 tool_input_guardrail_results ,
@@ -542,6 +565,7 @@ async def execute_tools_and_side_effects(
542565 hooks = hooks ,
543566 context_wrapper = context_wrapper ,
544567 run_config = run_config ,
568+ conversation_history = conversation_history ,
545569 )
546570 new_step_items .extend (
547571 _build_tool_result_items (
@@ -1103,6 +1127,11 @@ def _add_unmatched_pending(approval: ToolApprovalItem) -> None:
11031127 apply_patch_calls = approved_apply_patch_calls ,
11041128 )
11051129
1130+ conversation_history = _build_function_tool_conversation_history (
1131+ original_input ,
1132+ original_pre_step_items ,
1133+ )
1134+
11061135 (
11071136 function_results ,
11081137 tool_input_guardrail_results ,
@@ -1117,6 +1146,7 @@ def _add_unmatched_pending(approval: ToolApprovalItem) -> None:
11171146 hooks = hooks ,
11181147 context_wrapper = context_wrapper ,
11191148 run_config = run_config ,
1149+ conversation_history = conversation_history ,
11201150 )
11211151
11221152 for interruption in _collect_tool_interruptions (
0 commit comments