Skip to content

fix: prevent AttributeError when optional fields are None in web_search/file_search tools#1402

Open
Oxygen56 wants to merge 1 commit into
AgentOps-AI:mainfrom
Oxygen56:fix/issue-1285-none-type-file-search-web-search
Open

fix: prevent AttributeError when optional fields are None in web_search/file_search tools#1402
Oxygen56 wants to merge 1 commit into
AgentOps-AI:mainfrom
Oxygen56:fix/issue-1285-none-type-file-search-web-search

Conversation

@Oxygen56
Copy link
Copy Markdown

Description

Fixes #1285

When using the OpenAI Agents SDK with file search or web search tools, on_span_end raises:

AttributeError: 'NoneType' object has no attribute '__dict__'

Root Cause

In get_response_tool_web_search_attributes and get_response_tool_file_search_attributes, hasattr() guards check for the presence of optional fields (user_location, filters, ranking_options), but hasattr() returns True even when the attribute value is None. Accessing .__dict__ on None then raises AttributeError.

Fix

Added and <attr> is not None checks alongside each hasattr() guard:

  • get_response_tool_web_search_attributes: tool.user_location
  • get_response_tool_file_search_attributes: tool.filters, tool.ranking_options

Testing

Added two new test cases verifying that web search and file search tools work correctly when optional fields are None. All 15 tests pass.

  • test_get_response_tool_web_search_attributes_with_none_user_location
  • test_get_response_tool_file_search_attributes_with_none_filters_and_ranking

…earch tools

Add 'is not None' checks alongside hasattr() guards in
get_response_tool_web_search_attributes and get_response_tool_file_search_attributes.

The hasattr() check returns True even when optional attributes
(user_location, filters, ranking_options) are None, causing
AttributeError when accessing .__dict__ on None.

Fixes: AgentOps-AI#1285

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copy link
Copy Markdown

@biswajeetdev biswajeetdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean fix — the AttributeError happens because the OpenAI SDK marks user_location, filters, and ranking_options as optional in the type stubs, but the instrumentation code was accessing them unconditionally.

One thing to double-check on the web_search side: the fix uses hasattr(tool, "search_context_size") to guard the access. On Pydantic models and SDK dataclasses, hasattr always returns True for declared fields (the attribute exists, it is just None). So the actual guard needed is if tool.search_context_size is not None — which the code likely already has elsewhere. Worth a quick scan to make sure hasattr is not being used as a None check anywhere in this diff, since on Pydantic models it will always pass.

Tests cover the key cases (None values for optional fields, missing fields) and assert the function does not raise — good regression anchors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: AttributeError: 'NoneType' object has no attribute '__dict__' in get_response_tool_web/file_search_attributes

2 participants