|
4 | 4 |
|
5 | 5 | import sentry_sdk |
6 | 6 | from sentry_sdk.ai.monitoring import set_ai_pipeline_name |
7 | | -from sentry_sdk.ai.utils import set_data_normalized, get_start_span_function |
| 7 | +from sentry_sdk.ai.utils import ( |
| 8 | + normalize_message_roles, |
| 9 | + set_data_normalized, |
| 10 | + get_start_span_function, |
| 11 | +) |
8 | 12 | from sentry_sdk.consts import OP, SPANDATA |
9 | 13 | from sentry_sdk.integrations import DidNotEnable, Integration |
10 | 14 | from sentry_sdk.scope import should_send_default_pii |
@@ -209,8 +213,12 @@ def on_llm_start( |
209 | 213 | _set_tools_on_span(span, all_params.get("tools")) |
210 | 214 |
|
211 | 215 | if should_send_default_pii() and self.include_prompts: |
| 216 | + normalized_messages = normalize_message_roles(prompts) |
212 | 217 | set_data_normalized( |
213 | | - span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompts, unpack=False |
| 218 | + span, |
| 219 | + SPANDATA.GEN_AI_REQUEST_MESSAGES, |
| 220 | + normalized_messages, |
| 221 | + unpack=False, |
214 | 222 | ) |
215 | 223 |
|
216 | 224 | def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs): |
@@ -262,6 +270,8 @@ def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs): |
262 | 270 | normalized_messages.append( |
263 | 271 | self._normalize_langchain_message(message) |
264 | 272 | ) |
| 273 | + normalized_messages = normalize_message_roles(normalized_messages) |
| 274 | + |
265 | 275 | set_data_normalized( |
266 | 276 | span, |
267 | 277 | SPANDATA.GEN_AI_REQUEST_MESSAGES, |
@@ -740,8 +750,12 @@ def new_invoke(self, *args, **kwargs): |
740 | 750 | and should_send_default_pii() |
741 | 751 | and integration.include_prompts |
742 | 752 | ): |
| 753 | + normalized_messages = normalize_message_roles([input]) |
743 | 754 | set_data_normalized( |
744 | | - span, SPANDATA.GEN_AI_REQUEST_MESSAGES, [input], unpack=False |
| 755 | + span, |
| 756 | + SPANDATA.GEN_AI_REQUEST_MESSAGES, |
| 757 | + normalized_messages, |
| 758 | + unpack=False, |
745 | 759 | ) |
746 | 760 |
|
747 | 761 | output = result.get("output") |
@@ -791,8 +805,12 @@ def new_stream(self, *args, **kwargs): |
791 | 805 | and should_send_default_pii() |
792 | 806 | and integration.include_prompts |
793 | 807 | ): |
| 808 | + normalized_messages = normalize_message_roles([input]) |
794 | 809 | set_data_normalized( |
795 | | - span, SPANDATA.GEN_AI_REQUEST_MESSAGES, [input], unpack=False |
| 810 | + span, |
| 811 | + SPANDATA.GEN_AI_REQUEST_MESSAGES, |
| 812 | + normalized_messages, |
| 813 | + unpack=False, |
796 | 814 | ) |
797 | 815 |
|
798 | 816 | # Run the agent |
|
0 commit comments