From bb23668f3cdc4d88c3157076698a6aa7e10d073a Mon Sep 17 00:00:00 2001 From: Kevin Tam Date: Tue, 25 Jun 2024 09:36:58 +0800 Subject: [PATCH] fix: Document should handle empty pageContent (fixes #5884) --- langchain-core/src/documents/document.ts | 8 ++++---- langchain-core/src/tests/document.test.ts | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 langchain-core/src/tests/document.test.ts diff --git a/langchain-core/src/documents/document.ts b/langchain-core/src/documents/document.ts index 5979bbf63756..1d27d8002a20 100644 --- a/langchain-core/src/documents/document.ts +++ b/langchain-core/src/documents/document.ts @@ -20,8 +20,8 @@ export interface DocumentInterface< * Interface for interacting with a document. */ export class Document< - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Metadata extends Record = Record + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Metadata extends Record = Record > implements DocumentInput, DocumentInterface { pageContent: string; @@ -29,9 +29,9 @@ export class Document< metadata: Metadata; constructor(fields: DocumentInput) { - this.pageContent = fields.pageContent + this.pageContent = fields.pageContent !== undefined ? fields.pageContent.toString() - : this.pageContent; + : ""; this.metadata = fields.metadata ?? ({} as Metadata); } } diff --git a/langchain-core/src/tests/document.test.ts b/langchain-core/src/tests/document.test.ts new file mode 100644 index 000000000000..e839621b2b4f --- /dev/null +++ b/langchain-core/src/tests/document.test.ts @@ -0,0 +1,7 @@ +import {test, expect} from '@jest/globals' +import {Document} from '../documents/document.js' + +test('Document should handle empty pageContent', () => { + const doc = new Document({pageContent: ''}) + expect(doc.pageContent).toEqual('') +})