Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from collections.abc import Iterable
from functools import wraps
from typing import TYPE_CHECKING

import sentry_sdk
from sentry_sdk.ai.monitoring import record_token_usage
from sentry_sdk.ai.utils import (
GEN_AI_ALLOWED_MESSAGE_ROLES,
set_data_normalized,
normalize_message_roles,
truncate_and_annotate_messages,
Expand Down Expand Up @@ -122,6 +124,7 @@ def _set_input_data(span, kwargs, integration):
"""
Set input data for the span based on the provided keyword arguments for the anthropic message creation.
"""
system_prompt = kwargs.get("system")
messages = kwargs.get("messages")
if (
messages is not None
Expand All @@ -130,17 +133,39 @@ def _set_input_data(span, kwargs, integration):
and integration.include_prompts
):
normalized_messages = []
if system_prompt:
system_prompt_content = None
if isinstance(system_prompt, str):
system_prompt_content = system_prompt
elif isinstance(system_prompt, Iterable):
system_prompt_content = []
for item in system_prompt:
if (
isinstance(item, dict)
and item.get("type") == "text"
and item.get("text")
):
system_prompt_content.append(item.copy())

if system_prompt_content:
normalized_messages.append(
{
"role": GEN_AI_ALLOWED_MESSAGE_ROLES.SYSTEM,
"content": system_prompt_content,
}
)

for message in messages:
if (
message.get("role") == "user"
message.get("role") == GEN_AI_ALLOWED_MESSAGE_ROLES.USER
and "content" in message
and isinstance(message["content"], (list, tuple))
):
for item in message["content"]:
if item.get("type") == "tool_result":
normalized_messages.append(
{
"role": "tool",
"role": GEN_AI_ALLOWED_MESSAGE_ROLES.TOOL,
"content": {
"tool_use_id": item.get("tool_use_id"),
"output": item.get("content"),
Expand Down
Loading
Loading