Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions haystack/components/rankers/llm_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def run(self, query: str, documents: list[Document], top_k: int | None = None) -
deduplicated_documents = _deduplicate_documents(documents)
fallback_documents = deduplicated_documents

if not query.strip():
if not isinstance(query, str) or not query.strip():
logger.warning("Empty query provided to LLMRanker. Returning documents without reranking.")
return {"documents": fallback_documents}

Expand Down Expand Up @@ -320,7 +320,7 @@ async def run_async(
deduplicated_documents = _deduplicate_documents(documents)
fallback_documents = deduplicated_documents

if not query.strip():
if not isinstance(query, str) or not query.strip():
logger.warning("Empty query provided to LLMRanker. Returning documents without reranking.")
return {"documents": fallback_documents}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fixes:
- |
``LLMRanker`` no longer crashes with ``AttributeError`` when ``query`` is
``None`` (or another non-string) coming from an upstream pipeline
component. Both ``run`` and ``run_async`` now return the documents
unranked, matching the existing empty-query fallback.
19 changes: 19 additions & 0 deletions test/components/rankers/test_llm_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ def test_run_whitespace_query_returns_fallback(mock_chat_generator):
mock_chat_generator.run.assert_not_called()


@pytest.mark.parametrize("bad_query", [None, 123])
def test_run_non_string_query_returns_fallback(mock_chat_generator, bad_query):
documents = [Document(id="1", content="first"), Document(id="2", content="second")]
ranker = LLMRanker(chat_generator=mock_chat_generator)

assert ranker.run(query=bad_query, documents=documents) == {"documents": documents}
mock_chat_generator.run.assert_not_called()


def test_run_successful_ranking():
documents = [
Document(id="1", content="first"),
Expand Down Expand Up @@ -417,6 +426,16 @@ async def test_run_async_generator_exception_raises(self):
with pytest.raises(RuntimeError, match="generator failed"):
await ranker.run_async(query="test query", documents=documents)

@pytest.mark.asyncio
async def test_run_async_none_query_returns_fallback(self):
documents = [Document(id="1", content="first"), Document(id="2", content="second")]
mock_chat_generator = Mock(spec=OpenAIChatGenerator)
mock_chat_generator.run_async = AsyncMock()
ranker = LLMRanker(chat_generator=mock_chat_generator)

assert await ranker.run_async(query=None, documents=documents) == {"documents": documents} # type: ignore[arg-type]
mock_chat_generator.run_async.assert_not_called()

@pytest.mark.integration
@pytest.mark.skipif(
not os.environ.get("OPENAI_API_KEY", None),
Expand Down
Loading