fix(openai-agents): Patch tool functions following library refactor#5445
fix(openai-agents): Patch tool functions following library refactor#5445alexander-alderman-webb merged 6 commits intomasterfrom
Conversation
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. Bug Fixes 🐛
Documentation 📚
Internal Changes 🔧
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 6.00s All tests are passing successfully. ❌ Patch coverage is 5.00%. Project has 13668 uncovered lines. Files with missing lines (180)
Generated by Codecov Action |
|
|
||
|
|
||
| def _patch_run_get_all_tools() -> None: | ||
| agents.run.get_all_tools = _create_run_loop_get_all_tools_wrapper( |
There was a problem hiding this comment.
we're creating a lot of random indirection and cognitive overhead with these wrappers. I would just move the patching logic here in this function and remove those wrappers and just keep the _get_all_tools in the tools.py file.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| @staticmethod | ||
| def setup_once() -> None: | ||
| _patch_error_tracing() | ||
| _patch_tools() |
There was a problem hiding this comment.
let's keep _patch_tools for the isolation and add the tools stuff in there
There was a problem hiding this comment.
I want to avoid having the same if condition in all the patch_* functions that boils down to checking the version. Could we consider breaking into _patch_run_get_all_tools() and _patch_agent_runner_get_all_tools(), or do we want different names for these functions that are not based on the location+name of what we're patching?
There was a problem hiding this comment.
ok then keep this for now, we'll look at the shape of the __init__ file at the end, and we remove all the wrappers first
| @wraps(run_loop.get_all_tools) | ||
| async def new_wrapped_get_all_tools( | ||
| agent: "agents.Agent", | ||
| context_wrapper: "agents.RunContextWrapper", | ||
| ) -> "list[agents.Tool]": | ||
| return await _get_all_tools( | ||
| run_loop.get_all_tools, agent, context_wrapper | ||
| ) | ||
|
|
||
| agents.run.get_all_tools = new_wrapped_get_all_tools |
There was a problem hiding this comment.
Bug: The code accesses run_loop.get_all_tools after a version check, but doesn't verify that run_loop isn't None from a failed import, which can cause an AttributeError.
Severity: CRITICAL
Suggested Fix
Add a if run_loop is not None: check after the version check and before the code block that attempts to access run_loop.get_all_tools to ensure the object exists before it is used.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: sentry_sdk/integrations/openai_agents/__init__.py#L102-L111
Potential issue: In the OpenAI Agents integration, the code checks if the library
version is `0.8` or higher and, if so, attempts to access `run_loop.get_all_tools`.
However, the `run_loop` object is imported within a `try...except` block that sets
`run_loop = None` if the import fails. The code does not check if `run_loop` is `None`
after the version check but before accessing its attributes. This can lead to an
`AttributeError: 'NoneType' object has no attribute 'get_all_tools'` if the import fails
on a supported version, for instance, due to a corrupted installation. This error occurs
during SDK initialization and can prevent the application from starting.
Did we get this right? 👍 / 👎 to inform future reviews.
Description
The
AgentRunner._get_all_tools()class method has been moved to the functionagents.run_internal.turn_preparation.get_all_tools().Patch the new function by re-using existing wrapper logic.
Issues
Reminders
tox -e linters.feat:,fix:,ref:,meta:)