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

streamEvents with for await loop within try catch does not work #812

Closed
moyara-allgood opened this issue Jan 29, 2025 · 5 comments · Fixed by langchain-ai/langchainjs#7617

Comments

@moyara-allgood
Copy link

moyara-allgood commented Jan 29, 2025

Hi team, I have wasted a tons of time to figure out error messages were swallowed somewhere and finally figured for loop with the streamEvents swallows errors.

This is code to reproduce the issue. I simply copy and pasted from the example in https://langchain-ai.github.io/langgraphjs/concepts/streaming/#streaming-llm-tokens-and-events-streamevents

import { ChatOpenAI } from "@langchain/openai";
import { MessagesAnnotation, StateGraph } from "@langchain/langgraph";

const model = new ChatOpenAI({ model: "gpt-4-turbo-preview" });

function callModel(state: typeof MessagesAnnotation.State) {
  const response = model.invoke(state.messages);
  return { messages: response };
}

const workflow = new StateGraph(MessagesAnnotation)
  .addNode("callModel", callModel)
  .addEdge("__start__", "callModel")
  .addEdge("callModel", "__end__");
const app = workflow.compile();

const inputs = [{ role: "user", content: "hi!" }];

async function main() {
  try {
    for await (const event of app.streamEvents(
      { messages: inputs },
      { version: "v2" },
    )) {
      throw new Error("This should be caught");
    }
  } catch (error) {
    // Error not caught here
    console.error(error);
  }
}
main();

as you can see the code, the error "This should be caught" should be caught in the catch block, but when I run the code, it won't.

by the way this also happens with langchain as well.

import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  model: "gpt-4o-mini",
  temperature: 0,
});

async function main() {
  try {
    for await (const chunk of model.streamEvents(
      "Hello! Tell me about yourself.",
      {
        version: "v2",
      },
    )) {
      throw new Error("should catch this error");
    }
  } catch (e) {
    // Error not caught here
    console.error(e);
  }
}

main();

Test env:
"@langchain/core": "^0.3.27",
"@langchain/langgraph": "^0.2.39",
"@langchain/openai": "^0.3.16",

For fix, I just catch inside the for await loop, handle the error. But ideally it should catch the error from outside try catch.

@moyara-allgood moyara-allgood changed the title streamEvents with for await loop with try catch does not work streamEvents with for await loop within try catch does not work Jan 29, 2025
@jacoblee93
Copy link
Collaborator

Hmm weird - will dig in, thanks for reporting

@jacoblee93
Copy link
Collaborator

When I run it it just hangs - that's what you're seeing too right?

@jacoblee93
Copy link
Collaborator

jacoblee93 commented Jan 29, 2025

Think I have a fix! Will ship tomorrow in @langchain/core.

@jacoblee93
Copy link
Collaborator

Can you bump to @langchain/[email protected] and see if that fixes this for you?

@moyara-allgood
Copy link
Author

Thanks @jacoblee93 ! just verified it is fixed. Thanks for the quick fix and release!

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