Skip to content

Commit ab0f0a8

Browse files
committed
chore: unify python examples
1 parent 754e575 commit ab0f0a8

File tree

23 files changed

+215
-6792
lines changed

23 files changed

+215
-6792
lines changed
File renamed without changes.

typescript-sdk/integrations/langgraph/examples/python/agents/agentic_chat/agent.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
A simple agentic chat flow using LangGraph instead of CrewAI.
33
"""
44

5-
from typing import Dict, List, Any, Optional
5+
from typing import List, Any
6+
import os
67

78
# Updated imports for LangGraph
89
from langchain_core.runnables import RunnableConfig
@@ -76,5 +77,19 @@ async def chat_node(state: AgentState, config: RunnableConfig):
7677
workflow.add_edge(START, "chat_node")
7778
workflow.add_edge("chat_node", END)
7879

80+
# Conditionally use a checkpointer based on the environment
81+
# Check for multiple indicators that we're running in LangGraph dev/API mode
82+
is_langgraph_api = (
83+
os.environ.get("LANGGRAPH_API", "false").lower() == "true" or
84+
os.environ.get("LANGGRAPH_API_DIR") is not None
85+
)
86+
7987
# Compile the graph
80-
agentic_chat_graph = workflow.compile()
88+
if is_langgraph_api:
89+
# When running in LangGraph API/dev, don't use a custom checkpointer
90+
graph = workflow.compile()
91+
else:
92+
# For CopilotKit and other contexts, use MemorySaver
93+
from langgraph.checkpoint.memory import MemorySaver
94+
memory = MemorySaver()
95+
graph = workflow.compile(checkpointer=memory)

typescript-sdk/integrations/langgraph/examples/python/agents/agentic_generative_ui/agent.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
An example demonstrating agentic generative UI using LangGraph.
33
"""
44

5-
import json
65
import asyncio
7-
from typing import Dict, List, Any, Optional, Literal
6+
from typing import List, Any
7+
import os
8+
89
# LangGraph imports
910
from langchain_core.runnables import RunnableConfig
1011
from langgraph.graph import StateGraph, END, START
@@ -189,5 +190,19 @@ async def chat_node(state: AgentState, config: RunnableConfig):
189190
workflow.add_edge("start_flow", "chat_node")
190191
workflow.add_edge("chat_node", END)
191192

193+
# Conditionally use a checkpointer based on the environment
194+
# Check for multiple indicators that we're running in LangGraph dev/API mode
195+
is_langgraph_api = (
196+
os.environ.get("LANGGRAPH_API", "false").lower() == "true" or
197+
os.environ.get("LANGGRAPH_API_DIR") is not None
198+
)
199+
192200
# Compile the graph
193-
graph = workflow.compile()
201+
if is_langgraph_api:
202+
# When running in LangGraph API/dev, don't use a custom checkpointer
203+
graph = workflow.compile()
204+
else:
205+
# For CopilotKit and other contexts, use MemorySaver
206+
from langgraph.checkpoint.memory import MemorySaver
207+
memory = MemorySaver()
208+
graph = workflow.compile(checkpointer=memory)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
load_dotenv()
77

88
from ag_ui_langgraph import LangGraphAgent, add_langgraph_fastapi_endpoint
9-
from .human_in_the_loop import human_in_the_loop_graph
10-
from .predictive_state_updates import predictive_state_updates_graph
11-
from .shared_state import shared_state_graph
12-
from .tool_based_generative_ui import tool_based_generative_ui_graph
13-
from .agentic_chat import agentic_chat_graph
14-
from .agentic_generative_ui import graph
9+
from .human_in_the_loop.agent import graph as human_in_the_loop_graph
10+
from .predictive_state_updates.agent import graph as predictive_state_updates_graph
11+
from .shared_state.agent import graph as shared_state_graph
12+
from .tool_based_generative_ui.agent import graph as tool_based_generative_ui_graph
13+
from .agentic_chat.agent import graph as agentic_chat_graph
14+
from .agentic_generative_ui.agent import graph as agentic_generative_ui_graph
1515

1616
app = FastAPI(title="LangGraph Dojo Example Server")
1717

@@ -30,7 +30,7 @@
3030
"agentic_generative_ui": LangGraphAgent(
3131
name="agentic_generative_ui",
3232
description="An example for an agentic generative UI flow.",
33-
graph=graph,
33+
graph=agentic_generative_ui_graph,
3434
),
3535
"human_in_the_loop": LangGraphAgent(
3636
name="human_in_the_loop",

typescript-sdk/integrations/langgraph/examples/python/agents/human_in_the_loop/agent.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import json
66
from typing import Dict, List, Any
7+
import os
78

89
# LangGraph imports
910
from langchain_core.runnables import RunnableConfig
@@ -269,5 +270,19 @@ def should_continue(command: Command):
269270
},
270271
)
271272

273+
# Conditionally use a checkpointer based on the environment
274+
# Check for multiple indicators that we're running in LangGraph dev/API mode
275+
is_langgraph_api = (
276+
os.environ.get("LANGGRAPH_API", "false").lower() == "true" or
277+
os.environ.get("LANGGRAPH_API_DIR") is not None
278+
)
279+
272280
# Compile the graph
273-
human_in_the_loop_graph = workflow.compile()
281+
if is_langgraph_api:
282+
# When running in LangGraph API/dev, don't use a custom checkpointer
283+
graph = workflow.compile()
284+
else:
285+
# For CopilotKit and other contexts, use MemorySaver
286+
from langgraph.checkpoint.memory import MemorySaver
287+
memory = MemorySaver()
288+
graph = workflow.compile(checkpointer=memory)

typescript-sdk/integrations/langgraph/examples/python/agents/predictive_state_updates/agent.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
A demo of predictive state updates using LangGraph.
33
"""
44

5-
import json
65
import uuid
7-
from typing import Dict, List, Any, Optional
6+
from typing import List, Any, Optional
7+
import os
88

99
# LangGraph imports
1010
from langchain_core.runnables import RunnableConfig
@@ -174,5 +174,19 @@ async def chat_node(state: AgentState, config: RunnableConfig):
174174
workflow.add_edge("start_flow", "chat_node")
175175
workflow.add_edge("chat_node", END)
176176

177+
# Conditionally use a checkpointer based on the environment
178+
# Check for multiple indicators that we're running in LangGraph dev/API mode
179+
is_langgraph_api = (
180+
os.environ.get("LANGGRAPH_API", "false").lower() == "true" or
181+
os.environ.get("LANGGRAPH_API_DIR") is not None
182+
)
183+
177184
# Compile the graph
178-
predictive_state_updates_graph = workflow.compile()
185+
if is_langgraph_api:
186+
# When running in LangGraph API/dev, don't use a custom checkpointer
187+
graph = workflow.compile()
188+
else:
189+
# For CopilotKit and other contexts, use MemorySaver
190+
from langgraph.checkpoint.memory import MemorySaver
191+
memory = MemorySaver()
192+
graph = workflow.compile(checkpointer=memory)

typescript-sdk/integrations/langgraph/examples/python/agents/shared_state/agent.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
from enum import Enum
77
from typing import Dict, List, Any, Optional
8+
import os
89

910
# LangGraph imports
1011
from langchain_core.runnables import RunnableConfig
@@ -297,5 +298,19 @@ async def chat_node(state: Dict[str, Any], config: RunnableConfig):
297298
workflow.add_edge("start_flow", "chat_node")
298299
workflow.add_edge("chat_node", END)
299300

301+
# Conditionally use a checkpointer based on the environment
302+
# Check for multiple indicators that we're running in LangGraph dev/API mode
303+
is_langgraph_api = (
304+
os.environ.get("LANGGRAPH_API", "false").lower() == "true" or
305+
os.environ.get("LANGGRAPH_API_DIR") is not None
306+
)
307+
300308
# Compile the graph
301-
shared_state_graph = workflow.compile()
309+
if is_langgraph_api:
310+
# When running in LangGraph API/dev, don't use a custom checkpointer
311+
graph = workflow.compile()
312+
else:
313+
# For CopilotKit and other contexts, use MemorySaver
314+
from langgraph.checkpoint.memory import MemorySaver
315+
memory = MemorySaver()
316+
graph = workflow.compile(checkpointer=memory)

typescript-sdk/integrations/langgraph/examples/python/agents/tool_based_generative_ui/agent.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
An example demonstrating tool-based generative UI using LangGraph.
33
"""
44

5-
from typing import Dict, List, Any, Optional
5+
from typing import List, Any
6+
import os
67

78
# LangGraph imports
89
from langchain_core.runnables import RunnableConfig
@@ -124,6 +125,20 @@ async def chat_node(state: AgentState, config: RunnableConfig):
124125
workflow.add_edge(START, "chat_node")
125126
workflow.add_edge("chat_node", END)
126127

128+
# Conditionally use a checkpointer based on the environment
129+
# Check for multiple indicators that we're running in LangGraph dev/API mode
130+
is_langgraph_api = (
131+
os.environ.get("LANGGRAPH_API", "false").lower() == "true" or
132+
os.environ.get("LANGGRAPH_API_DIR") is not None
133+
)
134+
127135
# Compile the graph
128-
tool_based_generative_ui_graph = workflow.compile()
136+
if is_langgraph_api:
137+
# When running in LangGraph API/dev, don't use a custom checkpointer
138+
graph = workflow.compile()
139+
else:
140+
# For CopilotKit and other contexts, use MemorySaver
141+
from langgraph.checkpoint.memory import MemorySaver
142+
memory = MemorySaver()
143+
graph = workflow.compile(checkpointer=memory)
129144

typescript-sdk/integrations/langgraph/examples/python/langgraph.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"dockerfile_lines": [],
44
"dependencies": ["."],
55
"graphs": {
6-
"agentic_chat": "./agents/agentic_chat/agent.py:agentic_chat_graph",
6+
"agentic_chat": "./agents/agentic_chat/agent.py:graph",
77
"agentic_generative_ui": "./agents/agentic_generative_ui/agent.py:graph",
8-
"human_in_the_loop": "./agents/human_in_the_loop/agent.py:human_in_the_loop_graph",
9-
"predictive_state_updates": "./agents/predictive_state_updates/agent.py:predictive_state_updates_graph",
10-
"shared_state": "./agents/shared_state/agent.py:shared_state_graph",
11-
"tool_based_generative_ui": "./agents/tool_based_generative_ui/agent.py:tool_based_generative_ui_graph"
8+
"human_in_the_loop": "./agents/human_in_the_loop/agent.py:graph",
9+
"predictive_state_updates": "./agents/predictive_state_updates/agent.py:graph",
10+
"shared_state": "./agents/shared_state/agent.py:graph",
11+
"tool_based_generative_ui": "./agents/tool_based_generative_ui/agent.py:graph"
1212
},
1313
"env": ".env"
1414
}

0 commit comments

Comments
 (0)