Skip to content

Commit

Permalink
fix: Document should handle empty pageContent (fixes langchain-ai#5884)
Browse files Browse the repository at this point in the history
  • Loading branch information
glorat committed Jun 25, 2024
1 parent a4a2d11 commit bb23668
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions langchain-core/src/documents/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ export interface DocumentInterface<
* Interface for interacting with a document.
*/
export class Document<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Metadata extends Record<string, any> = Record<string, any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Metadata extends Record<string, any> = Record<string, any>
> implements DocumentInput, DocumentInterface
{
pageContent: string;

metadata: Metadata;

constructor(fields: DocumentInput<Metadata>) {
this.pageContent = fields.pageContent
this.pageContent = fields.pageContent !== undefined
? fields.pageContent.toString()
: this.pageContent;
: "";
this.metadata = fields.metadata ?? ({} as Metadata);
}
}
7 changes: 7 additions & 0 deletions langchain-core/src/tests/document.test.ts
Original file line number Diff line number Diff line change
@@ -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('')
})

0 comments on commit bb23668

Please sign in to comment.