I can't use stream LLM tokens in my ts #208

Closed
opened 2026-02-15 17:17:20 -05:00 by yindo · 2 comments
Owner

Originally created by @xiaohai0903 on GitHub (Mar 22, 2025).

I can't use stream LLM tokens in my ts. According to the examples on the official website, I don't receive streaming output. They are all output to me at the end. I use AzureChatOpenAI, but I can actually output in my python project. I don't know where the problem is, so the libraries are all the latest. Can you help me solve it?

export class LLMChatClass {
  private llm;
  //   private agent;
  constructor() {
    this.llm = new AzureChatOpenAI(params).bindTools(tools);
    // this.agent = createReactAgent({
    //   llm: this.llm,
    //   tools: [search],
    // });
  }
  async stream(input: string, fun: Function) {
    for await (const chunk of await this.llm.stream(input)) {
      console.log(chunk);
      fun(chunk);
    }
  }
  callModel = async (state: typeof MessagesAnnotation.State) => {
    const { messages } = state;
    const response = await this.llm.invoke(messages);

    return { messages: response };
  };
  shouldContinue = (state: typeof MessagesAnnotation.State) => {
    const { messages } = state;
    const lastMessage = messages[messages.length - 1];
    if (
      "tool_calls" in lastMessage &&
      Array.isArray(lastMessage.tool_calls) &&
      lastMessage.tool_calls?.length
    ) {
      return "tools";
    }
    return "__end__";
  };
  async runAgent(input: string) {
    const graph = new StateGraph(MessagesAnnotation)
      .addNode("agent", this.callModel)
      .addNode("tools", toolNodeForGraph)
      .addEdge("__start__", "agent")
      .addConditionalEdges("agent", this.shouldContinue)
      .addEdge("tools", "agent")
      .compile();

    const stream = await graph.stream(
      {
        messages: [{ role: "user", content: input }],
      },
      {
        streamMode: "messages",
      }
    );
    for await (const [message, _metadata] of stream) {
      if (isAIMessageChunk(message) && message.tool_call_chunks?.length) {
        console.log(
          `${message.getType()} MESSAGE TOOL CALL CHUNK: ${
            message.tool_call_chunks[0].args
          }`
        );
      } else {
        console.log(`${message.getType()} MESSAGE CONTENT: ${message.content}`);
      }
    }
  }
}
Originally created by @xiaohai0903 on GitHub (Mar 22, 2025). I can't use stream LLM tokens in my ts. According to the examples on the official website, I don't receive streaming output. They are all output to me at the end. I use AzureChatOpenAI, but I can actually output in my python project. I don't know where the problem is, so the libraries are all the latest. Can you help me solve it? ``` export class LLMChatClass { private llm; // private agent; constructor() { this.llm = new AzureChatOpenAI(params).bindTools(tools); // this.agent = createReactAgent({ // llm: this.llm, // tools: [search], // }); } async stream(input: string, fun: Function) { for await (const chunk of await this.llm.stream(input)) { console.log(chunk); fun(chunk); } } callModel = async (state: typeof MessagesAnnotation.State) => { const { messages } = state; const response = await this.llm.invoke(messages); return { messages: response }; }; shouldContinue = (state: typeof MessagesAnnotation.State) => { const { messages } = state; const lastMessage = messages[messages.length - 1]; if ( "tool_calls" in lastMessage && Array.isArray(lastMessage.tool_calls) && lastMessage.tool_calls?.length ) { return "tools"; } return "__end__"; }; async runAgent(input: string) { const graph = new StateGraph(MessagesAnnotation) .addNode("agent", this.callModel) .addNode("tools", toolNodeForGraph) .addEdge("__start__", "agent") .addConditionalEdges("agent", this.shouldContinue) .addEdge("tools", "agent") .compile(); const stream = await graph.stream( { messages: [{ role: "user", content: input }], }, { streamMode: "messages", } ); for await (const [message, _metadata] of stream) { if (isAIMessageChunk(message) && message.tool_call_chunks?.length) { console.log( `${message.getType()} MESSAGE TOOL CALL CHUNK: ${ message.tool_call_chunks[0].args }` ); } else { console.log(`${message.getType()} MESSAGE CONTENT: ${message.content}`); } } } } ```
yindo added the invalid label 2026-02-15 17:17:20 -05:00
yindo closed this issue 2026-02-15 17:17:20 -05:00
Author
Owner

@benjamincburns commented on GitHub (Mar 30, 2025):

Just a guess, but you're using arrow functions as class members and referencing this within them.

See the first bullet point on the MDN Arrow function expressions docs page.

You said this was "according to the examples on the official website" - did you copy this from our docs? If so we'll definitely want to fix that!

Edit: ah, I'm guessing that you copied the examples we had and then dropped them into a class as methods. AFAIK none of our examples are written as classes like this.

@benjamincburns commented on GitHub (Mar 30, 2025): Just a guess, but you're using arrow functions as class members and referencing `this` within them. See the first bullet point on [the MDN Arrow function expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) docs page. You said this was "according to the examples on the official website" - did you copy this from our docs? If so we'll definitely want to fix that! Edit: ah, I'm guessing that you copied the examples we had and then dropped them into a class as methods. AFAIK none of our examples are written as classes like this.
Author
Owner

@benjamincburns commented on GitHub (Mar 30, 2025):

Going to close this, as this isn't an issue w/ langgraph. Feel free to open a discussion if you want more help - or join our Community Slack and ask there!

@benjamincburns commented on GitHub (Mar 30, 2025): Going to close this, as this isn't an issue w/ langgraph. Feel free to open a discussion if you want more help - or join our [Community Slack](https://www.langchain.com/join-community) and ask there!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#208