Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running in Firebase Function #472

Open
objectiveSee opened this issue Sep 10, 2024 · 5 comments
Open

Running in Firebase Function #472

objectiveSee opened this issue Sep 10, 2024 · 5 comments

Comments

@objectiveSee
Copy link

objectiveSee commented Sep 10, 2024

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
Copy link
Author

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);

@jacoblee93
Copy link
Collaborator

Sorry for the delay - this is pretty odd. It should accept a function. Logging wikipediaNode just gives back a function?

Is there anything unique about Firebase Functions as a runtime?

@kdawgwilk
Copy link

They are just using NodeJS runtime although you can control which NodeJS version. I wonder if its specific to a NodeJS version? @objectiveSee what version of NodeJS are you using in your firebase functions?

@objectiveSee
Copy link
Author

This is running on node 22. What version have you been using? Have you gotten this to work?

@kdawgwilk
Copy link

Try adding .js file extensions on your imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants