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
20 changes: 2 additions & 18 deletions sentry_sdk/integrations/google_genai/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import sentry_sdk
from sentry_sdk.ai.utils import set_data_normalized
from sentry_sdk.consts import SPANDATA
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.traces import Span
from sentry_sdk.utils import (
has_data_collection_enabled,
safe_serialize,
)

Expand Down Expand Up @@ -154,28 +152,14 @@ def set_span_data_for_streaming_response(
)

if accumulated_response.get("tool_calls"):
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["outputs"]:
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
safe_serialize(accumulated_response["tool_calls"]),
)
else:
# Before data collection was introduced this was unconditionally set
if client.options["data_collection"]["gen_ai"]["outputs"]:
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
safe_serialize(accumulated_response["tool_calls"]),
)

if accumulated_response.get("text"):
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["outputs"]:
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TEXT,
safe_serialize([accumulated_response["text"]]),
)

elif should_send_default_pii():
if client.options["data_collection"]["gen_ai"]["outputs"]:
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TEXT,
safe_serialize([accumulated_response["text"]]),
Expand Down
46 changes: 5 additions & 41 deletions sentry_sdk/integrations/google_genai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
transform_google_content_part,
)
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
has_data_collection_enabled,
safe_serialize,
)

Expand Down Expand Up @@ -919,16 +917,7 @@ def set_span_data_for_request(
if tools:
formatted_tools = _format_tools_for_span(tools)
if formatted_tools:
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["inputs"]:
set_data_normalized(
span,
SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS,
formatted_tools,
unpack=False,
)
else:
# To remove once data collection has been fully rolled out
if client.options["data_collection"]["gen_ai"]["inputs"]:
set_data_normalized(
span,
SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS,
Expand All @@ -937,10 +926,7 @@ def set_span_data_for_request(
)

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

if record_inputs:
Expand Down Expand Up @@ -1043,29 +1029,14 @@ def set_span_data_for_response(

tool_calls = extract_tool_calls(response)
if tool_calls:
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["outputs"]:
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, safe_serialize(tool_calls)
)
else:
# Before data collection was introduced, this was set unconditionally
if client.options["data_collection"]["gen_ai"]["outputs"]:
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, safe_serialize(tool_calls)
)

if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["outputs"]:
response_texts = _extract_response_text(response)
if response_texts:
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TEXT, safe_serialize(response_texts)
)
elif should_send_default_pii():
# TODO: Delete this block once data collection has been completely rolled out
if client.options["data_collection"]["gen_ai"]["outputs"]:
response_texts = _extract_response_text(response)
if response_texts:
# Format as JSON string array as per documentation
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TEXT, safe_serialize(response_texts)
)
Expand Down Expand Up @@ -1111,14 +1082,7 @@ def set_span_data_for_embed_request(
"""Set span data for embedding request."""
client = sentry_sdk.get_client()

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

if record_inputs:
if client.options["data_collection"]["gen_ai"]["inputs"]:
if contents:
# For embeddings, contents is typically a list of strings/texts
input_texts = []
Expand Down
Loading
Loading