Skip to content

Commit 048c2d5

Browse files
committed
fix(integrations): add the system prompt to the gen_ai.request.messages attribute
1 parent 027aa6e commit 048c2d5

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

sentry_sdk/integrations/anthropic.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from collections.abc import Iterable
12
from functools import wraps
23
from typing import TYPE_CHECKING
34

45
import sentry_sdk
56
from sentry_sdk.ai.monitoring import record_token_usage
67
from sentry_sdk.ai.utils import (
8+
GEN_AI_ALLOWED_MESSAGE_ROLES,
79
set_data_normalized,
810
normalize_message_roles,
911
truncate_and_annotate_messages,
@@ -122,6 +124,7 @@ def _set_input_data(span, kwargs, integration):
122124
"""
123125
Set input data for the span based on the provided keyword arguments for the anthropic message creation.
124126
"""
127+
system_prompt = kwargs.get("system")
125128
messages = kwargs.get("messages")
126129
if (
127130
messages is not None
@@ -130,17 +133,38 @@ def _set_input_data(span, kwargs, integration):
130133
and integration.include_prompts
131134
):
132135
normalized_messages = []
136+
if system_prompt:
137+
if isinstance(system_prompt, str):
138+
system_prompt_content = system_prompt
139+
elif isinstance(system_prompt, Iterable):
140+
system_prompt_content = []
141+
for item in system_prompt:
142+
if (
143+
isinstance(item, dict)
144+
and item.get("type") == "text"
145+
and item.get("text")
146+
):
147+
system_prompt_content.append(item.copy())
148+
149+
if system_prompt_content:
150+
normalized_messages.append(
151+
{
152+
"role": GEN_AI_ALLOWED_MESSAGE_ROLES.SYSTEM,
153+
"content": system_prompt_content,
154+
}
155+
)
156+
133157
for message in messages:
134158
if (
135-
message.get("role") == "user"
159+
message.get("role") == GEN_AI_ALLOWED_MESSAGE_ROLES.USER
136160
and "content" in message
137161
and isinstance(message["content"], (list, tuple))
138162
):
139163
for item in message["content"]:
140164
if item.get("type") == "tool_result":
141165
normalized_messages.append(
142166
{
143-
"role": "tool",
167+
"role": GEN_AI_ALLOWED_MESSAGE_ROLES.TOOL,
144168
"content": {
145169
"tool_use_id": item.get("tool_use_id"),
146170
"output": item.get("content"),

0 commit comments

Comments
 (0)