|  | 
| 9 | 9 |     normalize_message_roles, | 
| 10 | 10 |     set_data_normalized, | 
| 11 | 11 |     get_start_span_function, | 
|  | 12 | +    truncate_and_annotate_messages, | 
| 12 | 13 | ) | 
| 13 | 14 | from sentry_sdk.consts import OP, SPANDATA | 
| 14 | 15 | from sentry_sdk.integrations import DidNotEnable, Integration | 
| @@ -221,12 +222,17 @@ def on_llm_start( | 
| 221 | 222 |                     } | 
| 222 | 223 |                     for prompt in prompts | 
| 223 | 224 |                 ] | 
| 224 |  | -                set_data_normalized( | 
| 225 |  | -                    span, | 
| 226 |  | -                    SPANDATA.GEN_AI_REQUEST_MESSAGES, | 
| 227 |  | -                    normalized_messages, | 
| 228 |  | -                    unpack=False, | 
|  | 225 | +                scope = sentry_sdk.get_current_scope() | 
|  | 226 | +                messages_data = truncate_and_annotate_messages( | 
|  | 227 | +                    normalized_messages, span, scope | 
| 229 | 228 |                 ) | 
|  | 229 | +                if messages_data is not None: | 
|  | 230 | +                    set_data_normalized( | 
|  | 231 | +                        span, | 
|  | 232 | +                        SPANDATA.GEN_AI_REQUEST_MESSAGES, | 
|  | 233 | +                        messages_data, | 
|  | 234 | +                        unpack=False, | 
|  | 235 | +                    ) | 
| 230 | 236 | 
 | 
| 231 | 237 |     def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs): | 
| 232 | 238 |         # type: (SentryLangchainCallback, Dict[str, Any], List[List[BaseMessage]], UUID, Any) -> Any | 
| @@ -278,13 +284,17 @@ def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs): | 
| 278 | 284 |                             self._normalize_langchain_message(message) | 
| 279 | 285 |                         ) | 
| 280 | 286 |                 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 | +                scope = sentry_sdk.get_current_scope() | 
|  | 288 | +                messages_data = truncate_and_annotate_messages( | 
|  | 289 | +                    normalized_messages, span, scope | 
| 287 | 290 |                 ) | 
|  | 291 | +                if messages_data is not None: | 
|  | 292 | +                    set_data_normalized( | 
|  | 293 | +                        span, | 
|  | 294 | +                        SPANDATA.GEN_AI_REQUEST_MESSAGES, | 
|  | 295 | +                        messages_data, | 
|  | 296 | +                        unpack=False, | 
|  | 297 | +                    ) | 
| 288 | 298 | 
 | 
| 289 | 299 |     def on_chat_model_end(self, response, *, run_id, **kwargs): | 
| 290 | 300 |         # type: (SentryLangchainCallback, LLMResult, UUID, Any) -> Any | 
| @@ -758,12 +768,17 @@ def new_invoke(self, *args, **kwargs): | 
| 758 | 768 |                 and integration.include_prompts | 
| 759 | 769 |             ): | 
| 760 | 770 |                 normalized_messages = normalize_message_roles([input]) | 
| 761 |  | -                set_data_normalized( | 
| 762 |  | -                    span, | 
| 763 |  | -                    SPANDATA.GEN_AI_REQUEST_MESSAGES, | 
| 764 |  | -                    normalized_messages, | 
| 765 |  | -                    unpack=False, | 
|  | 771 | +                scope = sentry_sdk.get_current_scope() | 
|  | 772 | +                messages_data = truncate_and_annotate_messages( | 
|  | 773 | +                    normalized_messages, span, scope | 
| 766 | 774 |                 ) | 
|  | 775 | +                if messages_data is not None: | 
|  | 776 | +                    set_data_normalized( | 
|  | 777 | +                        span, | 
|  | 778 | +                        SPANDATA.GEN_AI_REQUEST_MESSAGES, | 
|  | 779 | +                        messages_data, | 
|  | 780 | +                        unpack=False, | 
|  | 781 | +                    ) | 
| 767 | 782 | 
 | 
| 768 | 783 |             output = result.get("output") | 
| 769 | 784 |             if ( | 
| @@ -813,12 +828,17 @@ def new_stream(self, *args, **kwargs): | 
| 813 | 828 |             and integration.include_prompts | 
| 814 | 829 |         ): | 
| 815 | 830 |             normalized_messages = normalize_message_roles([input]) | 
| 816 |  | -            set_data_normalized( | 
| 817 |  | -                span, | 
| 818 |  | -                SPANDATA.GEN_AI_REQUEST_MESSAGES, | 
| 819 |  | -                normalized_messages, | 
| 820 |  | -                unpack=False, | 
|  | 831 | +            scope = sentry_sdk.get_current_scope() | 
|  | 832 | +            messages_data = truncate_and_annotate_messages( | 
|  | 833 | +                normalized_messages, span, scope | 
| 821 | 834 |             ) | 
|  | 835 | +            if messages_data is not None: | 
|  | 836 | +                set_data_normalized( | 
|  | 837 | +                    span, | 
|  | 838 | +                    SPANDATA.GEN_AI_REQUEST_MESSAGES, | 
|  | 839 | +                    messages_data, | 
|  | 840 | +                    unpack=False, | 
|  | 841 | +                ) | 
| 822 | 842 | 
 | 
| 823 | 843 |         # Run the agent | 
| 824 | 844 |         result = f(self, *args, **kwargs) | 
|  | 
0 commit comments