|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for Enhanced ToolCall Type Definition""" |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from opentelemetry.util.genai.types import ( |
| 20 | + InputMessage, |
| 21 | + OutputMessage, |
| 22 | + ToolCall, |
| 23 | +) |
| 24 | + |
| 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 |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + pytest.main([__file__, "-v"]) |
0 commit comments