langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.75k stars 2.2k forks source link

await the generation in the no cache branch #7233

Closed kevinnayar closed 1 day ago

kevinnayar commented 3 days ago

Fixes #7172

vercel[bot] commented 3 days ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Nov 19, 2024 6:31pm
1 Skipped Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **langchainjs-api-refs** | ⬜️ Ignored ([Inspect](https://vercel.com/langchain/langchainjs-api-refs/2An5nUHkjjJWLQF6Qp9EKaktPxcf)) | | | Nov 19, 2024 6:31pm |
jacoblee93 commented 2 days ago

Thanks for the PR!

How exactly does this fix the linked issue?

kevinnayar commented 1 day ago

Oh I'm sorry, I attached the wrong issue but I cannot seem to locate the one I was viewing earlier and am trying to address. The attached issue is related to ChatGoogleGenerativeAI and withStructuredOutput, but the one I was referencing was related to ChatVertexAI. I'll keep digging for it.

Without awaiting the return on this method, the response when calling model.withStructuredOutput(...) is consistently the following error:

TypeError: Cannot read properties of undefined (reading 'text')

In this call stack sequence invoke -> generatePrompt -> generate, the last method doesn't return a value which results in this error. I believe this is because generate's return type is a promise/non-promise union and therefore, it should be awaited.

generate(...): Promise<{
    generations: never[][];
    llmOutput: any;
} | {
    generations: any[];
    llmOutput: {};
}>
jacoblee93 commented 1 day ago

Gotcha. The only difference the await makes here after a return is in the call stack if an error were to occur:

https://stackoverflow.com/questions/38708550/difference-between-return-await-promise-and-return-promise

kevinnayar commented 1 day ago

Yep, understand what awaits does. I did think it was weird that it resolves the issue when this minor edit of the dist file. I'll do a bit more debugging and create a more meaningful bug first. The issue does persist without it. Thanks.