1+ from collections .abc import Iterable
12from functools import wraps
23from typing import TYPE_CHECKING
34
45import sentry_sdk
56from sentry_sdk .ai .monitoring import record_token_usage
67from 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