-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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.pageContent should not allow undefined #5884
Document.pageContent should not allow undefined #5884
Comments
To ensure that the export class Document<
// 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 !== undefined
? fields.pageContent.toString()
: "";
this.metadata = fields.metadata ?? ({} as Metadata);
}
} This change ensures that |
@dosu import { test, expect } from "@jest/globals";
import {Document} from "../documents/document.js";
test("Document", async () => {
const doc = new Document({pageContent: ''})
expect (doc.pageContent).toEqual('')
}); |
Your proposed unit test looks good. It effectively checks that the 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('');
});
test("Document should default pageContent to an empty string when not provided", () => {
const doc = new Document({});
expect(doc.pageContent).toEqual('');
}); This ensures that both scenarios are covered. |
Dosu's second test not applicable since DocumentInput does not allow {} Taken the liberty of making a PR |
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
AssertionError: expected undefined to strictly equal ''
Expected :
Actual :undefined
Description
pageContent should be set to '', as mandated by its type contract
string
(and notstring|undefined
)The bug is visible in the code
https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/documents/document.ts
Line 32 should check field.pageContent is undefined, not just truthy, otherwise it uses this.pageContent which is undefined and breaks the contract that pageContent must be a string. Thus in any event, pageContent needs to be initialised to something to fulfill the type contract
System Info
This will happen on any environment
The text was updated successfully, but these errors were encountered: