Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
66 changes: 11 additions & 55 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
normalize_message_roles,
truncate_and_annotate_messages,
get_start_span_function,
transform_anthropic_content_part,
)
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
Expand Down Expand Up @@ -323,27 +322,6 @@ def _collect_ai_data(
)


def _transform_anthropic_content_block(
content_block: "dict[str, Any]",
) -> "dict[str, Any]":
"""
Transform an Anthropic content block using the Anthropic-specific transformer,
with special handling for Anthropic's text-type documents.
"""
# Handle Anthropic's text-type documents specially (not covered by shared function)
if content_block.get("type") == "document":
source = content_block.get("source")
if isinstance(source, dict) and source.get("type") == "text":
return {
"type": "text",
"text": source.get("data", ""),
}

# Use Anthropic-specific transformation
result = transform_anthropic_content_part(content_block)
return result if result is not None else content_block


def _transform_system_instructions(
system_instructions: "Union[str, Iterable[TextBlockParam]]",
) -> "list[TextPart]":
Expand Down Expand Up @@ -401,41 +379,19 @@ def _set_common_input_data(
and "content" in message
and isinstance(message["content"], (list, tuple))
):
transformed_content = []
for item in message["content"]:
# Skip tool_result items - they can contain images/documents
# with nested structures that are difficult to redact properly
if isinstance(item, dict) and item.get("type") == "tool_result":
continue

# Transform content blocks (images, documents, etc.)
transformed_content.append(
_transform_anthropic_content_block(item)
if isinstance(item, dict)
else item
)

# If there are non-tool-result items, add them as a message
if transformed_content:
normalized_messages.append(
{
"role": message.get("role"),
"content": transformed_content,
}
)
if item.get("type") == "tool_result":
normalized_messages.append(
{
"role": GEN_AI_ALLOWED_MESSAGE_ROLES.TOOL,
"content": {
"tool_use_id": item.get("tool_use_id"),
"output": item.get("content"),
},
}
)
else:
# Transform content for non-list messages or assistant messages
transformed_message = message.copy()
if "content" in transformed_message:
content = transformed_message["content"]
if isinstance(content, (list, tuple)):
transformed_message["content"] = [
_transform_anthropic_content_block(item)
if isinstance(item, dict)
else item
for item in content
]
normalized_messages.append(transformed_message)
normalized_messages.append(message)

role_normalized_messages = normalize_message_roles(normalized_messages)
scope = sentry_sdk.get_current_scope()
Expand Down
Loading
Loading