From e2ff765460164c8bfd659c2512fc02d5935b3c8c Mon Sep 17 00:00:00 2001 From: Willem Jiang Date: Mon, 20 Oct 2025 23:14:09 +0800 Subject: [PATCH] fix: correct image result format for OpenAI compatibility to fix #632 (#634) - Change image result type from 'image' to 'image_url' to match OpenAI API expectations - Wrap image URL in dict structure: {"url": "..."} instead of plain string - Update SearchResultPostProcessor to handle dict-based image_url during duplicate removal - Update tests to validate new image format This fixes the 400 error: Invalid value: 'image'. Supported values are: 'text', 'image_url'... Co-authored-by: Willem Jiang <143703838+willem-bd@users.noreply.github.com> --- src/tools/search_postprocessor.py | 9 ++++++++- src/tools/tavily_search/tavily_search_api_wrapper.py | 4 ++-- tests/unit/tools/test_tavily_search_api_wrapper.py | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/tools/search_postprocessor.py b/src/tools/search_postprocessor.py index ddf9b82..8f9813a 100644 --- a/src/tools/search_postprocessor.py +++ b/src/tools/search_postprocessor.py @@ -201,7 +201,14 @@ class SearchResultPostProcessor: def _remove_duplicates(self, result: Dict, seen_urls: set) -> Dict: """Remove duplicate results""" - url = result.get("url", result.get("image_url", "")) + url = result.get("url") + if not url: + image_url_val = result.get("image_url", "") + if isinstance(image_url_val, dict): + url = image_url_val.get("url", "") + else: + url = image_url_val + if url and url not in seen_urls: seen_urls.add(url) return result.copy() # Return a copy to avoid modifying original diff --git a/src/tools/tavily_search/tavily_search_api_wrapper.py b/src/tools/tavily_search/tavily_search_api_wrapper.py index ae90736..bcaa78a 100644 --- a/src/tools/tavily_search/tavily_search_api_wrapper.py +++ b/src/tools/tavily_search/tavily_search_api_wrapper.py @@ -114,8 +114,8 @@ class EnhancedTavilySearchAPIWrapper(OriginalTavilySearchAPIWrapper): images = raw_results["images"] for image in images: clean_result = { - "type": "image", - "image_url": image["url"], + "type": "image_url", + "image_url": {"url": image["url"]}, "image_description": image["description"], } clean_results.append(clean_result) diff --git a/tests/unit/tools/test_tavily_search_api_wrapper.py b/tests/unit/tools/test_tavily_search_api_wrapper.py index 5481eaa..d215881 100644 --- a/tests/unit/tools/test_tavily_search_api_wrapper.py +++ b/tests/unit/tools/test_tavily_search_api_wrapper.py @@ -166,8 +166,8 @@ class TestEnhancedTavilySearchAPIWrapper: # Test image result image_result = result[1] - assert image_result["type"] == "image" - assert image_result["image_url"] == "https://example.com/image.jpg" + assert image_result["type"] == "image_url" + assert image_result["image_url"] == {"url": "https://example.com/image.jpg"} assert image_result["image_description"] == "Test image description" def test_clean_results_without_raw_content(self, wrapper):