From 99a1e3f3a309852da989af080ba47288dcb9a348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E7=8C=AB?= Date: Mon, 29 May 2023 04:19:34 +0800 Subject: [PATCH] Fix: Handle empty documents in ContextualCompressionRetriever (Issue #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 --- langchain/retrievers/document_compressors/cohere_rerank.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/langchain/retrievers/document_compressors/cohere_rerank.py b/langchain/retrievers/document_compressors/cohere_rerank.py index 7774c33971..63060914c7 100644 --- a/langchain/retrievers/document_compressors/cohere_rerank.py +++ b/langchain/retrievers/document_compressors/cohere_rerank.py @@ -50,6 +50,8 @@ class CohereRerank(BaseDocumentCompressor): 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(