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

Closed
opened 2026-02-15 17:16:35 -05:00 by yindo · 5 comments
Owner

Originally created by @moyara-allgood on GitHub (Jan 28, 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.

Originally created by @moyara-allgood on GitHub (Jan 28, 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.
yindo closed this issue 2026-02-15 17:16:35 -05:00
Author
Owner

@jacoblee93 commented on GitHub (Jan 28, 2025):

Hmm weird - will dig in, thanks for reporting

@jacoblee93 commented on GitHub (Jan 28, 2025): Hmm weird - will dig in, thanks for reporting
Author
Owner

@jacoblee93 commented on GitHub (Jan 28, 2025):

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

@jacoblee93 commented on GitHub (Jan 28, 2025): When I run it it just hangs - that's what you're seeing too right?
Author
Owner

@jacoblee93 commented on GitHub (Jan 29, 2025):

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

@jacoblee93 commented on GitHub (Jan 29, 2025): Think I have a fix! Will ship tomorrow in `@langchain/core`.
Author
Owner

@jacoblee93 commented on GitHub (Jan 29, 2025):

Can you bump to @langchain/core@0.3.37 and see if that fixes this for you?

@jacoblee93 commented on GitHub (Jan 29, 2025): Can you bump to `@langchain/core@0.3.37` and see if that fixes this for you?
Author
Owner

@moyara-allgood commented on GitHub (Jan 29, 2025):

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

@moyara-allgood commented on GitHub (Jan 29, 2025): Thanks @jacoblee93 ! just verified it is fixed. Thanks for the quick fix and release!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#161