Skip to content

Commit d03110e

Browse files
authored
feat(anthropic): Gate prompt collection on data_collection option (#7054)
Replace include_prompts and send_default_pii checks with the new data_collection configuration for controlling whether messages and system instructions are captured. Maintain backwards compatibility: when data_collection is not configured, fall back to the legacy pii/include_prompts behavior. Tools are always collected regardless of the message collection setting. Refs PY-2588
1 parent c21dd48 commit d03110e

2 files changed

Lines changed: 492 additions & 22 deletions

File tree

sentry_sdk/integrations/anthropic.py

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from sentry_sdk.utils import (
2727
capture_internal_exceptions,
2828
event_from_exception,
29+
has_data_collection_enabled,
2930
package_version,
3031
reraise,
3132
safe_serialize,
@@ -390,12 +391,43 @@ def _set_common_input_data(
390391
)
391392
set_on_span(SPANDATA.GEN_AI_SYSTEM, "anthropic")
392393
set_on_span(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
393-
if (
394-
messages is not None
395-
and len(messages) > 0 # type: ignore
396-
and should_send_default_pii()
397-
and integration.include_prompts
398-
):
394+
395+
if max_tokens is not None and _is_given(max_tokens):
396+
set_on_span(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)
397+
if model is not None and _is_given(model):
398+
set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model)
399+
if temperature is not None and _is_given(temperature):
400+
set_on_span(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature)
401+
if top_k is not None and _is_given(top_k):
402+
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_K, top_k)
403+
if top_p is not None and _is_given(top_p):
404+
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p)
405+
406+
client = sentry_sdk.get_client()
407+
408+
if has_data_collection_enabled(client.options):
409+
if client.options["data_collection"]["gen_ai"]["inputs"]:
410+
if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore
411+
set_on_span(
412+
SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools)
413+
)
414+
else:
415+
# Tools were unconditionally added pre-data collection configuration.
416+
# This can be removed once data collection is fully rolled out
417+
if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore
418+
set_on_span(SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools))
419+
420+
if messages is None or len(messages) == 0: # type: ignore
421+
return
422+
423+
record_inputs = False
424+
if has_data_collection_enabled(client.options):
425+
if client.options["data_collection"]["gen_ai"]["inputs"]:
426+
record_inputs = True
427+
elif should_send_default_pii() and integration.include_prompts:
428+
record_inputs = True
429+
430+
if record_inputs:
399431
if isinstance(system, str) or isinstance(system, Iterable):
400432
set_on_span(
401433
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
@@ -447,7 +479,6 @@ def _set_common_input_data(
447479

448480
role_normalized_messages = normalize_message_roles(normalized_messages)
449481

450-
client = sentry_sdk.get_client()
451482
scope = sentry_sdk.get_current_scope()
452483
messages_data = (
453484
truncate_and_annotate_messages(role_normalized_messages, span, scope)
@@ -456,23 +487,12 @@ def _set_common_input_data(
456487
)
457488
if messages_data is not None:
458489
set_data_normalized(
459-
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
490+
span,
491+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
492+
messages_data,
493+
unpack=False,
460494
)
461495

462-
if max_tokens is not None and _is_given(max_tokens):
463-
set_on_span(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)
464-
if model is not None and _is_given(model):
465-
set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model)
466-
if temperature is not None and _is_given(temperature):
467-
set_on_span(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature)
468-
if top_k is not None and _is_given(top_k):
469-
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_K, top_k)
470-
if top_p is not None and _is_given(top_p):
471-
set_on_span(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p)
472-
473-
if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore
474-
set_on_span(SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools))
475-
476496

477497
def _set_create_input_data(
478498
span: "Union[Span, StreamedSpan]",

0 commit comments

Comments
 (0)