Skip to content

interrupt works only for streaming #1422

Description

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangGraph.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangGraph.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package).

Example Code

import { MemorySaver, Annotation, interrupt, Command, StateGraph } from "@langchain/langgraph";

// Define the graph state
const StateAnnotation = Annotation.Root({
  some_text: Annotation<string>()
});

function humanNode(state: typeof StateAnnotation.State) {
   const value = interrupt(
      // Any JSON serializable value to surface to the human.
      // For example, a question or a piece of text or a set of keys in the state
      {
         text_to_revise: "Do you wish to approve ?"
      }
   );
   return {
      // Update the state with the human's input
      some_text: value
   };
}

// Build the graph
const workflow = new StateGraph(StateAnnotation)
// Add the human-node to the graph
  .addNode("human_node", humanNode)
  .addEdge("__start__", "human_node")

// A checkpointer is required for `interrupt` to work.
const checkpointer = new MemorySaver();
const graph = workflow.compile({
   checkpointer
});
const config = { configurable: { thread_id: "123" } };

// Using stream() to directly surface the `__interrupt__` information.

for await (const chunk of await graph.stream(
   { some_text: "Original text" },
   config
)) {
   console.log("Paused state", JSON.stringify(chunk));
}

// Resume using Command
for await (const chunk of await graph.stream(
   new Command({ resume: "Edited text" }),
   config
)) {
   console.log(chunk);
}

This actually prints

Paused state {"__interrupt__":[{"value":{"text_to_revise":"Do you wish to approve ?"},"when":"during","resumable":true,"ns":["human_node:256ce302-2f0b-5ba8-8eba-ec02b2bb7a82"]}]}
{ human_node: { some_text: 'Edited text' } }

But when I use invoke for the execution

import { MemorySaver, Annotation, interrupt, Command, StateGraph } from "@langchain/langgraph";

// Define the graph state
const StateAnnotation = Annotation.Root({
  some_text: Annotation<string>()
});

function humanNode(state: typeof StateAnnotation.State) {
   const value = interrupt(
      // Any JSON serializable value to surface to the human.
      // For example, a question or a piece of text or a set of keys in the state
      {
         text_to_revise: "Do you wish to approve ?"
      }
   );
   return {
      // Update the state with the human's input
      some_text: value
   };
}

// Build the graph
const workflow = new StateGraph(StateAnnotation)
// Add the human-node to the graph
  .addNode("human_node", humanNode)
  .addEdge("__start__", "human_node")

// A checkpointer is required for `interrupt` to work.
const checkpointer = new MemorySaver();
const graph = workflow.compile({
   checkpointer
});
const config = { configurable: { thread_id: "123" } };

const result = await graph.invoke({ some_text: "Original text" },config);
console.log("Paused state", JSON.stringify(result));

// Resume using Command
for await (const chunk of await graph.stream(
   new Command({ resume: "Edited text" }),
   config
)) {
   console.log(chunk);
}

I am getting only the state where it is paused

Paused state {"some_text":"Original text"}
{ human_node: { some_text: 'Edited text' } }

Error Message and Stack Trace (if applicable)

No response

Description

  • Using the streaming it is actually working
  • But using invoke it is not working

System Info

Linux
TS
NodeJS 18

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions