-
Notifications
You must be signed in to change notification settings - Fork 569
fix(integrations): hooking into error tracing function to find out if an execute tool span should be set to error #4986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
obostjancic
merged 5 commits into
master
from
constantinius/fix/integrations/openai-agents-execute-tool-error-reporting
Oct 29, 2025
+187
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8563507
fix(integrations): hooking into error tracing function to find out if…
constantinius 804c739
Merge branch 'master' into constantinius/fix/integrations/openai-agen…
constantinius 8866198
Fix import statement for agents._utils
obostjancic 7bbad54
Merge branch 'master' into constantinius/fix/integrations/openai-agen…
obostjancic 2b53bb6
set_span_errored
obostjancic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
sentry_sdk/integrations/openai_agents/patches/error_tracing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| from functools import wraps | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.consts import SPANSTATUS | ||
| from sentry_sdk.tracing_utils import set_span_errored | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Callable, Optional | ||
|
|
||
|
|
||
| def _patch_error_tracing(): | ||
| # type: () -> None | ||
| """ | ||
| Patches agents error tracing function to inject our span error logic | ||
| when a tool execution fails. | ||
|
|
||
| In newer versions, the function is at: agents.util._error_tracing.attach_error_to_current_span | ||
| In older versions, it was at: agents._utils.attach_error_to_current_span | ||
|
|
||
| This works even when the module or function doesn't exist. | ||
| """ | ||
| error_tracing_module = None | ||
|
|
||
| # Try newer location first (agents.util._error_tracing) | ||
| try: | ||
| from agents.util import _error_tracing | ||
|
|
||
| error_tracing_module = _error_tracing | ||
| except (ImportError, AttributeError): | ||
| pass | ||
|
|
||
| # Try older location (agents._utils) | ||
| if error_tracing_module is None: | ||
| try: | ||
| import agents._utils | ||
|
|
||
| error_tracing_module = agents._utils | ||
| except (ImportError, AttributeError): | ||
| # Module doesn't exist in either location, nothing to patch | ||
| return | ||
|
|
||
| # Check if the function exists | ||
| if not hasattr(error_tracing_module, "attach_error_to_current_span"): | ||
| return | ||
|
|
||
| original_attach_error = error_tracing_module.attach_error_to_current_span | ||
|
|
||
| @wraps(original_attach_error) | ||
| def sentry_attach_error_to_current_span(error, *args, **kwargs): | ||
| # type: (Any, *Any, **Any) -> Any | ||
| """ | ||
| Wraps agents' error attachment to also set Sentry span status to error. | ||
| This allows us to properly track tool execution errors even though | ||
| the agents library swallows exceptions. | ||
| """ | ||
| # Set the current Sentry span to errored | ||
| current_span = sentry_sdk.get_current_span() | ||
| if current_span is not None: | ||
| set_span_errored(current_span) | ||
| current_span.set_data("span.status", "error") | ||
|
|
||
| # Optionally capture the error details if we have them | ||
| if hasattr(error, "__class__"): | ||
| current_span.set_data("error.type", error.__class__.__name__) | ||
| if hasattr(error, "__str__"): | ||
| error_message = str(error) | ||
| if error_message: | ||
| current_span.set_data("error.message", error_message) | ||
|
|
||
| # Call the original function | ||
| return original_attach_error(error, *args, **kwargs) | ||
|
|
||
| error_tracing_module.attach_error_to_current_span = ( | ||
| sentry_attach_error_to_current_span | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Redundant Status Setting Causes Conflicts
The
set_data("span.status", "error")call is redundant withset_status(SPANSTATUS.ERROR)and incorrectly attempts to set the span status. Thisset_datausage is not the proper Sentry SDK API, which can lead to conflicting status representations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Product is querying for
span.statuseverywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is setting
current_span.set_status(SPANSTATUS.ERROR)not enough?Normally we would use
set_span_errored()fromsentry_sdk.tracing_utilsfor this. Do we need to setspan.statusmanually in other integrations as well, or can we use the helper here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also note
set_span_errored()has additional logic not present here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So in general, the product queries for
span.statusand notstatusso we need that to be present for this to be displayed as an error. Yes i think we should also do it in the other integrations, or overall (whether in the SDK or somewhere during ingestion)I also added
set_span_erroredcall here