diff --git a/langchain/src/chains/combine_documents/base.ts b/langchain/src/chains/combine_documents/base.ts index cf4cb70c833c..5ffa53df6c3d 100644 --- a/langchain/src/chains/combine_documents/base.ts +++ b/langchain/src/chains/combine_documents/base.ts @@ -21,6 +21,9 @@ export async function formatDocuments({ documents: Document[]; config?: RunnableConfig; }) { + if (documents == null || documents.length === 0) { + return ""; + } const formattedDocs = await Promise.all( documents.map((document) => documentPrompt diff --git a/libs/langchain-cohere/src/rerank.ts b/libs/langchain-cohere/src/rerank.ts index 78a13efa2f83..0dd5834e2527 100644 --- a/libs/langchain-cohere/src/rerank.ts +++ b/libs/langchain-cohere/src/rerank.ts @@ -60,6 +60,9 @@ export class CohereRerank extends BaseDocumentCompressor { documents: Array, query: string ): Promise> { + if (documents == null || documents.length === 0) { + return []; + } const _docs = documents.map((doc) => doc.pageContent); const { results } = await this.client.rerank({ model: this.model,