44
55import sentry_sdk
66from sentry_sdk .ai .monitoring import set_ai_pipeline_name , record_token_usage
7- from sentry_sdk .consts import OP , SPANDATA
87from sentry_sdk .ai .utils import set_data_normalized
8+ from sentry_sdk .consts import OP , SPANDATA
9+ from sentry_sdk .integrations import DidNotEnable , Integration
910from sentry_sdk .scope import should_send_default_pii
1011from sentry_sdk .tracing import Span
11- from sentry_sdk .integrations import DidNotEnable , Integration
1212from sentry_sdk .utils import logger , capture_internal_exceptions
1313
1414from typing import TYPE_CHECKING
1515
1616if TYPE_CHECKING :
1717 from typing import (
1818 Any ,
19- List ,
19+ AsyncIterator ,
2020 Callable ,
2121 Dict ,
22- Union ,
23- Optional ,
24- AsyncIterator ,
2522 Iterator ,
23+ List ,
24+ Optional ,
25+ Union ,
2626 )
2727 from uuid import UUID
2828
2929
3030try :
31- from langchain_core . messages import BaseMessage
32- from langchain_core .outputs import LLMResult
31+ from langchain . agents import AgentExecutor
32+ from langchain_core .agents import AgentAction , AgentFinish
3333 from langchain_core .callbacks import (
34- manager ,
3534 BaseCallbackHandler ,
3635 BaseCallbackManager ,
3736 Callbacks ,
37+ manager ,
3838 )
39- from langchain_core .agents import AgentAction , AgentFinish
40- from langchain . agents import AgentExecutor
39+ from langchain_core .messages import BaseMessage
40+ from langchain_core . outputs import LLMResult
4141
4242except ImportError :
4343 raise DidNotEnable ("langchain not installed" )
@@ -764,7 +764,7 @@ def new_invoke(self, *args, **kwargs):
764764 result = f (self , * args , ** kwargs )
765765
766766 input = result .get ("input" )
767- if input is not None :
767+ if input is not None and should_send_default_pii () and self . include_prompts :
768768 set_data_normalized (
769769 span ,
770770 SPANDATA .GEN_AI_REQUEST_MESSAGES ,
@@ -774,7 +774,11 @@ def new_invoke(self, *args, **kwargs):
774774 )
775775
776776 output = result .get ("output" )
777- if output is not None :
777+ if (
778+ output is not None
779+ and should_send_default_pii ()
780+ and self .include_prompts
781+ ):
778782 span .set_data (SPANDATA .GEN_AI_RESPONSE_TEXT , output )
779783
780784 return result
@@ -813,7 +817,7 @@ def new_stream(self, *args, **kwargs):
813817 )
814818
815819 input = args [0 ].get ("input" ) if len (args ) > 1 else None
816- if input is not None :
820+ if input is not None and should_send_default_pii () and self . include_prompts :
817821 set_data_normalized (
818822 span ,
819823 SPANDATA .GEN_AI_REQUEST_MESSAGES ,
@@ -833,7 +837,11 @@ def new_iterator():
833837 yield event
834838
835839 output = event .get ("output" )
836- if output is not None :
840+ if (
841+ output is not None
842+ and should_send_default_pii ()
843+ and self .include_prompts
844+ ):
837845 span .set_data (SPANDATA .GEN_AI_RESPONSE_TEXT , output )
838846
839847 span .__exit__ (None , None , None )
0 commit comments