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
11 changes: 7 additions & 4 deletions sentry_sdk/integrations/pydantic_ai/spans/ai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
_set_agent_data,
_set_available_tools,
_set_model_data,
_should_send_prompts,
_should_send_inputs,
_should_send_outputs,
get_current_agent,
get_is_streaming,
)
Expand Down Expand Up @@ -107,7 +108,7 @@ def _set_input_messages(
span: "Union[sentry_sdk.tracing.Span, StreamedSpan]", messages: "Any"
) -> None:
"""Set input messages data on a span."""
if not _should_send_prompts():
if not _should_send_inputs():
return

if not messages:
Expand Down Expand Up @@ -236,8 +237,7 @@ def _set_output_data(
response: "Optional[ModelResponse]",
) -> None:
"""Set output data on a span."""
if not _should_send_prompts():
return
record_outputs = _should_send_outputs()

if not response:
return
Expand All @@ -247,6 +247,9 @@ def _set_output_data(
)
set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response.model_name) # type: ignore[arg-type]

if not record_outputs:
return

try:
if hasattr(response, "parts"):
parts: "list[Union[_types.TextPart, _types.ReasoningPart, _types.ToolCallPart]]" = []
Expand Down
6 changes: 3 additions & 3 deletions sentry_sdk/integrations/pydantic_ai/spans/execute_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sentry_sdk.utils import safe_serialize

from ..consts import SPAN_ORIGIN
from ..utils import _set_agent_data, _should_send_prompts
from ..utils import _set_agent_data, _should_send_inputs, _should_send_outputs

if TYPE_CHECKING:
from typing import Any, Optional, Union
Expand Down Expand Up @@ -62,7 +62,7 @@ def execute_tool_span(

_set_agent_data(span, agent)

if _should_send_prompts() and tool_args is not None:
if _should_send_inputs() and tool_args is not None:
set_on_span(SPANDATA.GEN_AI_TOOL_INPUT, safe_serialize(tool_args))

return span
Expand All @@ -75,7 +75,7 @@ def update_execute_tool_span(
if not span:
return

if not _should_send_prompts() or result is None:
if not _should_send_outputs() or result is None:
return

if isinstance(span, StreamedSpan):
Expand Down
7 changes: 4 additions & 3 deletions sentry_sdk/integrations/pydantic_ai/spans/invoke_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
_set_agent_data,
_set_available_tools,
_set_model_data,
_should_send_prompts,
_should_send_inputs,
_should_send_outputs,
)
from .utils import (
_serialize_binary_content_item,
Expand Down Expand Up @@ -73,7 +74,7 @@ def invoke_agent_span(
_set_available_tools(span, agent)

# Add user prompt and system prompts if available and prompts are enabled
if _should_send_prompts():
if _should_send_inputs():
messages = []

# Add system prompts (both instructions and system_prompt)
Expand Down Expand Up @@ -163,7 +164,7 @@ def update_invoke_agent_span(
output = getattr(result, "output", None)

# Set response text if prompts are enabled
if _should_send_prompts() and output:
if _should_send_outputs() and output:
set_data_normalized(
span, SPANDATA.GEN_AI_RESPONSE_TEXT, str(output), unpack=False
)
Expand Down
34 changes: 30 additions & 4 deletions sentry_sdk/integrations/pydantic_ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from sentry_sdk.consts import SPANDATA
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.traces import StreamedSpan
from sentry_sdk.utils import event_from_exception, safe_serialize
from sentry_sdk.utils import (
event_from_exception,
has_data_collection_enabled,
safe_serialize,
)

if TYPE_CHECKING:
from typing import Any, Optional, Union
Expand Down Expand Up @@ -49,11 +53,12 @@ def get_is_streaming() -> bool:
return False


def _should_send_prompts() -> bool:
def _should_send_prompts_legacy() -> bool:
"""
Check if prompts should be sent to Sentry.
Check if prompts should be sent to Sentry based on the deprecated
``send_default_pii`` option and the ``include_prompts`` integration setting.

This checks both send_default_pii and the include_prompts integration setting.
TODO: Remove this once `send_default_pii` is deprecated.
"""
if not should_send_default_pii():
return False
Expand All @@ -69,6 +74,22 @@ def _should_send_prompts() -> bool:
return getattr(integration, "include_prompts", False)


def _should_send_inputs() -> bool:
client = sentry_sdk.get_client()
if has_data_collection_enabled(client.options):
return bool(client.options["data_collection"]["gen_ai"]["inputs"])

return _should_send_prompts_legacy()


def _should_send_outputs() -> bool:
client = sentry_sdk.get_client()
if has_data_collection_enabled(client.options):
return bool(client.options["data_collection"]["gen_ai"]["outputs"])

return _should_send_prompts_legacy()


def _set_agent_data(
span: "Union[sentry_sdk.tracing.Span, StreamedSpan]", agent: "Any"
) -> None:
Expand Down Expand Up @@ -191,6 +212,11 @@ def _set_available_tools(
if not agent or not hasattr(agent, "_function_toolset"):
return

client_options = sentry_sdk.get_client().options
if has_data_collection_enabled(client_options):
if not client_options["data_collection"]["gen_ai"]["inputs"]:
return

try:
tools = []
# Get tools from the function toolset
Expand Down
Loading
Loading