|
23 | 23 | ) |
24 | 24 |
|
25 | 25 |
|
26 | | -class TestToolCallEnhancements: |
27 | | - """Test suite for Enhanced ToolCall Type Definition""" |
28 | | - |
29 | | - def test_toolcall_backward_compatibility(self): |
30 | | - """Test backward compatibility as message part""" |
31 | | - tc = ToolCall( |
32 | | - name="get_weather", |
33 | | - arguments={"location": "Paris"}, |
34 | | - id="call_123", |
35 | | - ) |
36 | | - assert tc.name == "get_weather" |
37 | | - assert tc.arguments == {"location": "Paris"} |
38 | | - assert tc.id == "call_123" |
39 | | - assert tc.type == "tool_call" |
40 | | - |
41 | | - def test_toolcall_in_message(self): |
42 | | - """Test ToolCall works as message part in InputMessage""" |
43 | | - tc = ToolCall(name="get_weather", arguments={"location": "Paris"}) |
44 | | - msg = InputMessage(role="user", parts=[tc]) |
45 | | - assert len(msg.parts) == 1 |
46 | | - assert msg.parts[0] == tc |
47 | | - |
48 | | - def test_toolcall_full_lifecycle(self): |
49 | | - """Test complete tool call lifecycle with all fields""" |
50 | | - # Start with tool call request |
51 | | - tc = ToolCall( |
52 | | - name="get_weather", |
53 | | - arguments={"location": "Paris", "units": "metric"}, |
54 | | - id="call_abc123", |
55 | | - tool_type="function", |
56 | | - tool_description="Retrieves current weather for a location", |
57 | | - ) |
58 | | - |
59 | | - # Simulate successful execution - set result |
60 | | - tc.tool_result = {"temperature": 15, "condition": "cloudy"} |
61 | | - |
62 | | - assert tc.name == "get_weather" |
63 | | - assert tc.tool_type == "function" |
64 | | - assert tc.tool_result is not None |
65 | | - assert tc.error_type is None |
66 | | - |
67 | | - # Simulate failed execution - set error |
68 | | - tc_failed = ToolCall( |
69 | | - name="get_weather", |
70 | | - arguments={"location": "Invalid"}, |
71 | | - id="call_xyz789", |
72 | | - tool_type="function", |
73 | | - ) |
74 | | - tc_failed.error_type = "InvalidLocationError" |
75 | | - |
76 | | - assert tc_failed.error_type == "InvalidLocationError" |
77 | | - assert tc_failed.tool_result is None |
78 | | - |
79 | | - def test_toolcall_with_output_message(self): |
80 | | - """Test ToolCall in OutputMessage (backward compatibility)""" |
81 | | - tc = ToolCall( |
82 | | - name="get_weather", |
83 | | - arguments={"location": "Paris"}, |
84 | | - id="call_123", |
85 | | - ) |
86 | | - msg = OutputMessage( |
87 | | - role="assistant", parts=[tc], finish_reason="tool_calls" |
88 | | - ) |
89 | | - |
90 | | - assert len(msg.parts) == 1 |
91 | | - assert msg.parts[0].name == "get_weather" |
92 | | - assert msg.finish_reason == "tool_calls" |
93 | | - |
94 | | - def test_toolcall_field_values(self): |
95 | | - """Test that ToolCall fields can be set and retrieved correctly""" |
96 | | - tc = ToolCall( |
97 | | - name="get_weather", |
98 | | - id="call_123", |
99 | | - tool_type="function", |
100 | | - tool_description="Weather tool", |
101 | | - arguments={"location": "Paris"}, |
102 | | - tool_result={"temp": 20}, |
103 | | - ) |
104 | | - |
105 | | - # Verify all field values are set correctly |
106 | | - assert tc.name == "get_weather" |
107 | | - assert tc.id == "call_123" |
108 | | - assert tc.tool_type == "function" |
109 | | - assert tc.tool_description == "Weather tool" |
110 | | - assert tc.arguments == {"location": "Paris"} |
111 | | - assert tc.tool_result == {"temp": 20} |
112 | | - assert tc.error_type is None |
113 | | - |
114 | | - # Verify these fields map to semantic convention attributes: |
115 | | - # - name -> gen_ai.tool.name |
116 | | - # - id -> gen_ai.tool.call.id |
117 | | - # - tool_type -> gen_ai.tool.type |
118 | | - # - tool_description -> gen_ai.tool.description |
119 | | - # - arguments -> gen_ai.tool.call.arguments (Opt-In) |
120 | | - # - tool_result -> gen_ai.tool.call.result (Opt-In) |
121 | | - # - error_type -> error.type |
| 26 | +def test_toolcall_backward_compatibility(): |
| 27 | + """Test backward compatibility as message part""" |
| 28 | + tc = ToolCall( |
| 29 | + name="get_weather", |
| 30 | + arguments={"location": "Paris"}, |
| 31 | + id="call_123", |
| 32 | + ) |
| 33 | + assert tc.name == "get_weather" |
| 34 | + assert tc.arguments == {"location": "Paris"} |
| 35 | + assert tc.id == "call_123" |
| 36 | + assert tc.type == "tool_call" |
| 37 | + |
| 38 | + |
| 39 | +def test_toolcall_in_message(): |
| 40 | + """Test ToolCall works as message part in InputMessage""" |
| 41 | + tc = ToolCall(name="get_weather", arguments={"location": "Paris"}) |
| 42 | + msg = InputMessage(role="user", parts=[tc]) |
| 43 | + assert len(msg.parts) == 1 |
| 44 | + assert msg.parts[0] == tc |
| 45 | + |
| 46 | + |
| 47 | +def test_toolcall_full_lifecycle(): |
| 48 | + """Test complete tool call lifecycle with all fields""" |
| 49 | + # Start with tool call request |
| 50 | + tc = ToolCall( |
| 51 | + name="get_weather", |
| 52 | + arguments={"location": "Paris", "units": "metric"}, |
| 53 | + id="call_abc123", |
| 54 | + tool_type="function", |
| 55 | + tool_description="Retrieves current weather for a location", |
| 56 | + ) |
| 57 | + |
| 58 | + # Simulate successful execution - set result |
| 59 | + tc.tool_result = {"temperature": 15, "condition": "cloudy"} |
| 60 | + |
| 61 | + assert tc.name == "get_weather" |
| 62 | + assert tc.tool_type == "function" |
| 63 | + assert tc.tool_result is not None |
| 64 | + assert tc.error_type is None |
| 65 | + |
| 66 | + # Simulate failed execution - set error |
| 67 | + tc_failed = ToolCall( |
| 68 | + name="get_weather", |
| 69 | + arguments={"location": "Invalid"}, |
| 70 | + id="call_xyz789", |
| 71 | + tool_type="function", |
| 72 | + ) |
| 73 | + tc_failed.error_type = "InvalidLocationError" |
| 74 | + |
| 75 | + assert tc_failed.error_type == "InvalidLocationError" |
| 76 | + assert tc_failed.tool_result is None |
| 77 | + |
| 78 | + |
| 79 | +def test_toolcall_with_output_message(): |
| 80 | + """Test ToolCall in OutputMessage (backward compatibility)""" |
| 81 | + tc = ToolCall( |
| 82 | + name="get_weather", |
| 83 | + arguments={"location": "Paris"}, |
| 84 | + id="call_123", |
| 85 | + ) |
| 86 | + msg = OutputMessage( |
| 87 | + role="assistant", parts=[tc], finish_reason="tool_calls" |
| 88 | + ) |
| 89 | + |
| 90 | + assert len(msg.parts) == 1 |
| 91 | + assert msg.parts[0].name == "get_weather" |
| 92 | + assert msg.finish_reason == "tool_calls" |
| 93 | + |
| 94 | + |
| 95 | +def test_toolcall_field_values(): |
| 96 | + """Test that ToolCall fields can be set and retrieved correctly""" |
| 97 | + tc = ToolCall( |
| 98 | + name="get_weather", |
| 99 | + id="call_123", |
| 100 | + tool_type="function", |
| 101 | + tool_description="Weather tool", |
| 102 | + arguments={"location": "Paris"}, |
| 103 | + tool_result={"temp": 20}, |
| 104 | + ) |
| 105 | + |
| 106 | + # Verify all field values are set correctly |
| 107 | + assert tc.name == "get_weather" |
| 108 | + assert tc.id == "call_123" |
| 109 | + assert tc.tool_type == "function" |
| 110 | + assert tc.tool_description == "Weather tool" |
| 111 | + assert tc.arguments == {"location": "Paris"} |
| 112 | + assert tc.tool_result == {"temp": 20} |
| 113 | + assert tc.error_type is None |
| 114 | + |
| 115 | + # Verify these fields map to semantic convention attributes: |
| 116 | + # - name -> gen_ai.tool.name |
| 117 | + # - id -> gen_ai.tool.call.id |
| 118 | + # - tool_type -> gen_ai.tool.type |
| 119 | + # - tool_description -> gen_ai.tool.description |
| 120 | + # - arguments -> gen_ai.tool.call.arguments (Opt-In) |
| 121 | + # - tool_result -> gen_ai.tool.call.result (Opt-In) |
| 122 | + # - error_type -> error.type |
122 | 123 |
|
123 | 124 |
|
124 | 125 | if __name__ == "__main__": |
|
0 commit comments