Open mrcrdwd opened 3 months ago
Hey @mrcrdwd! Great to see you diving into the depths of LangChain.js again. Hope you've been doing well since our last chat! 🚀
Based on the error message and stack trace you provided, it seems like the issue is related to the setFileContent()
method in the LocalFileStore
class. This method expects a Uint8Array
as the content to be written to the file. However, the ParentDocumentRetriever
might be passing a different type, which is causing the ERR_INVALID_ARG_TYPE
error.
In your implementation, you're using LocalFileStore
as the docstore
for ParentDocumentRetriever
. When you call addDocuments()
, ParentDocumentRetriever
internally calls the mset()
method of LocalFileStore
, which in turn calls setFileContent()
for each document. The content passed to setFileContent()
should be a Uint8Array
.
Here's the relevant part of the mset()
method in LocalFileStore
:
async mset(pairs: [string, Uint8Array][]): Promise<void> {
await Promise.all(
pairs.map(([key, content]) => this.setFileContent(content, key))
);
}
And here's the setFileContent()
method:
private async setFileContent(content: Uint8Array, key: string): Promise<void> {
const filePath = this.getFilePathForKey(key);
await fs.writeFile(filePath, content);
}
As you can see, setFileContent()
expects content
to be a Uint8Array
. If ParentDocumentRetriever
is passing a different type, that would cause the ERR_INVALID_ARG_TYPE
error.
To fix this issue, you need to ensure that the documents you're passing to addDocuments()
are correctly converted to Uint8Array
before they're stored. The Document
class should handle this conversion, but you might need to check if there's an issue with the way you're creating your Document
instances.
Here's how you're creating your Document
instances:
const parentDocuments = [new Document({ pageContent: "The first document."}), new Document({ pageContent: "Another document for testing."})]
Ensure that the pageContent
property is correctly converted to Uint8Array
in the Document
class. If the issue persists, you might need to debug the Document
class and the addDocuments()
method in ParentDocumentRetriever
to see where the type mismatch is happening.
I hope this helps! If you have any more questions or need further clarification, feel free to ask.
Sources:
There is a clear difference between how mset()
is implemented in LocalFileStore
compared to InMemoryStore
. InMemoryStore
takes T
as a parameter while LocalFileStore
requires an Uint8Array
.
And addDocuments()
in ParentDocumentRetriever
passes a Record<string, Document>
array tomset()
.
LocalFileStore
async mset(keyValuePairs: [string, Uint8Array][]): Promise<void> {
await Promise.all(
keyValuePairs.map(([key, value]) => this.setFileContent(value, key))
);
}
InMemoryStore
async mset(keyValuePairs: [string, T][]): Promise<void> {
for (const [key, value] of keyValuePairs) {
this.store[key] = value;
}
}
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
I'm trying to use
LocalFileStore
as a replacement forInMemoryStore
.The example code above (running in an N8N LangChain Code node) throws an
ERR_INVALID_ARG_TYPE
error.Inspecting file_system.ts it looks like
setFileContent()
expects anUint8Array
. I couldn't tell if parent_document.ts is passing the right type.Is this a user error from my end?
System Info