langchain-ai / langgraphjs

⚡ Build language agents as graphs ⚡
https://langchain-ai.github.io/langgraphjs/
MIT License
493 stars 72 forks source link

Running in Firebase Function #472

Open objectiveSee opened 1 week ago

objectiveSee commented 1 week ago

I am getting run-time error when trying to run LangGraph inside a Firebase function.

Firebase Function:

exports.langgraph = onRequest(async (request, response) => {
  const articleId = request.query.articleId as string;
  const workflow = getWorkflow();

  const result = await workflow.invoke({
    articleId,
  });
  response.json(result);
});

Workflow:

import { END, START, StateGraph } from "@langchain/langgraph";
import { AgentState } from "./state";
import { testNode, wikipediaNode } from "./nodes";
import { researcherNode } from "./nodes/researcher";

export const getWorkflow = () => {
  const workflow = new StateGraph(AgentState)
    .addNode("final", testNode)
    .addNode("wikipedia", wikipediaNode)
    .addNode("researcher", researcherNode)
    .addEdge(START, "wikipedia")
    .addEdge("wikipedia", "researcher")
    .addEdge("researcher", "final")
    .addEdge("final", END);

  return workflow.compile();
};

Error:

⚠  functions: Error: Expected a Runnable, function or object.
Instead got an unsupported type.
    at _coerceToRunnable (/Users/user/workspace/foo-api/functions/node_modules/@langchain/core/dist/runnables/base.cjs:1857:15)
    at StateGraph.addNode (/Users/user/workspace/foo-api/functions/node_modules/@langchain/langgraph/dist/graph/state.cjs:204:57)
    at getWorkflow (/Users/user/workspace/foo-api/functions/lib/src/graph/index.js:11:10)
    at /Users/user/workspace/foo-api/functions/lib/src/index.js:225:46
    at /Users/user/workspace/foo-api/functions/node_modules/firebase-functions/lib/v2/providers/https.js:65:29
    at cors (/Users/user/workspace/foo-api/functions/node_modules/cors/lib/index.js:188:7)
    at /Users/user/workspace/foo-api/functions/node_modules/cors/lib/index.js:224:17
    at originCallback (/Users/user/workspace/foo-api/functions/node_modules/cors/lib/index.js:214:15)
    at /Users/user/workspace/foo-api/functions/node_modules/cors/lib/index.js:219:13
    at optionsCallback (/Users/user/workspace/foo-api/functions/node_modules/cors/lib/index.js:199:9)
⚠  Your function was killed because it raised an unhandled error.

Any thoughts on why this might happen? My code worked with LangGraph studio. I've tried two ways of doing this: 1) the workflow is compiled at script start time, and 2) on demand via getWorkflow(). Both fail with same error.

Thanks

Packages:

    "@langchain/anthropic": "^0.2.17",
    "@langchain/core": "^0.2.31",
    "@langchain/langgraph": "^0.2.2",
    "@langchain/openai": "^0.2.10",
objectiveSee commented 1 week ago

Follow-up: Looks like the issue is what the way nodes are defined. This works fine in LangGraph Studio, but fails at run-time when I hook it up to a Firebase Function. It seems like the graph doesn't like the way the node is defined, which is odd because I am following the docs. Is there something I am missing?

Node:

export const wikipediaNode = async (state: typeof AgentState.State) => {
  const x = state.articleId;
  const articleContent = await fetchArticleContent(Number(x));
  return { article: articleContent };
};

Simplified Example:

❌ FAILS

  workflow = new StateGraph(AgentState)
    .addNode("wiki", wikipediaNode)
    .addEdge(START, "wiki")
    .addEdge("wiki", END);

✅ SUCEEDS

workflow = new StateGraph(AgentState).addEdge(START, END);