|
10 | 10 | set_data_normalized,
|
11 | 11 | get_start_span_function,
|
12 | 12 | )
|
| 13 | +from sentry_sdk.ai.utils import set_data_normalized, get_start_span_function |
| 14 | +from sentry_sdk.ai.message_utils import truncate_and_serialize_messages |
13 | 15 | from sentry_sdk.consts import OP, SPANDATA
|
14 | 16 | from sentry_sdk.integrations import DidNotEnable, Integration
|
15 | 17 | from sentry_sdk.scope import should_send_default_pii
|
@@ -221,12 +223,11 @@ def on_llm_start(
|
221 | 223 | }
|
222 | 224 | for prompt in prompts
|
223 | 225 | ]
|
224 |
| - set_data_normalized( |
225 |
| - span, |
226 |
| - SPANDATA.GEN_AI_REQUEST_MESSAGES, |
227 |
| - normalized_messages, |
228 |
| - unpack=False, |
229 |
| - ) |
| 226 | + result = truncate_and_serialize_messages(prompts) |
| 227 | + if result["serialized_data"]: |
| 228 | + span.set_data( |
| 229 | + SPANDATA.GEN_AI_REQUEST_MESSAGES, result["serialized_data"] |
| 230 | + ) |
230 | 231 |
|
231 | 232 | def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
|
232 | 233 | # type: (SentryLangchainCallback, Dict[str, Any], List[List[BaseMessage]], UUID, Any) -> Any
|
@@ -278,13 +279,11 @@ def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
|
278 | 279 | self._normalize_langchain_message(message)
|
279 | 280 | )
|
280 | 281 | normalized_messages = normalize_message_roles(normalized_messages)
|
281 |
| - |
282 |
| - set_data_normalized( |
283 |
| - span, |
284 |
| - SPANDATA.GEN_AI_REQUEST_MESSAGES, |
285 |
| - normalized_messages, |
286 |
| - unpack=False, |
287 |
| - ) |
| 282 | + result = truncate_and_serialize_messages(normalized_messages) |
| 283 | + if result["serialized_data"]: |
| 284 | + span.set_data( |
| 285 | + SPANDATA.GEN_AI_REQUEST_MESSAGES, result["serialized_data"] |
| 286 | + ) |
288 | 287 |
|
289 | 288 | def on_chat_model_end(self, response, *, run_id, **kwargs):
|
290 | 289 | # type: (SentryLangchainCallback, LLMResult, UUID, Any) -> Any
|
@@ -758,12 +757,11 @@ def new_invoke(self, *args, **kwargs):
|
758 | 757 | and integration.include_prompts
|
759 | 758 | ):
|
760 | 759 | normalized_messages = normalize_message_roles([input])
|
761 |
| - set_data_normalized( |
762 |
| - span, |
763 |
| - SPANDATA.GEN_AI_REQUEST_MESSAGES, |
764 |
| - normalized_messages, |
765 |
| - unpack=False, |
766 |
| - ) |
| 760 | + result = truncate_and_serialize_messages(normalized_messages) |
| 761 | + if result["serialized_data"]: |
| 762 | + span.set_data( |
| 763 | + SPANDATA.GEN_AI_REQUEST_MESSAGES, result["serialized_data"] |
| 764 | + ) |
767 | 765 |
|
768 | 766 | output = result.get("output")
|
769 | 767 | if (
|
@@ -813,12 +811,11 @@ def new_stream(self, *args, **kwargs):
|
813 | 811 | and integration.include_prompts
|
814 | 812 | ):
|
815 | 813 | normalized_messages = normalize_message_roles([input])
|
816 |
| - set_data_normalized( |
817 |
| - span, |
818 |
| - SPANDATA.GEN_AI_REQUEST_MESSAGES, |
819 |
| - normalized_messages, |
820 |
| - unpack=False, |
821 |
| - ) |
| 814 | + result = truncate_and_serialize_messages(normalized_messages) |
| 815 | + if result["serialized_data"]: |
| 816 | + span.set_data( |
| 817 | + SPANDATA.GEN_AI_REQUEST_MESSAGES, result["serialized_data"] |
| 818 | + ) |
822 | 819 |
|
823 | 820 | # Run the agent
|
824 | 821 | result = f(self, *args, **kwargs)
|
|
0 commit comments