Skip to content

Commit a794814

Browse files
committed
chore(integrations/langgraph-fastapi): sync to langgraph-python reference demo
1 parent efb3766 commit a794814

6 files changed

Lines changed: 728 additions & 588 deletions

File tree

examples/integrations/langgraph-fastapi/agent/main.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import os
22
import warnings
3+
from pathlib import Path
34
from dotenv import load_dotenv
5+
6+
# Load .env from the demo project root (one level up from agent/) BEFORE
7+
# importing src.agent — that import constructs ChatOpenAI at module load,
8+
# which needs OPENAI_API_KEY in the environment already.
9+
_demo_root = Path(__file__).parent.parent
10+
for env_path in (_demo_root / ".env", Path(".env")):
11+
if env_path.is_file():
12+
load_dotenv(env_path)
13+
break
14+
else:
15+
load_dotenv()
16+
417
from fastapi import FastAPI
518
import uvicorn
619
from src.agent import graph
720
from copilotkit import LangGraphAGUIAgent
821
from ag_ui_langgraph import add_langgraph_fastapi_endpoint
9-
10-
_ = load_dotenv()
1122
app = FastAPI()
1223

1324

examples/integrations/langgraph-fastapi/agent/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies = [
1515
"fastapi>=0.115.5,<1.0.0",
1616
"uvicorn>=0.29.0,<1.0.0",
1717
"python-dotenv>=1.0.0,<2.0.0",
18-
"ag-ui-langgraph==0.0.22",
18+
"ag-ui-langgraph>=0.0.34",
19+
"ag-ui-protocol>=0.1.18",
1920
"pydantic>=2.0.0,<3.0.0",
2021
]

examples/integrations/langgraph-fastapi/agent/src/agent.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from copilotkit import CopilotKitMiddleware, StateStreamingMiddleware, StateItem
99
from langchain.agents import create_agent
1010
from langchain_openai import ChatOpenAI
11+
from langgraph.checkpoint.memory import MemorySaver
1112

1213
from src.a2ui_dynamic_schema import generate_a2ui
1314
from src.a2ui_fixed_schema import search_flights
@@ -16,6 +17,10 @@
1617

1718
model = ChatOpenAI(model="gpt-5.4", model_kwargs={"parallel_tool_calls": False})
1819

20+
# FastAPI-specific: langgraph-cli dev supplies its own checkpointer in the
21+
# reference demo. Here we run under uvicorn + ag-ui-langgraph, so the graph
22+
# needs an explicit MemorySaver for state/thread persistence within a
23+
# process.
1924
agent = create_agent(
2025
model=model,
2126
tools=[query_data, *todo_tools, generate_a2ui, search_flights],
@@ -26,6 +31,7 @@
2631
),
2732
],
2833
state_schema=AgentState,
34+
checkpointer=MemorySaver(),
2935
system_prompt="""
3036
You are a polished, professional demo assistant. Keep responses to 1-2 sentences.
3137

0 commit comments

Comments
 (0)