Skip to content

invoke() and ainvoke()

invoke() runs a graph synchronously; ainvoke() runs it through Python’s async interface. Both normally return the final state after the graph stops.1

# Synchronous application
result = graph.invoke({"question": "Where is order A-104?"})
# Inside an async application such as FastAPI
result = await graph.ainvoke({"question": "Where is order A-104?"})
Application Normal choice
Simple script or sync worker invoke()
FastAPI async route await graph.ainvoke(...)
Need progress while running stream() or astream()

await belongs to Python. LangGraph supplies the awaitable ainvoke() method.

Use async nodes for async I/O and call the graph with ainvoke() or astream(). Do not hide blocking I/O or time.sleep() inside an async node; it blocks the event loop.

  1. LangChain, Use the graph API — Async.