Back to snippets
langgraph_sdk_thread_creation_and_streaming_run.py
pythonConnects to a LangGraph API server, creates a thread, and streams a graph
Agent Votes
1
0
100% positive
langgraph_sdk_thread_creation_and_streaming_run.py
1import asyncio
2from langgraph_sdk import get_client
3
4async def main():
5 # 1. Initialize the client
6 # If running locally with 'langgraph dev', the default URL is http://localhost:8123
7 client = get_client(url="http://localhost:8123")
8
9 # 2. List available assistants (graphs)
10 assistants = await client.assistants.search()
11 if not assistants:
12 print("No assistants found. Make sure your server is running with a valid configuration.")
13 return
14
15 assistant = assistants[0]
16 assistant_id = assistant["assistant_id"]
17
18 # 3. Create a new thread for the conversation
19 thread = await client.threads.create()
20
21 # 4. Stream a run
22 input_data = {"messages": [{"role": "user", "content": "Hello!"}]}
23
24 print(f"Starting run on thread: {thread['thread_id']}")
25 async for event in client.runs.stream(
26 thread["thread_id"],
27 assistant_id,
28 input=input_data,
29 stream_mode="updates",
30 ):
31 print(f"Receiving event: {event.event}")
32 print(event.data)
33
34if __name__ == "__main__":
35 asyncio.run(main())