|
| 1 | +import pytest |
| 2 | + |
| 3 | +from agents import Agent, ModelSettings, Runner, Tool |
| 4 | +from agents._run_impl import RunImpl |
| 5 | + |
| 6 | +from .fake_model import FakeModel |
| 7 | +from .test_responses import ( |
| 8 | + get_function_tool, |
| 9 | + get_function_tool_call, |
| 10 | + get_text_message, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class TestToolChoiceReset: |
| 15 | + |
| 16 | + def test_should_reset_tool_choice_direct(self): |
| 17 | + """ |
| 18 | + Test the _should_reset_tool_choice method directly with various inputs |
| 19 | + to ensure it correctly identifies cases where reset is needed. |
| 20 | + """ |
| 21 | + # Case 1: tool_choice = None should not reset |
| 22 | + model_settings = ModelSettings(tool_choice=None) |
| 23 | + tools1: list[Tool] = [get_function_tool("tool1")] |
| 24 | + # Cast to list[Tool] to fix type checking issues |
| 25 | + assert not RunImpl._should_reset_tool_choice(model_settings, tools1) |
| 26 | + |
| 27 | + # Case 2: tool_choice = "auto" should not reset |
| 28 | + model_settings = ModelSettings(tool_choice="auto") |
| 29 | + assert not RunImpl._should_reset_tool_choice(model_settings, tools1) |
| 30 | + |
| 31 | + # Case 3: tool_choice = "none" should not reset |
| 32 | + model_settings = ModelSettings(tool_choice="none") |
| 33 | + assert not RunImpl._should_reset_tool_choice(model_settings, tools1) |
| 34 | + |
| 35 | + # Case 4: tool_choice = "required" with one tool should reset |
| 36 | + model_settings = ModelSettings(tool_choice="required") |
| 37 | + assert RunImpl._should_reset_tool_choice(model_settings, tools1) |
| 38 | + |
| 39 | + # Case 5: tool_choice = "required" with multiple tools should not reset |
| 40 | + model_settings = ModelSettings(tool_choice="required") |
| 41 | + tools2: list[Tool] = [get_function_tool("tool1"), get_function_tool("tool2")] |
| 42 | + assert not RunImpl._should_reset_tool_choice(model_settings, tools2) |
| 43 | + |
| 44 | + # Case 6: Specific tool choice should reset |
| 45 | + model_settings = ModelSettings(tool_choice="specific_tool") |
| 46 | + assert RunImpl._should_reset_tool_choice(model_settings, tools1) |
| 47 | + |
| 48 | + @pytest.mark.asyncio |
| 49 | + async def test_required_tool_choice_with_multiple_runs(self): |
| 50 | + """ |
| 51 | + Test scenario 1: When multiple runs are executed with tool_choice="required" |
| 52 | + Ensure each run works correctly and doesn't get stuck in infinite loop |
| 53 | + Also verify that tool_choice remains "required" between runs |
| 54 | + """ |
| 55 | + # Set up our fake model with responses for two runs |
| 56 | + fake_model = FakeModel() |
| 57 | + fake_model.add_multiple_turn_outputs([ |
| 58 | + [get_text_message("First run response")], |
| 59 | + [get_text_message("Second run response")] |
| 60 | + ]) |
| 61 | + |
| 62 | + # Create agent with a custom tool and tool_choice="required" |
| 63 | + custom_tool = get_function_tool("custom_tool") |
| 64 | + agent = Agent( |
| 65 | + name="test_agent", |
| 66 | + model=fake_model, |
| 67 | + tools=[custom_tool], |
| 68 | + model_settings=ModelSettings(tool_choice="required"), |
| 69 | + ) |
| 70 | + |
| 71 | + # First run should work correctly and preserve tool_choice |
| 72 | + result1 = await Runner.run(agent, "first run") |
| 73 | + assert result1.final_output == "First run response" |
| 74 | + assert agent.model_settings.tool_choice == "required", "tool_choice should stay required" |
| 75 | + |
| 76 | + # Second run should also work correctly with tool_choice still required |
| 77 | + result2 = await Runner.run(agent, "second run") |
| 78 | + assert result2.final_output == "Second run response" |
| 79 | + assert agent.model_settings.tool_choice == "required", "tool_choice should stay required" |
| 80 | + |
| 81 | + @pytest.mark.asyncio |
| 82 | + async def test_required_with_stop_at_tool_name(self): |
| 83 | + """ |
| 84 | + Test scenario 2: When using required tool_choice with stop_at_tool_names behavior |
| 85 | + Ensure it correctly stops at the specified tool |
| 86 | + """ |
| 87 | + # Set up fake model to return a tool call for second_tool |
| 88 | + fake_model = FakeModel() |
| 89 | + fake_model.set_next_output([ |
| 90 | + get_function_tool_call("second_tool", "{}") |
| 91 | + ]) |
| 92 | + |
| 93 | + # Create agent with two tools and tool_choice="required" and stop_at_tool behavior |
| 94 | + first_tool = get_function_tool("first_tool", return_value="first tool result") |
| 95 | + second_tool = get_function_tool("second_tool", return_value="second tool result") |
| 96 | + |
| 97 | + agent = Agent( |
| 98 | + name="test_agent", |
| 99 | + model=fake_model, |
| 100 | + tools=[first_tool, second_tool], |
| 101 | + model_settings=ModelSettings(tool_choice="required"), |
| 102 | + tool_use_behavior={"stop_at_tool_names": ["second_tool"]}, |
| 103 | + ) |
| 104 | + |
| 105 | + # Run should stop after using second_tool |
| 106 | + result = await Runner.run(agent, "run test") |
| 107 | + assert result.final_output == "second tool result" |
| 108 | + |
| 109 | + @pytest.mark.asyncio |
| 110 | + async def test_specific_tool_choice(self): |
| 111 | + """ |
| 112 | + Test scenario 3: When using a specific tool choice name |
| 113 | + Ensure it doesn't cause infinite loops |
| 114 | + """ |
| 115 | + # Set up fake model to return a text message |
| 116 | + fake_model = FakeModel() |
| 117 | + fake_model.set_next_output([get_text_message("Test message")]) |
| 118 | + |
| 119 | + # Create agent with specific tool_choice |
| 120 | + tool1 = get_function_tool("tool1") |
| 121 | + tool2 = get_function_tool("tool2") |
| 122 | + tool3 = get_function_tool("tool3") |
| 123 | + |
| 124 | + agent = Agent( |
| 125 | + name="test_agent", |
| 126 | + model=fake_model, |
| 127 | + tools=[tool1, tool2, tool3], |
| 128 | + model_settings=ModelSettings(tool_choice="tool1"), # Specific tool |
| 129 | + ) |
| 130 | + |
| 131 | + # Run should complete without infinite loops |
| 132 | + result = await Runner.run(agent, "first run") |
| 133 | + assert result.final_output == "Test message" |
| 134 | + |
| 135 | + @pytest.mark.asyncio |
| 136 | + async def test_required_with_single_tool(self): |
| 137 | + """ |
| 138 | + Test scenario 4: When using required tool_choice with only one tool |
| 139 | + Ensure it doesn't cause infinite loops |
| 140 | + """ |
| 141 | + # Set up fake model to return a tool call followed by a text message |
| 142 | + fake_model = FakeModel() |
| 143 | + fake_model.add_multiple_turn_outputs([ |
| 144 | + # First call returns a tool call |
| 145 | + [get_function_tool_call("custom_tool", "{}")], |
| 146 | + # Second call returns a text message |
| 147 | + [get_text_message("Final response")] |
| 148 | + ]) |
| 149 | + |
| 150 | + # Create agent with a single tool and tool_choice="required" |
| 151 | + custom_tool = get_function_tool("custom_tool", return_value="tool result") |
| 152 | + agent = Agent( |
| 153 | + name="test_agent", |
| 154 | + model=fake_model, |
| 155 | + tools=[custom_tool], |
| 156 | + model_settings=ModelSettings(tool_choice="required"), |
| 157 | + ) |
| 158 | + |
| 159 | + # Run should complete without infinite loops |
| 160 | + result = await Runner.run(agent, "first run") |
| 161 | + assert result.final_output == "Final response" |
0 commit comments