Skip to content

Commit 6ed0bee

Browse files
author
xianghuijin
committed
fix: prevent modifying the original agent's model_settings
This fixes the issue where the original agent's model_settings was being directly modified during the tool choice reset process. The original implementation caused the agent's tool_choice to unintentionally reset to "auto" for subsequent runs, which could be unexpected behavior. The fix creates new copies of the agent and model settings objects using dataclasses.replace() instead of modifying the original objects. This ensures that the tool choice reset is limited to the current run only, maintaining the expected behavior for sequential runs with the same agent. Addresses feedback from @baderalfahad about the agent instance being modified when it should maintain its original state between runs.
1 parent 8f2f76c commit 6ed0bee

File tree

2 files changed

+148
-267
lines changed

2 files changed

+148
-267
lines changed

src/agents/_run_impl.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,25 @@ async def execute_tools_and_side_effects(
213213
tools = agent.tools
214214
# Only reset in the problematic scenarios where loops are likely unintentional
215215
if cls._should_reset_tool_choice(agent.model_settings, tools):
216-
agent.model_settings = dataclasses.replace(
216+
# Create a modified copy instead of modifying the original agent
217+
new_model_settings = dataclasses.replace(
217218
agent.model_settings,
218219
tool_choice="auto"
219220
)
221+
# Create a new internal agent with updated settings
222+
agent = dataclasses.replace(agent, model_settings=new_model_settings)
220223

221224
if (
222225
run_config.model_settings and
223226
cls._should_reset_tool_choice(run_config.model_settings, tools)
224227
):
225-
run_config.model_settings = dataclasses.replace(
228+
# Also update the run_config model settings with a copy
229+
new_run_config_settings = dataclasses.replace(
226230
run_config.model_settings,
227231
tool_choice="auto"
228232
)
233+
# Create a new run_config with the new settings
234+
run_config = dataclasses.replace(run_config, model_settings=new_run_config_settings)
229235

230236
# Second, check if there are any handoffs
231237
if run_handoffs := processed_response.handoffs:

0 commit comments

Comments
 (0)