langchain-ai / langchainjs

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

Possible circular dependencies breaking from building binaries with Bun #4602

Open stewones opened 4 months ago

stewones commented 4 months ago

Checked other resources

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:

  1. bun install
  2. bun build --compile --sourcemap ./index.ts --outfile app
  3. ./app

System Info

platform: mac Python: 3.9.6 langchain: 0.1.25

dosubot[bot] commented 4 months ago

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
jacoblee93 commented 4 months ago

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.

mryraghi commented 2 months ago

@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
mryraghi commented 2 months ago

@jacoblee93 Hey Jacob 👋🏼 any chance this can get solved? PromptTemplates can't really be substituted if used with OpenAI's function calling, correct?

stewones commented 2 months ago

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 🥹

mryraghi commented 2 months ago

Not on Windows but in a Lambda function, even worse

jacoblee93 commented 2 months ago

I have it planned for two weeks out but will try to get to it as soon as I can. Sorry for the delay.

PaoloRollo commented 1 month ago

is there any update regarding this issue?

ubaranzorlu commented 2 weeks ago

Same issue happens for me when I try to build with bun.