Skip to content

Commit

Permalink
Fix: Handle empty documents in ContextualCompressionRetriever (Issue #…
Browse files Browse the repository at this point in the history
…5304) (#5306)

# Fix: Handle empty documents in ContextualCompressionRetriever (Issue
#5304)

Fixes #5304 

Prevent cohere.error.CohereAPIError caused by an empty list of documents
by adding a condition to check if the input documents list is empty in
the compress_documents method. If the list is empty, return an empty
list immediately, avoiding the error and unnecessary processing.

@dev2049

---------

Co-authored-by: Dev 2049 <[email protected]>
  • Loading branch information
hanguofeng and dev2049 authored May 28, 2023
1 parent 1366d07 commit 99a1e3f
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions langchain/retrievers/document_compressors/cohere_rerank.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def validate_environment(cls, values: Dict) -> Dict:
def compress_documents(
self, documents: Sequence[Document], query: str
) -> Sequence[Document]:
if len(documents) == 0: # to avoid empty api call
return []
doc_list = list(documents)
_docs = [d.page_content for d in doc_list]
results = self.client.rerank(
Expand Down

0 comments on commit 99a1e3f

Please sign in to comment.