-
Notifications
You must be signed in to change notification settings - Fork 597
fix(anthropic): send tool definitions in standardized format #5800
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
valkrypton
wants to merge
2
commits into
getsentry:master
Choose a base branch
from
valkrypton:fix/anthropic-tool-definitions
base: master
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.
+162
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
123 changes: 123 additions & 0 deletions
123
tests/integrations/anthropic/test_anthropic_tools_definitions.py
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,123 @@ | ||
| import pytest | ||
| from unittest import mock | ||
| import json | ||
|
|
||
| from anthropic import Anthropic | ||
| from anthropic.types.message import Message | ||
| from anthropic.types.usage import Usage | ||
|
|
||
| try: | ||
| from anthropic.types.text_block import TextBlock | ||
| except ImportError: | ||
| from anthropic.types.content_block import ContentBlock as TextBlock | ||
|
|
||
| from sentry_sdk import start_transaction | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.integrations.anthropic import AnthropicIntegration | ||
|
|
||
|
|
||
| EXAMPLE_MESSAGE = Message( | ||
| id="msg_01XFDUDYJgAACzvnptvVoYEL", | ||
| model="model", | ||
| role="assistant", | ||
| content=[TextBlock(type="text", text="Hi, I'm Claude.")], | ||
| type="message", | ||
| stop_reason="end_turn", | ||
| usage=Usage(input_tokens=10, output_tokens=20), | ||
| ) | ||
|
|
||
| TOOLS = [ | ||
| { | ||
| "name": "get_weather", | ||
| "description": "Get the current weather in a given location", | ||
| "input_schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "location": { | ||
| "type": "string", | ||
| "description": "The city and state, e.g. San Francisco, CA", | ||
| }, | ||
| "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, | ||
| }, | ||
| "required": ["location"], | ||
| }, | ||
| }, | ||
| { | ||
| "name": "no_description_tool", | ||
| "input_schema": { | ||
| "type": "object", | ||
| "properties": {"arg1": {"type": "string"}}, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "send_default_pii, include_prompts", | ||
| [ | ||
| (True, True), | ||
| (True, False), | ||
| (False, True), | ||
| (False, False), | ||
| ], | ||
| ) | ||
| def test_tool_definitions_in_create_message( | ||
| sentry_init, capture_events, send_default_pii, include_prompts | ||
| ): | ||
| sentry_init( | ||
| integrations=[AnthropicIntegration(include_prompts=include_prompts)], | ||
| traces_sample_rate=1.0, | ||
| send_default_pii=send_default_pii, | ||
| ) | ||
| events = capture_events() | ||
| client = Anthropic(api_key="z") | ||
| client.messages._post = mock.Mock(return_value=EXAMPLE_MESSAGE) | ||
|
|
||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": "What is the weather in San Francisco?", | ||
| } | ||
| ] | ||
|
|
||
| with start_transaction(name="anthropic"): | ||
| client.messages.create( | ||
| max_tokens=1024, | ||
| messages=messages, | ||
| model="model", | ||
| tools=TOOLS, | ||
| ) | ||
|
|
||
| assert len(events) == 1 | ||
| (event,) = events | ||
| (span,) = event["spans"] | ||
|
|
||
| assert span["op"] == OP.GEN_AI_CHAT | ||
|
|
||
| # Check old available_tools attribute (always present if tools provided) | ||
| assert SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in span["data"] | ||
| available_tools = json.loads(span["data"][SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS]) | ||
| assert available_tools == TOOLS | ||
|
|
||
| # Check new tool.definitions attribute (only present if PII and prompts enabled) | ||
| if send_default_pii and include_prompts: | ||
| assert SPANDATA.GEN_AI_TOOL_DEFINITIONS in span["data"] | ||
| tool_definitions = json.loads(span["data"][SPANDATA.GEN_AI_TOOL_DEFINITIONS]) | ||
| assert len(tool_definitions) == 2 | ||
|
|
||
| # Check tool with description | ||
| assert tool_definitions[0]["name"] == "get_weather" | ||
| assert ( | ||
| tool_definitions[0]["description"] | ||
| == "Get the current weather in a given location" | ||
| ) | ||
| assert tool_definitions[0]["type"] == "function" | ||
| assert tool_definitions[0]["parameters"] == TOOLS[0]["input_schema"] | ||
|
|
||
| # Check tool without description | ||
| assert tool_definitions[1]["name"] == "no_description_tool" | ||
| assert "description" not in tool_definitions[1] | ||
| assert tool_definitions[1]["type"] == "function" | ||
| assert tool_definitions[1]["parameters"] == TOOLS[1]["input_schema"] | ||
| else: | ||
| assert SPANDATA.GEN_AI_TOOL_DEFINITIONS not in span["data"] |
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.
Uh oh!
There was an error while loading. Please reload this page.