Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 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

Check failure on line 11 in sentry_sdk/integrations/openai_agents/patches/agent_run.py

View check run for this annotation

@sentry/warden / warden: code-review

set_span_errored called with wrong number of arguments causing TypeError at runtime

The import changed from `_record_exception_on_span` to `set_span_errored`, but `set_span_errored` in `tracing_utils.py` only accepts one optional parameter (`span`). However, line 160 in this file calls `set_span_errored(span, exc)` with two arguments. This will cause a `TypeError: set_span_errored() takes at most 1 argument (2 given)` when an exception is raised in `_run_single_turn_streamed`.

Check failure on line 11 in sentry_sdk/integrations/openai_agents/patches/agent_run.py

View check run for this annotation

@sentry/warden / warden: find-bugs

set_span_errored called with incompatible signature causes TypeError

The import change replaces `_record_exception_on_span` with `set_span_errored`, but `set_span_errored(span, exc)` is called at line 160 with two arguments while the function only accepts one parameter (`span`). This will raise a `TypeError: set_span_errored() takes 1 positional argument but 2 were given` at runtime when an exception occurs during streaming.
Comment thread
alexander-alderman-webb marked this conversation as resolved.

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -99,9 +99,9 @@

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 @@ -157,7 +157,7 @@
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, exc)

Check failure on line 160 in sentry_sdk/integrations/openai_agents/patches/agent_run.py

View check run for this annotation

@sentry/warden / warden: code-review

[CVU-UDU] set_span_errored called with wrong number of arguments causing TypeError at runtime (additional location)

The import changed from `_record_exception_on_span` to `set_span_errored`, but `set_span_errored` in `tracing_utils.py` only accepts one optional parameter (`span`). However, line 160 in this file calls `set_span_errored(span, exc)` with two arguments. This will cause a `TypeError: set_span_errored() takes at most 1 argument (2 given)` when an exception is raised in `_run_single_turn_streamed`.

Check failure on line 160 in sentry_sdk/integrations/openai_agents/patches/agent_run.py

View check run for this annotation

@sentry/warden / warden: find-bugs

[EXU-XWG] set_span_errored called with incompatible signature causes TypeError (additional location)

The import change replaces `_record_exception_on_span` with `set_span_errored`, but `set_span_errored(span, exc)` is called at line 160 with two arguments while the function only accepts one parameter (`span`). This will raise a `TypeError: set_span_errored() takes 1 positional argument but 2 were given` at runtime when an exception occurs during streaming.
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