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
64 changes: 42 additions & 22 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
has_data_collection_enabled,
package_version,
reraise,
safe_serialize,
Expand Down Expand Up @@ -390,12 +391,43 @@
)
set_on_span(SPANDATA.GEN_AI_SYSTEM, "anthropic")
set_on_span(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
if (
messages is not None
and len(messages) > 0 # type: ignore
and should_send_default_pii()
and integration.include_prompts
):

if max_tokens is not None and _is_given(max_tokens):
set_on_span(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)
if model is not None and _is_given(model):
set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model)
if temperature is not None and _is_given(temperature):
set_on_span(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature)
if top_k is not None and _is_given(top_k):
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_K, top_k)
if top_p is not None and _is_given(top_p):
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p)

client = sentry_sdk.get_client()

if has_data_collection_enabled(client.options):
if client.options["data_collection"]["gen_ai"]["inputs"]:
if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore
set_on_span(
SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools)
)
else:
# Tools were unconditionally added pre-data collection configuration.
# This can be removed once data collection is fully rolled out
if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore
set_on_span(SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools))

if messages is None or len(messages) == 0: # type: ignore
return

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() and integration.include_prompts:
record_inputs = True

if record_inputs:
if isinstance(system, str) or isinstance(system, Iterable):
set_on_span(
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
Expand Down Expand Up @@ -447,32 +479,20 @@

role_normalized_messages = normalize_message_roles(normalized_messages)

client = sentry_sdk.get_client()
scope = sentry_sdk.get_current_scope()
messages_data = (
truncate_and_annotate_messages(role_normalized_messages, span, scope)
if should_truncate_gen_ai_input(client.options)
else role_normalized_messages
)

Check warning on line 487 in sentry_sdk/integrations/anthropic.py

View check run for this annotation

@sentry/warden / warden: security-review

Anthropic response text/tool calls ignore data_collection gen_ai.outputs opt-out

This change gates prompt (input) collection on `data_collection["gen_ai"]["inputs"]`, but `_set_output_data` (line 609) still records `GEN_AI_RESPONSE_TEXT` and `GEN_AI_RESPONSE_TOOL_CALLS` based solely on the legacy `should_send_default_pii() and integration.include_prompts` check, so a user who sets `_experiments={"data_collection": {"gen_ai": {"outputs": False}}}` while still having `send_default_pii=True` will keep sending LLM response content — which frequently echoes user PII — to Sentry despite the explicit opt-out and despite the SDK warning that `send_default_pii` "is ignored when `data_collection` is set".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anthropic response text/tool calls ignore data_collection gen_ai.outputs opt-out

This change gates prompt (input) collection on data_collection["gen_ai"]["inputs"], but _set_output_data (line 609) still records GEN_AI_RESPONSE_TEXT and GEN_AI_RESPONSE_TOOL_CALLS based solely on the legacy should_send_default_pii() and integration.include_prompts check, so a user who sets _experiments={"data_collection": {"gen_ai": {"outputs": False}}} while still having send_default_pii=True will keep sending LLM response content — which frequently echoes user PII — to Sentry despite the explicit opt-out and despite the SDK warning that send_default_pii "is ignored when data_collection is set".

Evidence
  • _set_common_input_data (this hunk) now resolves record_inputs from client.options["data_collection"]["gen_ai"]["inputs"], falling back to should_send_default_pii() and integration.include_prompts only when has_data_collection_enabled() is false.
  • _set_output_data (line 609) gates GEN_AI_RESPONSE_TEXT/GEN_AI_RESPONSE_TOOL_CALLS only on should_send_default_pii() and integration.include_prompts; it never reads options["data_collection"]["gen_ai"]["outputs"] (confirmed via grep — no data_collection reference in the output path).
  • data_collection._gen_ai_from_value supports an explicit outputs: False, and _resolve_data_collection documents that data_collection wins over send_default_pii, emitting a DeprecationWarning that send_default_pii "is ignored when data_collection is set."
  • With send_default_pii=True plus data_collection={"gen_ai": {"outputs": False}}, the resolved config says outputs are off, yet the legacy check at line 609 still evaluates true and response text is attached to the span and sent to Sentry.

Identified by Warden · security-review · JXZ-LMZ

if messages_data is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
span,
SPANDATA.GEN_AI_REQUEST_MESSAGES,
messages_data,
unpack=False,
)

if max_tokens is not None and _is_given(max_tokens):
set_on_span(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)
if model is not None and _is_given(model):
set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model)
if temperature is not None and _is_given(temperature):
set_on_span(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature)
if top_k is not None and _is_given(top_k):
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_K, top_k)
if top_p is not None and _is_given(top_p):
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p)

if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore
set_on_span(SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools))


def _set_create_input_data(
span: "Union[Span, StreamedSpan]",
Expand Down
Loading
Loading