langchainjs
langchainjs copied to clipboard
Possible circular dependencies breaking from building binaries with Bun
Checked other resources
- [X] I added a very descriptive title to this issue.
- [X] I searched the LangChain.js documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I am sure that this is a bug in LangChain.js rather than my code.
- [X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
import { LLMChain } from "langchain/chains";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { PromptTemplate } from "langchain/prompts";
const model = new ChatOpenAI();
const template = `
you're a helpful assistant who is very knowledgeable about the world.
input: {input}
output:
`;
const prompt = PromptTemplate.fromTemplate(template, {
validateTemplate: false,
});
const chain = new LLMChain({ llm: model, prompt, verbose: true });
chain.call({
input: "What is the meaning of life?",
});
Error Message and Stack Trace (if applicable)
134254 | var init_base5 = __esm(() => {
134255 | init_base4();
134256 | });
134257 |
134258 | // node_modules/@langchain/core/dist/prompts/string.js
134259 | class BaseStringPromptTemplate extends BasePromptTemplate {
^
ReferenceError: Cannot access uninitialized variable.
at /$bunfs/root/chatness:134259:40
Description
I'm trying to generate a binary from an app that relies on langchain.
here's the minimal reproduction https://github.com/stewones/langchain-bun-binary
steps to recreate:
-
bun install
-
bun build --compile --sourcemap ./index.ts --outfile app
-
./app
System Info
platform: mac Python: 3.9.6 langchain: 0.1.25
Hey @stewones, great to see you around here again! Ran into another tricky spot, huh? Let's dive into it. 🚀
The "ReferenceError: Cannot access uninitialized variable" error you're encountering when trying to generate a binary from an app that relies on langchainjs using Bun is likely due to the way Bun handles module exports and imports, especially when dealing with circular dependencies or the order of module initialization.
In JavaScript and Node.js environments, modules are loaded and evaluated synchronously. This means that if a module A exports something that is immediately required by module B, which in turn is required by module A (a circular dependency), the environment ensures that these dependencies are resolved in a way that does not throw a reference error, even if it means one of the modules might temporarily get an incomplete view of the other module's exports.
However, Bun, being a newer and different runtime that aims for performance improvements over Node.js, might handle these situations differently. It could be stricter about the order of module initialization or how circular dependencies are resolved. If Bun encounters a reference to an export that has not been initialized yet due to the module containing it not having been fully evaluated, it might throw a "ReferenceError: Cannot access uninitialized variable" error.
Given the extensive list of exports in your entrypoints.js
file, it's plausible that there's a circular dependency somewhere within the langchain
modules, or a module is being imported before it's fully initialized due to the order of exports. This situation is exacerbated in a complex dependency graph where many modules interdepend on each other.
To resolve this issue, you might need to:
-
Analyze the Dependency Graph: Use tools or manual inspection to understand the dependency graph of the
langchain
modules. Look for circular dependencies that might cause some modules to be used before they're fully initialized. - Refactor Circular Dependencies: If possible, refactor the code to eliminate circular dependencies. This might involve changing the architecture slightly, such as introducing intermediary modules that break the circular chain.
- Adjust Import/Export Order: Try reordering imports and exports in a way that ensures modules are fully initialized before they're used. This might be trial and error but can resolve issues where the initialization order is the problem.
- Check for Bun-Specific Issues: Since Bun is relatively new, there might be specific issues or limitations with how it handles module exports and imports. Checking Bun's documentation, GitHub issues, or forums for similar problems encountered by others might provide a solution or workaround.
Without specific details on the internals of the langchain
modules and how they interact, these are general approaches to troubleshooting and resolving the "ReferenceError: Cannot access uninitialized variable" error in this context.
Unfortunately, I don't have the exact structure of the dependency graph within the langchainjs codebase. This information might be available in the repository's documentation or by analyzing the codebase with a tool that can generate dependency graphs. If you need further assistance, please provide more details about the specific modules and dependencies involved in your application.
Sources
About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
Thanks for the heads up - stopped the Bun CI test due to some issues but will try to fix this and get that back running.
@stewones have you found a way around this? I'm building a TS project with Bun for Lambda in 'esm' and for target platform 'node', all looks good except:
file:///Users/user/Development/.../packages/lambda-processor/dist/index.js:758712
class BaseStringPromptTemplate extends BasePromptTemplate {
^
ReferenceError: Cannot access 'BasePromptTemplate' before initialization
at file:///Users/user/Development/.../packages/lambda-processor/dist/index.js:758712:40
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Node.js v18.17.0
@jacoblee93 Hey Jacob 👋🏼 any chance this can get solved? PromptTemplates can't really be substituted if used with OpenAI's function calling, correct?
No I haven't found a workaround. Anyways I was just experimenting with bun binaries. My workload runs with "bun run" in production.
Too bad you getting this on windows 🥹
Not on Windows but in a Lambda function, even worse
I have it planned for two weeks out but will try to get to it as soon as I can. Sorry for the delay.
is there any update regarding this issue?
Same issue happens for me when I try to build with bun.
Wait, why won't this be fixed? 😏
Bad bot
This issue seems to be solved with the latest version of Bun (1.1.30), was using 1.1.3 previously so somewhere between it was resovled
Great news! Closing for now then, please comment again if you're seeing issues.
We also cleaned up a few dependencies recently so may have been fixed by that.