1818from sentry_sdk .utils import (
1919 capture_internal_exceptions ,
2020 event_from_exception ,
21+ has_data_collection_enabled ,
2122 reraise ,
2223)
2324
@@ -74,7 +75,8 @@ def _capture_exception(exc: "Any") -> None:
7475def _wrap_huggingface_task (f : "Callable[..., Any]" , op : str ) -> "Callable[..., Any]" :
7576 @wraps (f )
7677 def new_huggingface_task (* args : "Any" , ** kwargs : "Any" ) -> "Any" :
77- integration = sentry_sdk .get_client ().get_integration (HuggingfaceHubIntegration )
78+ client = sentry_sdk .get_client ()
79+ integration = client .get_integration (HuggingfaceHubIntegration )
7880 if integration is None :
7981 return f (* args , ** kwargs )
8082
@@ -91,12 +93,12 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
9193 # invalid call, dont instrument, let it return error
9294 return f (* args , ** kwargs )
9395
94- client = args [0 ]
95- model = client .model or kwargs .get ("model" ) or ""
96+ hf_client = args [0 ]
97+ model = hf_client .model or kwargs .get ("model" ) or ""
9698 operation_name = op .split ("." )[- 1 ]
9799
98100 span : "Union[Span, StreamedSpan]"
99- if has_span_streaming_enabled (sentry_sdk . get_client () .options ):
101+ if has_span_streaming_enabled (client .options ):
100102 span = sentry_sdk .traces .start_span (
101103 name = f"{ operation_name } { model } " ,
102104 attributes = {
@@ -117,14 +119,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
117119 if model :
118120 _set_span_data_attribute (span , SPANDATA .GEN_AI_REQUEST_MODEL , model )
119121
120- # Input attributes
121- if should_send_default_pii () and integration .include_prompts :
122- set_data_normalized (
123- span , SPANDATA .GEN_AI_REQUEST_MESSAGES , prompt , unpack = False
124- )
125-
126122 attribute_mapping = {
127- "tools" : SPANDATA .GEN_AI_REQUEST_AVAILABLE_TOOLS ,
128123 "frequency_penalty" : SPANDATA .GEN_AI_REQUEST_FREQUENCY_PENALTY ,
129124 "max_tokens" : SPANDATA .GEN_AI_REQUEST_MAX_TOKENS ,
130125 "presence_penalty" : SPANDATA .GEN_AI_REQUEST_PRESENCE_PENALTY ,
@@ -134,6 +129,24 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
134129 "stream" : SPANDATA .GEN_AI_RESPONSE_STREAMING ,
135130 }
136131
132+ if has_data_collection_enabled (client .options ):
133+ if client .options ["data_collection" ]["gen_ai" ]["inputs" ]:
134+ attribute_mapping ["tools" ] = SPANDATA .GEN_AI_REQUEST_AVAILABLE_TOOLS
135+ else :
136+ # Legacy behaviour where we unconditionally set this. Remove when data collection is fully rolled out
137+ attribute_mapping ["tools" ] = SPANDATA .GEN_AI_REQUEST_AVAILABLE_TOOLS
138+
139+ # Input attributes
140+ if has_data_collection_enabled (client .options ):
141+ if client .options ["data_collection" ]["gen_ai" ]["inputs" ]:
142+ set_data_normalized (
143+ span , SPANDATA .GEN_AI_REQUEST_MESSAGES , prompt , unpack = False
144+ )
145+ elif should_send_default_pii () and integration .include_prompts :
146+ set_data_normalized (
147+ span , SPANDATA .GEN_AI_REQUEST_MESSAGES , prompt , unpack = False
148+ )
149+
137150 for attribute , span_attribute in attribute_mapping .items ():
138151 value = kwargs .get (attribute , None )
139152 if value is not None :
@@ -210,18 +223,34 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
210223 finish_reason ,
211224 )
212225
213- if should_send_default_pii () and integration .include_prompts :
214- if tool_calls is not None and len (tool_calls ) > 0 :
226+ if tool_calls is not None and len (tool_calls ) > 0 :
227+ if has_data_collection_enabled (client .options ):
228+ if client .options ["data_collection" ]["gen_ai" ]["inputs" ]:
229+ set_data_normalized (
230+ span ,
231+ SPANDATA .GEN_AI_RESPONSE_TOOL_CALLS ,
232+ tool_calls ,
233+ unpack = False ,
234+ )
235+ elif should_send_default_pii () and integration .include_prompts :
215236 set_data_normalized (
216237 span ,
217238 SPANDATA .GEN_AI_RESPONSE_TOOL_CALLS ,
218239 tool_calls ,
219240 unpack = False ,
220241 )
221242
222- if len (response_text_buffer ) > 0 :
223- text_response = "" .join (response_text_buffer )
224- if text_response :
243+ if len (response_text_buffer ) > 0 :
244+ text_response = "" .join (response_text_buffer )
245+ if text_response :
246+ if has_data_collection_enabled (client .options ):
247+ if client .options ["data_collection" ]["gen_ai" ]["outputs" ]:
248+ set_data_normalized (
249+ span ,
250+ SPANDATA .GEN_AI_RESPONSE_TEXT ,
251+ text_response ,
252+ )
253+ elif should_send_default_pii () and integration .include_prompts :
225254 set_data_normalized (
226255 span ,
227256 SPANDATA .GEN_AI_RESPONSE_TEXT ,
@@ -284,7 +313,14 @@ def new_details_iterator() -> "Iterable[Any]":
284313 finish_reason ,
285314 )
286315
287- if should_send_default_pii () and integration .include_prompts :
316+ should_set_response_text = False
317+ if has_data_collection_enabled (client .options ):
318+ if client .options ["data_collection" ]["gen_ai" ]["outputs" ]:
319+ should_set_response_text = True
320+ elif should_send_default_pii () and integration .include_prompts :
321+ should_set_response_text = True
322+
323+ if should_set_response_text :
288324 if len (response_text_buffer ) > 0 :
289325 text_response = "" .join (response_text_buffer )
290326 if text_response :
@@ -363,18 +399,44 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]":
363399 finish_reason ,
364400 )
365401
366- if should_send_default_pii () and integration .include_prompts :
367- if tool_calls is not None and len (tool_calls ) > 0 :
402+ if tool_calls is not None and len (tool_calls ) > 0 :
403+ if has_data_collection_enabled (client .options ):
404+ if client .options ["data_collection" ]["gen_ai" ][
405+ "inputs"
406+ ]:
407+ set_data_normalized (
408+ span ,
409+ SPANDATA .GEN_AI_RESPONSE_TOOL_CALLS ,
410+ tool_calls ,
411+ unpack = False ,
412+ )
413+ elif (
414+ should_send_default_pii ()
415+ and integration .include_prompts
416+ ):
368417 set_data_normalized (
369418 span ,
370419 SPANDATA .GEN_AI_RESPONSE_TOOL_CALLS ,
371420 tool_calls ,
372421 unpack = False ,
373422 )
374423
375- if len (response_text_buffer ) > 0 :
376- text_response = "" .join (response_text_buffer )
377- if text_response :
424+ if len (response_text_buffer ) > 0 :
425+ text_response = "" .join (response_text_buffer )
426+ if text_response :
427+ if has_data_collection_enabled (client .options ):
428+ if client .options ["data_collection" ]["gen_ai" ][
429+ "outputs"
430+ ]:
431+ set_data_normalized (
432+ span ,
433+ SPANDATA .GEN_AI_RESPONSE_TEXT ,
434+ text_response ,
435+ )
436+ elif (
437+ should_send_default_pii ()
438+ and integration .include_prompts
439+ ):
378440 set_data_normalized (
379441 span ,
380442 SPANDATA .GEN_AI_RESPONSE_TEXT ,
0 commit comments