Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 24 additions & 74 deletions sentry_sdk/integrations/huggingface_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
from sentry_sdk.ai.utils import set_data_normalized
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
has_data_collection_enabled,
parse_version,
reraise,
)
Expand Down Expand Up @@ -113,20 +111,8 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
"stream": SPANDATA.GEN_AI_RESPONSE_STREAMING,
}

if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["inputs"]:
attribute_mapping["tools"] = SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS
else:
# Legacy behaviour where we unconditionally set this. Remove when data collection is fully rolled out
if client.options["data_collection"]["gen_ai"]["inputs"]:
attribute_mapping["tools"] = SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS

# Input attributes
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["inputs"]:
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompt, unpack=False
)
elif should_send_default_pii():
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompt, unpack=False
)
Expand Down Expand Up @@ -206,15 +192,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
)

if tool_calls is not None and len(tool_calls) > 0:
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["outputs"]:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
tool_calls,
unpack=False,
)
elif should_send_default_pii():
if client.options["data_collection"]["gen_ai"]["outputs"]:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
Expand All @@ -225,14 +203,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
if len(response_text_buffer) > 0:
text_response = "".join(response_text_buffer)
if text_response:
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["outputs"]:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TEXT,
text_response,
)
elif should_send_default_pii():
if client.options["data_collection"]["gen_ai"]["outputs"]:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TEXT,
Expand Down Expand Up @@ -295,14 +266,7 @@ def new_details_iterator() -> "Iterable[Any]":
finish_reason,
)

should_set_response_text = False
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["outputs"]:
should_set_response_text = True
elif should_send_default_pii():
should_set_response_text = True

if should_set_response_text:
if client.options["data_collection"]["gen_ai"]["outputs"]:
if len(response_text_buffer) > 0:
text_response = "".join(response_text_buffer)
if text_response:
Expand Down Expand Up @@ -381,43 +345,29 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]":
finish_reason,
)

if tool_calls is not None and len(tool_calls) > 0:
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"][
"outputs"
]:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
tool_calls,
unpack=False,
)
elif should_send_default_pii():
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
tool_calls,
unpack=False,
)
if (
tool_calls is not None
and len(tool_calls) > 0
and client.options["data_collection"]["gen_ai"]["outputs"]
):
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
tool_calls,
unpack=False,
)

if len(response_text_buffer) > 0:
if (
len(response_text_buffer) > 0
and client.options["data_collection"]["gen_ai"]["outputs"]
):
text_response = "".join(response_text_buffer)
if text_response:
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"][
"outputs"
]:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TEXT,
text_response,
)
elif should_send_default_pii():
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TEXT,
text_response,
)
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TEXT,
text_response,
)

if usage is not None:
record_token_usage(
Expand Down
Loading
Loading