Replies: 2 comments 9 replies
-
I'm also interested in this functionality. I've opened another discussion here with some reproducible code. |
Beta Was this translation helpful? Give feedback.
5 replies
-
I've came up to the following solution pretty similar to the davids's solution. I have a FastAPI server with LangGraph agent. The full version is here https://github.com/akveo/ai-cookbook/blob/main/agent/app/server.py from typing import AsyncGenerator, Dict
import asyncio
# Track active connections
active_connections: Dict[str, asyncio.Event] = {}
@app.post("/agent/stop")
async def stop_agent(request: Request):
"""Endpoint for stopping the running agent."""
if thread_id in active_connections:
active_connections[thread_id].set()
@app.post("/agent")
async def agent(request: Request):
stop_event = asyncio.Event()
active_connections[thread_id] = stop_event
async def generate_events() -> AsyncGenerator[dict, None]:
try:
async for chunk in graph.astream(
input,
config,
stream_mode=["debug", "messages", "updates", "custom"],
):
if stop_event.is_set():
break
yield {}
finally:
if thread_id in active_connections:
del active_connections[thread_id]
return EventSourceResponse(generate_events()) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What is the correct way to stop (abort, terminate) graph execution at a specific point in time? It is a common scenario in applications when a user wants to stop generation and regenerate a response later. I haven't found any solution for LangGraph in Python. The only information I found is the implementation for the JS version.
Beta Was this translation helpful? Give feedback.
All reactions