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
Tiny example
Section titled “Tiny example”# Synchronous applicationresult = graph.invoke({"question": "Where is order A-104?"})
# Inside an async application such as FastAPIresult = 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.
Failure note
Section titled “Failure note”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.
Related
Section titled “Related”Footnotes
Section titled “Footnotes”-
LangChain, Use the graph API — Async. ↩