-
Notifications
You must be signed in to change notification settings - Fork 877
GenAI Utils | Update ToolCall Type #4218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
keith-decker
wants to merge
7
commits into
open-telemetry:main
Choose a base branch
from
keith-decker:pr1-enhance-toolcall-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−6
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
321ea30
feat: update toolcall types to match semconv
keith-decker 3de0e8d
update changelog
keith-decker b277b72
lint updates
keith-decker 9b3f2a6
feat: refactor ToolCall to ToolCallRequest and enhance type definitions
keith-decker 0f28588
test: point vertexai at current version of genai utils
keith-decker db3802a
Merge branch 'main' into pr1-enhance-toolcall-type
keith-decker 2f009f9
test: fix fileupload test to use ToolCallRequest
keith-decker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ class ContentCapturingMode(Enum): | |
|
|
||
|
|
||
| @dataclass() | ||
| class ToolCall: | ||
| class ToolCallRequest: | ||
| """Represents a tool call requested by the model | ||
|
|
||
| This model is specified as part of semconv in `GenAI messages Python models - ToolCallRequestPart | ||
|
|
@@ -55,6 +55,40 @@ class ToolCall: | |
| type: Literal["tool_call"] = "tool_call" | ||
|
|
||
|
|
||
| @dataclass() | ||
| class ToolCall(ToolCallRequest): | ||
| """Represents a tool call for execution tracking with spans and metrics. | ||
|
|
||
| This type extends ToolCallRequest with additional fields for tracking tool execution | ||
| per the execute_tool span semantic conventions. | ||
|
|
||
| Reference: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-spans.md#execute-tool-span | ||
|
|
||
| For simple message parts (tool calls requested by the model), consider using | ||
| ToolCallRequest instead to avoid unnecessary execution-tracking fields. | ||
|
|
||
| Semantic convention attributes for execute_tool spans: | ||
| - gen_ai.operation.name: "execute_tool" (Required) | ||
| - gen_ai.tool.name: Name of the tool (Recommended) | ||
| - gen_ai.tool.call.id: Tool call identifier (Recommended if available) | ||
| - gen_ai.tool.type: Type classification - "function", "extension", or "datastore" (Recommended if available) | ||
| - gen_ai.tool.description: Tool description (Recommended if available) | ||
| - gen_ai.tool.call.arguments: Parameters passed to tool (Opt-In, may contain sensitive data) | ||
| - gen_ai.tool.call.result: Result returned by tool (Opt-In, may contain sensitive data) | ||
| - error.type: Error type if operation failed (Conditionally Required) | ||
| """ | ||
|
|
||
| # Execution-only fields (used for execute_tool spans): | ||
| # gen_ai.tool.type - Tool type: "function", "extension", or "datastore" | ||
| tool_type: str | None = None | ||
| # gen_ai.tool.description - Description of what the tool does | ||
| tool_description: str | None = None | ||
| # gen_ai.tool.call.result - Result returned by the tool (Opt-In, may contain sensitive data) | ||
| tool_result: Any = None | ||
| # error.type - Error type if the tool call failed | ||
| error_type: str | None = None | ||
|
|
||
|
|
||
| @dataclass() | ||
| class ToolCallResponse: | ||
| """Represents a tool call result sent to the model or a built-in tool call outcome and details | ||
|
|
@@ -158,7 +192,15 @@ class GenericToolDefinition: | |
| ToolDefinition = Union[FunctionToolDefinition, GenericToolDefinition] | ||
|
|
||
| MessagePart = Union[ | ||
| Text, ToolCall, ToolCallResponse, Blob, File, Uri, Reasoning, Any | ||
| Text, | ||
| ToolCallRequest, | ||
| ToolCall, | ||
| ToolCallResponse, | ||
| Blob, | ||
| File, | ||
| Uri, | ||
| Reasoning, | ||
| Any, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a drive by comment not related to this PR, we should remove |
||
| ] | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for ToolCallRequest and ToolCall types""" | ||
|
|
||
| import pytest | ||
|
|
||
| from opentelemetry.util.genai.types import ( | ||
| InputMessage, | ||
| OutputMessage, | ||
| ToolCall, | ||
| ToolCallRequest, | ||
| ) | ||
|
|
||
|
|
||
| def test_toolcallrequest_basic(): | ||
| """Test basic ToolCallRequest instantiation""" | ||
| tcr = ToolCallRequest(arguments=None, name="get_weather", id=None) | ||
| assert tcr.name == "get_weather" | ||
| assert tcr.type == "tool_call" | ||
| assert tcr.arguments is None | ||
| assert tcr.id is None | ||
|
|
||
|
|
||
| def test_toolcallrequest_with_all_fields(): | ||
| """Test ToolCallRequest with all fields""" | ||
| tcr = ToolCallRequest( | ||
| name="get_weather", | ||
| arguments={"location": "Paris"}, | ||
| id="call_123", | ||
| ) | ||
| assert tcr.name == "get_weather" | ||
| assert tcr.arguments == {"location": "Paris"} | ||
| assert tcr.id == "call_123" | ||
| assert tcr.type == "tool_call" | ||
|
|
||
|
|
||
| def test_toolcallrequest_in_message(): | ||
| """Test ToolCallRequest works as message part""" | ||
| tcr = ToolCallRequest( | ||
| arguments={"location": "Paris"}, name="get_weather", id=None | ||
| ) | ||
| msg = InputMessage(role="user", parts=[tcr]) | ||
| assert len(msg.parts) == 1 | ||
| assert msg.parts[0] == tcr | ||
|
|
||
|
|
||
| def test_toolcall_inherits_from_toolcallrequest(): | ||
| """Test that ToolCall inherits from ToolCallRequest""" | ||
| tc = ToolCall(arguments=None, name="get_weather", id=None) | ||
| assert isinstance(tc, ToolCallRequest) | ||
| assert isinstance(tc, ToolCall) | ||
|
|
||
|
|
||
| def test_toolcall_has_execution_fields(): | ||
| """Test ToolCall has execution-only fields""" | ||
| tc = ToolCall(arguments=None, name="get_weather", id=None) | ||
| assert hasattr(tc, "tool_type") | ||
| assert hasattr(tc, "tool_description") | ||
| assert hasattr(tc, "tool_result") | ||
| assert hasattr(tc, "error_type") | ||
|
|
||
|
|
||
| def test_toolcall_execution_fields_default_none(): | ||
| """Test ToolCall execution fields default to None""" | ||
| tc = ToolCall(arguments=None, name="get_weather", id=None) | ||
| assert tc.tool_type is None | ||
| assert tc.tool_description is None | ||
| assert tc.tool_result is None | ||
| assert tc.error_type is None | ||
|
|
||
|
|
||
| def test_toolcall_with_execution_fields(): | ||
| """Test ToolCall with execution fields set""" | ||
| tc = ToolCall( | ||
| name="get_weather", | ||
| arguments={"location": "Paris"}, | ||
| id="call_123", | ||
| tool_type="function", | ||
| tool_description="Get current weather", | ||
| tool_result={"temp": 20, "condition": "sunny"}, | ||
| ) | ||
| assert tc.name == "get_weather" | ||
| assert tc.tool_type == "function" | ||
| assert tc.tool_description == "Get current weather" | ||
| assert tc.tool_result == {"temp": 20, "condition": "sunny"} | ||
|
|
||
|
|
||
| def test_toolcall_with_error(): | ||
| """Test ToolCall with error_type set""" | ||
| tc = ToolCall( | ||
| arguments={"location": "Invalid"}, | ||
| name="get_weather", | ||
| id=None, | ||
| error_type="InvalidLocationError", | ||
| ) | ||
| assert tc.error_type == "InvalidLocationError" | ||
| assert tc.tool_result is None | ||
|
|
||
|
|
||
| def test_toolcall_backward_compatibility(): | ||
| """Test ToolCall still works as message part (backward compatibility)""" | ||
| tc = ToolCall( | ||
| name="get_weather", | ||
| arguments={"location": "Paris"}, | ||
| id="call_123", | ||
| ) | ||
| # Should work in messages | ||
| msg = InputMessage(role="user", parts=[tc]) | ||
| assert len(msg.parts) == 1 | ||
|
|
||
| # Should work in output messages | ||
| out_msg = OutputMessage( | ||
| role="assistant", parts=[tc], finish_reason="tool_calls" | ||
| ) | ||
| assert len(out_msg.parts) == 1 | ||
|
|
||
|
|
||
| def test_toolcallrequest_no_execution_fields(): | ||
| """Test that ToolCallRequest doesn't have execution fields""" | ||
| tcr = ToolCallRequest(arguments=None, name="get_weather", id=None) | ||
| # ToolCallRequest should only have message part fields | ||
| assert not hasattr(tcr, "tool_type") | ||
| assert not hasattr(tcr, "tool_description") | ||
| assert not hasattr(tcr, "tool_result") | ||
| assert not hasattr(tcr, "error_type") | ||
|
|
||
|
|
||
| def test_mixed_types_in_message(): | ||
| """Test using both ToolCallRequest and ToolCall in messages""" | ||
| tcr = ToolCallRequest(arguments=None, name="simple_tool", id=None) | ||
| tc = ToolCall( | ||
| arguments=None, name="complex_tool", id=None, tool_type="function" | ||
| ) | ||
|
|
||
| msg = InputMessage(role="user", parts=[tcr, tc]) | ||
| assert len(msg.parts) == 2 | ||
| assert isinstance(msg.parts[0], ToolCallRequest) | ||
| assert isinstance(msg.parts[1], ToolCall) | ||
| # ToolCall is also a ToolCallRequest | ||
| assert isinstance(msg.parts[1], ToolCallRequest) | ||
|
|
||
|
|
||
| def test_toolcall_tool_type_values(): | ||
| """Test valid tool_type values""" | ||
| for tool_type in ["function", "extension", "datastore"]: | ||
| tc = ToolCall( | ||
| arguments=None, name="test", id=None, tool_type=tool_type | ||
| ) | ||
| assert tc.tool_type == tool_type | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| pytest.main([__file__, "-v"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we are to have 2 different types here, should we also have 2 different
typenames ? Otherwise why not just have 1 type that encompasses both ?