fix(file_search): address latest greptile feedback

Strip internal logging ids from emulated sub-calls, dedupe included search_results by file_id, clean unused imports, and add unit coverage for dedupe behavior.

Made-with: Cursor
This commit is contained in:
Sameer Kankute 2026-03-17 15:33:11 +05:30
parent 77a5093ce2
commit 5692db8123
3 changed files with 31 additions and 5 deletions

View file

@ -14,9 +14,7 @@ Flow:
import json
import time
import uuid
from typing import Any, Dict, Iterable, List, Literal, Optional, Tuple, Union, cast
import httpx
from typing import Any, Dict, Iterable, List, Optional, Tuple, cast
from litellm._logging import verbose_logger
from litellm.types.llms.openai import ResponsesAPIResponse
@ -220,7 +218,13 @@ def _build_search_results_for_include(
file_search_call.search_results (mirrors OpenAI's include= format).
"""
formatted: List[Dict[str, Any]] = []
seen_file_ids: set = set()
for result in results:
file_id = _get_field(result, "file_id") or ""
if file_id and file_id in seen_file_ids:
continue
if file_id:
seen_file_ids.add(file_id)
content_items = _get_field(result, "content") or []
text_chunks = [
c.get("text", "") if isinstance(c, dict) else getattr(c, "text", "")
@ -229,7 +233,7 @@ def _build_search_results_for_include(
text = " ".join(t for t in text_chunks if t)
formatted.append(
{
"file_id": _get_field(result, "file_id") or "",
"file_id": file_id,
"filename": _get_field(result, "filename") or "",
"score": _get_field(result, "score"),
"text": text,

View file

@ -732,6 +732,7 @@ def responses(
aresponses_with_emulated_file_search,
)
_internal_skip = {"litellm_logging_obj", "litellm_call_id", "aresponses"}
emulated_kwargs = {
"include": include,
"instructions": instructions,
@ -759,7 +760,7 @@ def responses(
"extra_body": extra_body,
"timeout": timeout,
"custom_llm_provider": custom_llm_provider,
**kwargs,
**{k: v for k, v in kwargs.items() if k not in _internal_skip},
}
if _is_async:
return aresponses_with_emulated_file_search(

View file

@ -659,6 +659,27 @@ class TestEmulatedFileSearchHandler:
annotations = _build_file_citation_annotations([r1, r2], "text")
assert len(annotations) == 1
def test_H14_include_search_results_dedupes_by_file_id(self):
from litellm.responses.file_search.emulated_handler import (
_build_search_results_for_include,
)
r1, r2 = MagicMock(), MagicMock()
r1.file_id = "file-abc"
r1.filename = "doc.pdf"
r1.score = 0.9
r1.attributes = {}
r1.content = [{"type": "text", "text": "first hit"}]
r2.file_id = "file-abc" # same file appears for a second query
r2.filename = "doc.pdf"
r2.score = 0.85
r2.attributes = {}
r2.content = [{"type": "text", "text": "second hit"}]
search_results = _build_search_results_for_include([r1, r2])
assert len(search_results) == 1
assert search_results[0]["file_id"] == "file-abc"
# --- End-to-end (mocked) ---
@pytest.mark.asyncio