|
| 1 | +""" |
| 2 | +Unit tests for LitellmModel._convert_tool_choice_for_response |
| 3 | +
|
| 4 | +Tests the static method that converts various tool_choice formats |
| 5 | +to the format expected by the Response type. |
| 6 | +
|
| 7 | +Related to Issue #1846: Support tool_choice with specific tool names in LiteLLM streaming |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +from openai import NotGiven, omit |
| 12 | +from openai.types.responses.tool_choice_function import ToolChoiceFunction |
| 13 | + |
| 14 | +from agents.extensions.models.litellm_model import LitellmModel |
| 15 | + |
| 16 | + |
| 17 | +class TestConvertToolChoiceForResponse: |
| 18 | + """Test the _convert_tool_choice_for_response static method.""" |
| 19 | + |
| 20 | + def test_convert_omit_returns_auto(self): |
| 21 | + """Test that omit is converted to 'auto'""" |
| 22 | + result = LitellmModel._convert_tool_choice_for_response(omit) |
| 23 | + assert result == "auto" |
| 24 | + |
| 25 | + def test_convert_not_given_returns_auto(self): |
| 26 | + """Test that NotGiven is converted to 'auto'""" |
| 27 | + result = LitellmModel._convert_tool_choice_for_response(NotGiven()) |
| 28 | + assert result == "auto" |
| 29 | + |
| 30 | + def test_convert_literal_auto(self): |
| 31 | + """Test that literal 'auto' is preserved""" |
| 32 | + result = LitellmModel._convert_tool_choice_for_response("auto") |
| 33 | + assert result == "auto" |
| 34 | + |
| 35 | + def test_convert_literal_required(self): |
| 36 | + """Test that literal 'required' is preserved""" |
| 37 | + result = LitellmModel._convert_tool_choice_for_response("required") |
| 38 | + assert result == "required" |
| 39 | + |
| 40 | + def test_convert_literal_none(self): |
| 41 | + """Test that literal 'none' is preserved""" |
| 42 | + result = LitellmModel._convert_tool_choice_for_response("none") |
| 43 | + assert result == "none" |
| 44 | + |
| 45 | + def test_convert_tool_choice_function_preserved(self): |
| 46 | + """Test that ToolChoiceFunction is preserved as-is""" |
| 47 | + tool_choice = ToolChoiceFunction(type="function", name="my_tool") |
| 48 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice) |
| 49 | + assert result == tool_choice |
| 50 | + assert isinstance(result, ToolChoiceFunction) |
| 51 | + assert result.name == "my_tool" |
| 52 | + |
| 53 | + def test_convert_dict_from_chatcompletions_converter(self): |
| 54 | + """ |
| 55 | + Test conversion from ChatCompletions Converter dict format. |
| 56 | + Format: {"type": "function", "function": {"name": "tool_name"}} |
| 57 | + """ |
| 58 | + tool_choice_dict = { |
| 59 | + "type": "function", |
| 60 | + "function": {"name": "my_custom_tool"}, |
| 61 | + } |
| 62 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_dict) |
| 63 | + assert isinstance(result, ToolChoiceFunction) |
| 64 | + assert result.type == "function" |
| 65 | + assert result.name == "my_custom_tool" |
| 66 | + |
| 67 | + def test_convert_dict_missing_function_name_returns_auto(self): |
| 68 | + """Test that dict without function name falls back to 'auto'""" |
| 69 | + tool_choice_dict = { |
| 70 | + "type": "function", |
| 71 | + "function": {}, # Missing 'name' |
| 72 | + } |
| 73 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_dict) |
| 74 | + assert result == "auto" |
| 75 | + |
| 76 | + def test_convert_dict_empty_function_name_returns_auto(self): |
| 77 | + """Test that dict with empty function name falls back to 'auto'""" |
| 78 | + tool_choice_dict = { |
| 79 | + "type": "function", |
| 80 | + "function": {"name": ""}, # Empty name |
| 81 | + } |
| 82 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_dict) |
| 83 | + assert result == "auto" |
| 84 | + |
| 85 | + def test_convert_dict_missing_function_key_returns_auto(self): |
| 86 | + """Test that dict without 'function' key falls back to 'auto'""" |
| 87 | + tool_choice_dict = {"type": "function"} # Missing 'function' key |
| 88 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_dict) |
| 89 | + assert result == "auto" |
| 90 | + |
| 91 | + def test_convert_dict_wrong_type_returns_auto(self): |
| 92 | + """Test that dict with wrong type falls back to 'auto'""" |
| 93 | + tool_choice_dict = { |
| 94 | + "type": "wrong_type", |
| 95 | + "function": {"name": "my_tool"}, |
| 96 | + } |
| 97 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_dict) |
| 98 | + assert result == "auto" |
| 99 | + |
| 100 | + def test_convert_dict_function_not_dict_returns_auto(self): |
| 101 | + """Test that dict with non-dict function value falls back to 'auto'""" |
| 102 | + tool_choice_dict = { |
| 103 | + "type": "function", |
| 104 | + "function": "not_a_dict", |
| 105 | + } |
| 106 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_dict) |
| 107 | + assert result == "auto" |
| 108 | + |
| 109 | + def test_convert_unexpected_type_returns_auto(self): |
| 110 | + """Test that unexpected types fall back to 'auto'""" |
| 111 | + result = LitellmModel._convert_tool_choice_for_response(123) |
| 112 | + assert result == "auto" |
| 113 | + |
| 114 | + result = LitellmModel._convert_tool_choice_for_response([]) |
| 115 | + assert result == "auto" |
| 116 | + |
| 117 | + result = LitellmModel._convert_tool_choice_for_response(None) |
| 118 | + assert result == "auto" |
| 119 | + |
| 120 | + |
| 121 | +class TestToolChoiceConversionEdgeCases: |
| 122 | + """Test edge cases and real-world scenarios.""" |
| 123 | + |
| 124 | + def test_real_world_scenario_chatcompletions_format(self): |
| 125 | + """ |
| 126 | + Test a real-world scenario from ChatCompletions Converter. |
| 127 | + This is the actual format returned when tool_choice specifies a tool name. |
| 128 | + """ |
| 129 | + # This is what ChatCompletions Converter returns |
| 130 | + tool_choice_from_converter = { |
| 131 | + "type": "function", |
| 132 | + "function": {"name": "get_weather"}, |
| 133 | + } |
| 134 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_from_converter) |
| 135 | + assert isinstance(result, ToolChoiceFunction) |
| 136 | + assert result.name == "get_weather" |
| 137 | + assert result.type == "function" |
| 138 | + |
| 139 | + def test_none_string_vs_none_literal(self): |
| 140 | + """Test that string 'none' works but None (NoneType) defaults to auto""" |
| 141 | + # String "none" should be preserved |
| 142 | + result = LitellmModel._convert_tool_choice_for_response("none") |
| 143 | + assert result == "none" |
| 144 | + |
| 145 | + # NoneType should fallback to auto |
| 146 | + result = LitellmModel._convert_tool_choice_for_response(None) |
| 147 | + assert result == "auto" |
| 148 | + |
| 149 | + def test_complex_tool_name(self): |
| 150 | + """Test that complex tool names are handled correctly""" |
| 151 | + tool_choice_dict = { |
| 152 | + "type": "function", |
| 153 | + "function": {"name": "get_user_profile_with_special_chars_123"}, |
| 154 | + } |
| 155 | + result = LitellmModel._convert_tool_choice_for_response(tool_choice_dict) |
| 156 | + assert isinstance(result, ToolChoiceFunction) |
| 157 | + assert result.name == "get_user_profile_with_special_chars_123" |
0 commit comments