langchain: Fix error in LLMListwiseRerank when Document list is empty (#31300)

**Description:**
This PR fixes an `IndexError` that occurs when `LLMListwiseRerank` is
called with an empty list of documents.

Earlier, the code assumed the presence of at least one document and
attempted to construct the context string based on `len(documents) - 1`,
which raises an error when documents is an empty list.

The fix works with gpt-4o-mini if I make the list empty, but fails
occasionally with gpt-3.5-turbo. In case of empty list, setting the
string to "empty list" seems to have the expected response.

**Issue:**  #31192
This commit is contained in:
Himanshu Sharma 2025-06-17 09:14:07 -05:00 committed by GitHub
parent 0b5c06e89f
commit bb7c190d2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,7 +24,10 @@ def _get_prompt_input(input_: dict) -> dict[str, Any]:
context = ""
for index, doc in enumerate(documents):
context += f"Document ID: {index}\n```{doc.page_content}```\n\n"
context += f"Documents = [Document ID: 0, ..., Document ID: {len(documents) - 1}]"
document_range = "empty list"
if len(documents) > 0:
document_range = f"Document ID: 0, ..., Document ID: {len(documents) - 1}"
context += f"Documents = [{document_range}]"
return {"query": input_["query"], "context": context}