From 4a847ac936a02f8660fad6d987208bb77d37e65c Mon Sep 17 00:00:00 2001 From: Waqas Ahmed Date: Fri, 2 Oct 2026 14:15:15 -0700 Subject: [PATCH] fix(exa): fall back to highlights/summary when text is missing (#42213) * fix(exa): fall back to highlights/summary when text is missing (#36905) * test: type exa search transformation test helpers * test(exa): drop type: ignore in test helper and trim redundant comments * test(exa): move snippet fallback test to tests/unit and parametrize it Uses a real httpx.Response instead of a Mock and annotates snippet as Final. * fix(exa): inline snippet fallback since Final is not allowed inside a loop --------- Co-authored-by: ryan-crabbe-berri --- litellm/llms/exa_ai/search/transformation.py | 7 +++- tests/unit/llms/exa_ai/__init__.py | 0 tests/unit/llms/exa_ai/search/__init__.py | 0 .../llms/exa_ai/search/test_transformation.py | 33 +++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/unit/llms/exa_ai/__init__.py create mode 100644 tests/unit/llms/exa_ai/search/__init__.py create mode 100644 tests/unit/llms/exa_ai/search/test_transformation.py diff --git a/litellm/llms/exa_ai/search/transformation.py b/litellm/llms/exa_ai/search/transformation.py index 022622d7af7..cd904e493f2 100644 --- a/litellm/llms/exa_ai/search/transformation.py +++ b/litellm/llms/exa_ai/search/transformation.py @@ -165,6 +165,8 @@ class ExaAISearchConfig(BaseSearchConfig): - results[].title → SearchResult.title - results[].url → SearchResult.url - results[].text → SearchResult.snippet + - results[].highlights → SearchResult.snippet (fallback when "text" is absent) + - results[].summary → SearchResult.snippet (fallback when "text" and "highlights" are absent) - results[].publishedDate → SearchResult.date - No last_updated field in Exa AI response (set to None) @@ -183,7 +185,10 @@ class ExaAISearchConfig(BaseSearchConfig): search_result = SearchResult( title=result.get("title", ""), url=result.get("url", ""), - snippet=result.get("text", ""), # Exa AI uses "text" for content + snippet=result.get("text") + or "\n\n".join(result.get("highlights") or []) + or result.get("summary") + or "", date=result.get("publishedDate"), # ISO 8601 datetime string last_updated=None, # Exa AI doesn't provide last_updated in response ) diff --git a/tests/unit/llms/exa_ai/__init__.py b/tests/unit/llms/exa_ai/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/exa_ai/search/__init__.py b/tests/unit/llms/exa_ai/search/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/exa_ai/search/test_transformation.py b/tests/unit/llms/exa_ai/search/test_transformation.py new file mode 100644 index 00000000000..5e5eb24f23b --- /dev/null +++ b/tests/unit/llms/exa_ai/search/test_transformation.py @@ -0,0 +1,33 @@ +from typing import Final +from unittest.mock import Mock + +import httpx +import pytest + +from litellm.llms.exa_ai.search.transformation import ExaAISearchConfig + + +@pytest.mark.parametrize( + ("content_fields", "expected_snippet"), + [ + ({"text": "full text"}, "full text"), + ({"highlights": ["first highlight", "second highlight"]}, "first highlight\n\nsecond highlight"), + ({"summary": "a summary"}, "a summary"), + ({"text": "full text", "highlights": ["a highlight"], "summary": "a summary"}, "full text"), + ({"highlights": ["a highlight"], "summary": "a summary"}, "a highlight"), + ({"text": "", "highlights": ["a highlight"]}, "a highlight"), + ({"highlights": [], "summary": "a summary"}, "a summary"), + ({}, ""), + ], +) +def test_transform_search_response_snippet_falls_back_through_content_modes( + content_fields: dict[str, str | list[str]], expected_snippet: str +): + raw_response: Final = httpx.Response( + 200, + json={"results": [{"title": "Title", "url": "https://example.com", **content_fields}]}, + ) + + response: Final = ExaAISearchConfig().transform_search_response(raw_response, logging_obj=Mock()) + + assert response.results[0].snippet == expected_snippet