Skip to content

Graphs & nodes

Nodes are plain functions (state -> dict). Graphs wire them together with GraphBuilder and compile to a real LangGraph StateGraph.

Nodes

from langstitch import graph_node

@graph_node(description="Answer the latest message.")
def respond(state: dict) -> dict:
    # read from state, do work, return a partial state update
    return {"response": "...", "messages": [...]}

A node receives the current graph state and returns a dict that is merged into it. Keep nodes pure and import-safe; pull dependencies (LLM, tools, services) at call time via the runtime helpers.

Graphs

from langstitch import graph, GraphBuilder, START, END

@graph(name="main", entrypoint=True)
def main_graph() -> GraphBuilder:
    g = GraphBuilder("main")
    g.add_node("respond", respond)
    g.set_entry_point("respond")
    g.add_edge("respond", END)
    return g
  • entrypoint=True marks the root graph that the server invokes.
  • parent=... registers a subgraph under another graph.
  • START and END are the LangGraph sentinels for edges.

Subgraphs

@graph(name="research", parent="main")
def research_graph() -> GraphBuilder:
    g = GraphBuilder("research")
    g.add_node("gather", gather)
    g.add_node("summarize", summarize)
    g.set_entry_point("gather")
    g.add_edge("gather", "summarize")
    g.add_edge("summarize", END)
    return g

Conditional edges

GraphBuilder mirrors LangGraph's conditional routing โ€” return the name of the next node from a router function:

def route(state: dict) -> str:
    return "escalate" if state.get("needs_human") else "respond"

g.add_conditional_edges("classify", route, {"escalate": "escalate", "respond": "respond"})

Compiling & running

from langstitch import LangStitchApp

app = LangStitchApp.bootstrap()
graph = app.build_graph()           # compiles to a LangGraph StateGraph (needs [graph])
result = app.invoke({"messages": [{"role": "user", "content": "hi"}]})

graph extra required

GraphBuilder.compile() / build_graph() import LangGraph lazily. Install the graph extra (pip install "langstitch-sdk[graph]") to compile to a real StateGraph; without it the core still imports for tooling and codegen.