LangGraph SDK

0.3.12 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an asynchronous LangGraph client, connect to a running LangGraph API server (which can be local or remote), and then list the deployed assistants. It highlights the use of `get_client` and retrieving basic information about agents.

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())

view raw JSON →