LangGraph SDK
The LangGraph SDK (langgraph-sdk) is a Python client for interacting with the LangGraph API (also known as LangSmith Deployment REST API), allowing programmatic access to manage Assistants, Threads, Runs, and Cron jobs. Currently at version 0.3.12, it provides both synchronous and asynchronous clients, serving as a key interface for deploying and managing stateful AI agents orchestrated by the LangGraph framework. The library is part of the actively developed LangChain ecosystem.
Warnings
- gotcha The `langgraph-sdk` acts as a client and requires a separate, running LangGraph API server to operate. It does not embed the server. If `langgraph-cli` is used locally, the SDK defaults to `http://localhost:8123`, otherwise the server URL must be explicitly provided during client initialization.
- gotcha The primary `get_client()` function returns an asynchronous client. For synchronous operations, it is crucial to use `get_sync_client()` instead. Attempting to use the asynchronous client in a synchronous context without proper `await` calls or an event loop will lead to runtime errors or unexpected behavior.
- gotcha By default, `get_client()` (and `get_sync_client()`) attempts to load the API key from the `LANGGRAPH_API_KEY` environment variable. If the key is not set or you explicitly want to connect without an API key (e.g., for an unsecured local development server), you must pass `api_key=None` to the client constructor.
- breaking Recent updates to the underlying LangGraph Agent Server API (which the SDK interacts with) have changed header handling. Headers previously included automatically in runs must now be explicitly allowed by setting `configurable_headers` in the server's configuration. Additionally, the format of stream event IDs for resumable streams has changed to `ms-seq`, although backward compatibility is currently maintained for older formats. This may require adjustments if relying on specific header propagation or event ID parsing.
Install
-
pip install langgraph-sdk
Imports
- get_client
from langgraph_sdk import get_client
- get_sync_client
from langgraph_sdk import get_sync_client
Quickstart
import os
import asyncio
from langgraph_sdk import get_client
# Ensure the LangGraph API server is running and accessible.
# By default, it looks for a server at http://localhost:8123
# and LANGGRAPH_API_KEY in environment variables.
LANGGRAPH_SERVER_URL = os.environ.get('LANGGRAPH_SERVER_URL', 'http://localhost:8123')
LANGGRAPH_API_KEY = os.environ.get('LANGGRAPH_API_KEY', 'your_api_key_here') # Replace with actual key or set env var
async def main():
try:
client = get_client(url=LANGGRAPH_SERVER_URL, api_key=LANGGRAPH_API_KEY)
print(f"Connected to LangGraph server at: {LANGGRAPH_SERVER_URL}")
# List all assistants (agents/graphs deployed on the server)
assistants_page = await client.assistants.search(limit=10)
assistants = assistants_page.results # Access the results attribute
if assistants:
print(f"Found {len(assistants)} assistants:")
for assistant in assistants:
print(f" - ID: {assistant.assistant_id}, Name: {assistant.name}, Version: {assistant.public_version}")
# Example: Get details of the first assistant
first_assistant = await client.assistants.get(assistant_id=assistants[0].assistant_id)
print(f"\nDetails for first assistant ({first_assistant.name}):\n{first_assistant.model_dump_json(indent=2)}")
else:
print("No assistants found on the server.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure a LangGraph API server is running and accessible, and LANGGRAPH_API_KEY is correctly set.")
if __name__ == "__main__":
asyncio.run(main())