Skip to content

Command goto not routing correctly in astream #6248

@wongjingping

Description

@wongjingping

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

import asyncio
from typing import TypedDict

from langgraph.graph import StateGraph, START, END
from langgraph.types import Command


class State(TypedDict):
    foo: str


async def node_y(state: State) -> dict:
    await asyncio.sleep(0.2)
    return {"foo": "Y done"}


async def node_x(state: State) -> dict:
    await asyncio.sleep(0.5)
    return {"foo": "X done"}


def build_graph() -> StateGraph:
    builder = StateGraph(State)
    builder.add_node("Y", node_y)
    builder.add_node("X", node_x)
    builder.add_edge(START, "Y")
    builder.add_edge("Y", END)
    builder.add_edge("X", END)
    return builder.compile()


async def run_once(x_first: bool = False) -> None:
    # If x_first=True, make X faster than Y to demonstrate order flip
    global node_x, node_y

    async def fast_x(state: State) -> dict:
        await asyncio.sleep(0.1)
        return {"foo": "X done"}

    async def slow_y(state: State) -> dict:
        await asyncio.sleep(0.4)
        return {"foo": "Y done"}

    async def slow_x(state: State) -> dict:
        await asyncio.sleep(0.5)
        return {"foo": "X done"}

    async def fast_y(state: State) -> dict:
        await asyncio.sleep(0.2)
        return {"foo": "Y done"}

    if x_first:
        node_x = fast_x  # type: ignore[assignment]
        node_y = slow_y  # type: ignore[assignment]
    else:
        node_x = slow_x  # type: ignore[assignment]
        node_y = fast_y  # type: ignore[assignment]

    graph = build_graph()

    print("\n=== Streaming with Command(goto='X') ===")
    # Pass Command with goto to schedule X alongside START->Y
    # We only consume the first chunk to see which node emits first
    async for chunk in graph.astream(Command(goto="X"), stream_mode="updates"):
        print(f"first chunk: {chunk}")
        break


async def main() -> None:
    print("Case 1: Y is faster than X (expect Y to emit first)")
    await run_once(x_first=False)

    print("\nCase 2: X is faster than Y (expect X to emit first)")
    await run_once(x_first=True)


if __name__ == "__main__":
    asyncio.run(main())

Error Message and Stack Trace (if applicable)

$ python adhoc/langgraph_goto_mre.py
Case 1: Y is faster than X (expect Y to emit first)

=== Streaming with Command(goto='X') ===
first chunk: {'Y': {'foo': 'Y done'}}

Case 2: X is faster than Y (expect X to emit first)

=== Streaming with Command(goto='X') ===
first chunk: {'X': {'foo': 'X done'}}

Description

I'm trying to use Command(goto="...") when passed to astream to start from a specific node in the graph (X).

However, as this example shows, we're still running the nodes linked right after START (Y), instead of executing the node (X) exclusively. This seems wrong to me, since when a user explicitly asks to goto a specific node, we're still triggering the graph from the start, and depending on which node completes first, we will have a different execution path, which isn't expected.

System Info

System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 24.6.0: Mon Aug 11 21:16:34 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T6020
> Python Version:  3.12.0 (main, Oct  2 2023, 20:56:14) [Clang 16.0.3 ]

Package Information
-------------------
> langchain_core: 0.3.72
> langchain: 0.3.26
> langsmith: 0.4.4
> langchain_openai: 0.3.27
> langchain_text_splitters: 0.3.8
> langgraph_rollback_final: Installed. No version info available.
> langgraph_sdk: 0.2.0

Optional packages not installed
-------------------------------
> langserve

Other Dependencies
------------------
> async-timeout<5.0.0,>=4.0.0;: Installed. No version info available.
> httpx: 0.27.2
> httpx>=0.25.2: Installed. No version info available.
> jsonpatch<2.0,>=1.33: Installed. No version info available.
> langchain-anthropic;: Installed. No version info available.
> langchain-aws;: Installed. No version info available.
> langchain-azure-ai;: Installed. No version info available.
> langchain-cohere;: Installed. No version info available.
> langchain-community;: Installed. No version info available.
> langchain-core<1.0.0,>=0.3.51: Installed. No version info available.
> langchain-core<1.0.0,>=0.3.66: Installed. No version info available.
> langchain-deepseek;: Installed. No version info available.
> langchain-fireworks;: Installed. No version info available.
> langchain-google-genai;: Installed. No version info available.
> langchain-google-vertexai;: Installed. No version info available.
> langchain-groq;: Installed. No version info available.
> langchain-huggingface;: Installed. No version info available.
> langchain-mistralai;: Installed. No version info available.
> langchain-ollama;: Installed. No version info available.
> langchain-openai;: Installed. No version info available.
> langchain-perplexity;: Installed. No version info available.
> langchain-text-splitters<1.0.0,>=0.3.8: Installed. No version info available.
> langchain-together;: Installed. No version info available.
> langchain-xai;: Installed. No version info available.
> langsmith-pyo3: Installed. No version info available.
> langsmith>=0.1.17: Installed. No version info available.
> langsmith>=0.3.45: Installed. No version info available.
> openai-agents: Installed. No version info available.
> openai<2.0.0,>=1.86.0: Installed. No version info available.
> opentelemetry-api: 1.37.0
> opentelemetry-exporter-otlp-proto-http: 1.37.0
> opentelemetry-sdk: 1.37.0
> orjson: 3.10.18
> orjson>=3.10.1: Installed. No version info available.
> packaging: 24.2
> packaging>=23.2: Installed. No version info available.
> pydantic: 2.11.7
> pydantic<3.0.0,>=2.7.4: Installed. No version info available.
> pydantic>=2.7.4: Installed. No version info available.
> pytest: 8.4.1
> PyYAML>=5.3: Installed. No version info available.
> requests: 2.32.4
> requests-toolbelt: 1.0.0
> requests<3,>=2: Installed. No version info available.
> rich: 13.9.4
> SQLAlchemy<3,>=1.4: Installed. No version info available.
> tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
> tiktoken<1,>=0.7: Installed. No version info available.
> typing-extensions>=4.7: Installed. No version info available.
> zstandard: 0.23.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpendingawaiting review/confirmation by maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions