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
10 changes: 5 additions & 5 deletions sentry_sdk/integrations/openai_agents/patches/agent_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end_invoke_agent_span,
handoff_span,
)
from ..utils import _record_exception_on_span
from sentry_sdk.tracing_utils import set_span_errored
Comment thread
alexander-alderman-webb marked this conversation as resolved.

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -99,9 +99,9 @@ async def _run_single_turn(

try:
result = await original_run_single_turn(*args, **kwargs)
except Exception as exc:
except Exception:
if span is not None and span.timestamp is None:
_record_exception_on_span(span, exc)
set_span_errored(span)
end_invoke_agent_span(context_wrapper, agent)
reraise(*sys.exc_info())

Expand Down Expand Up @@ -153,11 +153,11 @@ async def _run_single_turn_streamed(

try:
result = await original_run_single_turn_streamed(*args, **kwargs)
except Exception as exc:
except Exception:
exc_info = sys.exc_info()
with capture_internal_exceptions():
if span is not None and span.timestamp is None:
_record_exception_on_span(span, exc)
set_span_errored(span)
end_invoke_agent_span(context_wrapper, agent)
_close_streaming_workflow_span(agent)
reraise(*exc_info)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import wraps

import sentry_sdk
from ..utils import _record_exception_on_span
from sentry_sdk.tracing_utils import set_span_errored

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -57,7 +57,7 @@ def sentry_attach_error_to_current_span(
# Set the current Sentry span to errored
current_span = sentry_sdk.get_current_span()
if current_span is not None:
_record_exception_on_span(current_span, error)
set_span_errored(current_span)

# Call the original function
return original_attach_error(error, *args, **kwargs)
Expand Down
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/openai_agents/patches/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.utils import capture_internal_exceptions, reraise
from sentry_sdk.tracing_utils import set_span_errored

from ..spans import agent_workflow_span, end_invoke_agent_span
from ..utils import _capture_exception, _record_exception_on_span
from ..utils import _capture_exception

try:
from agents.exceptions import AgentsException
Expand Down Expand Up @@ -65,7 +66,7 @@ async def wrapper(*args: "Any", **kwargs: "Any") -> "Any":
invoke_agent_span is not None
and invoke_agent_span.timestamp is None
):
_record_exception_on_span(invoke_agent_span, exc)
set_span_errored(invoke_agent_span)
end_invoke_agent_span(context_wrapper, agent)
reraise(*exc_info)
except Exception as exc:
Expand Down
24 changes: 1 addition & 23 deletions sentry_sdk/integrations/openai_agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from sentry_sdk.consts import SPANDATA, SPANSTATUS, OP
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.tracing import Span
from sentry_sdk.tracing_utils import set_span_errored
from sentry_sdk.utils import event_from_exception, safe_serialize
from sentry_sdk.ai._openai_completions_api import _transform_system_instructions
Expand All @@ -23,10 +22,9 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any, Union
from typing import Any
from agents import Usage, TResponseInputItem

from sentry_sdk.traces import StreamedSpan
from sentry_sdk._types import TextPart

try:
Expand All @@ -47,26 +45,6 @@ def _capture_exception(exc: "Any") -> None:
sentry_sdk.capture_event(event, hint=hint)


def _record_exception_on_span(
span: "Union[Span, StreamedSpan]", error: Exception
) -> "Any":
set_span_errored(span)

if not isinstance(span, Span):
# TODO[span-first]: make this work with streamedspans
return

span.set_data("span.status", "error")

# Optionally capture the error details if we have them
if hasattr(error, "__class__"):
span.set_data("error.type", error.__class__.__name__)
if hasattr(error, "__str__"):
error_message = str(error)
if error_message:
span.set_data("error.message", error_message)


def _set_agent_data(span: "sentry_sdk.tracing.Span", agent: "agents.Agent") -> None:
span.set_data(
SPANDATA.GEN_AI_SYSTEM, "openai"
Expand Down
Loading