fix: prevent AttributeError when optional fields are None in web_search/file_search tools#1402
Conversation
…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>
biswajeetdev
left a comment
There was a problem hiding this comment.
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.
Description
Fixes #1285
When using the OpenAI Agents SDK with file search or web search tools,
on_span_endraises:Root Cause
In
get_response_tool_web_search_attributesandget_response_tool_file_search_attributes,hasattr()guards check for the presence of optional fields (user_location,filters,ranking_options), buthasattr()returnsTrueeven when the attribute value isNone. Accessing.__dict__onNonethen raisesAttributeError.Fix
Added
and <attr> is not Nonechecks alongside eachhasattr()guard:get_response_tool_web_search_attributes:tool.user_locationget_response_tool_file_search_attributes:tool.filters,tool.ranking_optionsTesting
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_locationtest_get_response_tool_file_search_attributes_with_none_filters_and_ranking