Skip to content

Commit 1a427db

Browse files
fix(tools): require image results when WebSearchTool image_settings is set
image_settings configures results the request only asks for when search_content_types includes "image"; the constructor now refuses the pair without it, the same way ShellTool refuses an executor for a hosted environment.
1 parent a86aa57 commit 1a427db

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎src/agents/tool.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ class WebSearchTool:
835835
"""
836836

837837
image_settings: WebSearchToolImageSettings | None = None
838-
"""Settings for image results when `search_content_types` includes `"image"`."""
838+
"""Settings for image results; requires `search_content_types` to include `"image"`."""
839839

840840
if TYPE_CHECKING:
841841

@@ -854,6 +854,12 @@ def __post_init__(self) -> None:
854854
self.filters = coerce_pydantic_config(
855855
self.filters, WebSearchToolFilters, parameter_name="web search filters"
856856
)
857+
if self.image_settings is not None and (
858+
self.search_content_types is None or "image" not in self.search_content_types
859+
):
860+
raise UserError(
861+
'WebSearchTool image_settings requires search_content_types to include "image".'
862+
)
857863

858864
@property
859865
def name(self):

‎tests/models/test_openai_responses_converter.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
one `ComputerTool`.
2424
"""
2525

26-
from typing import Any, cast
26+
from typing import Any, Literal, cast
2727

2828
import pytest
2929
from openai import omit
@@ -517,6 +517,19 @@ def test_convert_tools_includes_web_search_content_types_and_image_settings() ->
517517
]
518518

519519

520+
@pytest.mark.parametrize("search_content_types", [None, ["text"]])
521+
def test_web_search_tool_image_settings_require_image_results(
522+
search_content_types: list[Literal["text", "image"]] | None,
523+
) -> None:
524+
# Image settings without image results would send settings for results the
525+
# request never asks for, so the constructor refuses the pair.
526+
with pytest.raises(UserError, match='search_content_types to include "image"'):
527+
WebSearchTool(
528+
search_content_types=search_content_types,
529+
image_settings={"max_results": 3},
530+
)
531+
532+
520533
def test_convert_tools_text_only_content_types_adds_no_include() -> None:
521534
web_tool = WebSearchTool(search_content_types=["text"])
522535

0 commit comments

Comments
 (0)