Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document: Explicitly check that pageContents is not an empty string. #2836

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions langchain/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export interface DocumentInput<
}

/**
* Interface for interacting with a document.
* Class for creating and interacting with a document.
*/

export class Document<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Metadata extends Record<string, any> = Record<string, any>
Expand All @@ -19,10 +20,14 @@ export class Document<

metadata: Metadata;

/**
* Creates a new Document instance.
*
* {DocumentInput<Metadata>} fields - Takes `pageContent` as a string
* and optional `metadata` as Metadata.
*/
constructor(fields: DocumentInput<Metadata>) {
this.pageContent = fields.pageContent
? fields.pageContent.toString()
: this.pageContent;
this.pageContent = fields.pageContent?.toString() ?? "";
this.metadata = fields.metadata ?? ({} as Metadata);
}
}
14 changes: 14 additions & 0 deletions langchain/src/tests/document.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Document } from "../document.js";

describe("Document", () => {
test("Constructing a document with an empty sting", async () => {
const doc = new Document({ pageContent: "" });
expect(doc.pageContent).toEqual("");
});

test("Constructing a document with an undefined at runtime", async () => {
const pageContent = undefined as unknown as string;
const doc = new Document({ pageContent });
expect(doc.pageContent).toEqual("");
});
});