Skip to content

6.4 Handle double texting ​

This chapter reorganizes the upstream notebook and Python implementation into a web lesson. The teaching notes were prepared for this site with a matching Chinese edition.

Source notebook · Python implementation and configuration

Version and environment

Code follows the upstream snapshot dated 2025-10-12, not a compatibility promise for current SDKs. Original code cells appear below in notebook order, including Jupyter magic such as %pip / %%capture; execute them separately in a notebook. Configure credentials locally, never in a web page or Git. This site renders lessons; the paid model calls and Docker exercises have not been executed here.

Why concurrent input matters ​

A user may submit another message while the first run is active. Set multitask_strategy on the new run to define what happens on that thread. Tests need an actually running first request; otherwise an apparently successful second request proves little about concurrency.

Reject or enqueue ​

reject refuses the new run while the thread is busy; the example catches an HTTPStatusError. enqueue preserves both requests and schedules the second after the first. In a UI, explain rejection or show queued status so input does not appear lost.

Interrupt or rollback ​

interrupt stops the current run but retains progress already saved; the old run can be inspected as interrupted. rollback removes the prior run and restores thread state before starting the replacement. Neither policy should be assumed to undo external side effects such as an email already sent or an independent store write.

Original code cells ​

Comments and identifiers remain as supplied upstream. Cells share variables from earlier cells; saved outputs and historical screenshots are not results from this website.

Cell 1 ​

python
%%capture --no-stderr
%pip install -U langgraph_sdk

Cell 2 ​

python
from langgraph_sdk import get_client
url_for_cli_deployment = "http://localhost:8123"
client = get_client(url=url_for_cli_deployment)

Cell 3 ​

python
import httpx
from langchain_core.messages import HumanMessage

# Create a thread
thread = await client.threads.create()

# Create to dos
user_input_1 = "Add a ToDo to follow-up with DI Repairs."
user_input_2 = "Add a ToDo to mount dresser to the wall."
config = {"configurable": {"user_id": "Test-Double-Texting"}}
graph_name = "task_maistro"

run = await client.runs.create(
    thread["thread_id"],
    graph_name,
    input={"messages": [HumanMessage(content=user_input_1)]},
    config=config,
)
try:
    await client.runs.create(
        thread["thread_id"],
        graph_name,
        input={"messages": [HumanMessage(content=user_input_2)]},
        config=config,
        multitask_strategy="reject",
    )
except httpx.HTTPStatusError as e:
    print("Failed to start concurrent run", e)

Cell 4 ​

python
from langchain_core.messages import convert_to_messages

# Wait until the original run completes
await client.runs.join(thread["thread_id"], run["run_id"])

# Get the state of the thread
state = await client.threads.get_state(thread["thread_id"])
for m in convert_to_messages(state["values"]["messages"]):
    m.pretty_print()

Cell 5 ​

python
# Create a new thread
thread = await client.threads.create()

# Create new ToDos
user_input_1 = "Send Erik his t-shirt gift this weekend."
user_input_2 = "Get cash and pay nanny for 2 weeks. Do this by Friday."
config = {"configurable": {"user_id": "Test-Double-Texting"}}
graph_name = "task_maistro"

first_run = await client.runs.create(
    thread["thread_id"],
    graph_name,
    input={"messages": [HumanMessage(content=user_input_1)]},
    config=config,
)

second_run = await client.runs.create(
    thread["thread_id"],
    graph_name,
    input={"messages": [HumanMessage(content=user_input_2)]},
    config=config,
    multitask_strategy="enqueue",
)

# Wait until the second run completes
await client.runs.join(thread["thread_id"], second_run["run_id"])

# Get the state of the thread
state = await client.threads.get_state(thread["thread_id"])
for m in convert_to_messages(state["values"]["messages"]):
    m.pretty_print()

Cell 6 ​

python
import asyncio

# Create a new thread
thread = await client.threads.create()

# Create new ToDos
user_input_1 = "Give me a summary of my ToDos due tomrrow."
user_input_2 = "Never mind, create a ToDo to Order Ham for Thanksgiving by next Friday."
config = {"configurable": {"user_id": "Test-Double-Texting"}}
graph_name = "task_maistro"

interrupted_run = await client.runs.create(
    thread["thread_id"],
    graph_name,
    input={"messages": [HumanMessage(content=user_input_1)]},
    config=config,
)

# Wait for some of run 1 to complete so that we can see it in the thread
await asyncio.sleep(1)

second_run = await client.runs.create(
    thread["thread_id"],
    graph_name,
    input={"messages": [HumanMessage(content=user_input_2)]},
    config=config,
    multitask_strategy="interrupt",
)

# Wait until the second run completes
await client.runs.join(thread["thread_id"], second_run["run_id"])

# Get the state of the thread
state = await client.threads.get_state(thread["thread_id"])
for m in convert_to_messages(state["values"]["messages"]):
    m.pretty_print()

Cell 7 ​

python
# Confirm that the first run was interrupted
print((await client.runs.get(thread["thread_id"], interrupted_run["run_id"]))["status"])

Cell 8 ​

python
# Create a new thread
thread = await client.threads.create()

# Create new ToDos
user_input_1 = "Add a ToDo to call to make appointment at Yoga."
user_input_2 = "Actually, add a ToDo to drop by Yoga in person on Sunday."
config = {"configurable": {"user_id": "Test-Double-Texting"}}
graph_name = "task_maistro"

rolled_back_run = await client.runs.create(
    thread["thread_id"],
    graph_name,
    input={"messages": [HumanMessage(content=user_input_1)]},
    config=config,
)

second_run = await client.runs.create(
    thread["thread_id"],
    graph_name,
    input={"messages": [HumanMessage(content=user_input_2)]},
    config=config,
    multitask_strategy="rollback",
)

# Wait until the second run completes
await client.runs.join(thread["thread_id"], second_run["run_id"])

# Get the state of the thread
state = await client.threads.get_state(thread["thread_id"])
for m in convert_to_messages(state["values"]["messages"]):
    m.pretty_print()

Cell 9 ​

python
# Confirm that the original run was deleted
try:
    await client.runs.get(thread["thread_id"], rolled_back_run["run_id"])
except httpx.HTTPStatusError as _:
    print("Original run was correctly deleted")

Practice and checks ​

Try each policy on a fresh thread with test tasks. Inspect both run status and thread messages. For rollback, also inspect the store separately; for enqueue, wait for the second run before judging the final state.

For the full original narrative, saved outputs and diagrams, consult the notebook.

English translation of community learning materials. MIT licensed.