- Spec files
pypi-langgraph-sdk
Describes: pkg:pypi/langgraph-sdk@0.2.x
- Description
- Python SDK for interacting with the LangGraph Platform REST API to build and manage AI assistants and conversational workflows
- Author
- tessl
- Last updated
index.md docs/
1# LangGraph SDK23A comprehensive Python SDK for interacting with the LangGraph Platform REST API. The SDK enables developers to build and manage AI assistants and conversational workflows with async and sync client interfaces, automatic local server discovery, streaming support, and fine-grained authentication and authorization management.45## Package Information67- **Package Name**: langgraph-sdk8- **Language**: Python9- **Installation**: `pip install langgraph-sdk`1011## Core Imports1213```python14from langgraph_sdk import get_client, get_sync_client, Auth15```1617## Basic Usage1819```python20from langgraph_sdk import get_client2122# Connect to LangGraph server (auto-detects local server at localhost:8123)23client = await get_client()2425# List all assistants26assistants = await client.assistants.search()27agent = assistants[0]2829# Create a new conversation thread30thread = await client.threads.create()3132# Start a streaming run33input_data = {"messages": [{"role": "human", "content": "Hello!"}]}34async for chunk in client.runs.stream(35thread['thread_id'],36agent['assistant_id'],37input=input_data38):39print(chunk)4041# Close the client42await client.aclose()43```4445## Architecture4647The LangGraph SDK follows a resource-oriented design with distinct client managers:4849- **Client Factory**: `get_client()` and `get_sync_client()` create configured HTTP clients50- **Resource Clients**: Dedicated managers for assistants, threads, runs, crons, and store operations51- **Authentication System**: Pluggable auth handlers for custom security implementations52- **Streaming Support**: Server-sent events for real-time execution monitoring53- **Type System**: Comprehensive TypedDict schemas for all API interactions5455Both async and sync versions provide identical APIs, enabling integration into any Python application architecture.5657## Capabilities5859### Client Management6061Core client creation and HTTP operations for connecting to LangGraph servers with automatic discovery, custom authentication, and connection management.6263```python { .api }64from collections.abc import Mapping65from typing import Union, Optional66import httpx6768# Type aliases69TimeoutTypes = Union[70None,71float,72tuple[Optional[float], Optional[float]],73tuple[Optional[float], Optional[float], Optional[float], Optional[float]],74httpx.Timeout,75]7677def get_client(78*,79url: str | None = None,80api_key: str | None = None,81headers: Mapping[str, str] | None = None,82timeout: TimeoutTypes | None = None,83) -> LangGraphClient: ...8485def get_sync_client(86*,87url: str | None = None,88api_key: str | None = None,89headers: Mapping[str, str] | None = None,90timeout: TimeoutTypes | None = None,91) -> SyncLangGraphClient: ...92```9394[Client Management](./client-management.md)9596### Assistant Management9798Create, configure, and manage AI assistants based on registered graphs. Assistants serve as the execution engines for conversational workflows, with support for versioning, configuration, and metadata management.99100```python { .api }101from collections.abc import Mapping102from langgraph_sdk.schema import (103Assistant, AssistantSelectField, AssistantSortBy, SortOrder,104Config, Context, Json, OnConflictBehavior, QueryParamTypes105)106107# Via client.assistants108async def get(109assistant_id: str,110*,111headers: Mapping[str, str] | None = None,112params: QueryParamTypes | None = None,113) -> Assistant: ...114115async def create(116graph_id: str | None,117config: Config | None = None,118*,119context: Context | None = None,120metadata: Json = None,121assistant_id: str | None = None,122if_exists: OnConflictBehavior | None = None,123name: str | None = None,124headers: Mapping[str, str] | None = None,125params: QueryParamTypes | None = None,126) -> Assistant: ...127128async def update(129assistant_id: str,130*,131graph_id: str | None = None,132config: Config | None = None,133context: Context | None = None,134metadata: Json = None,135name: str | None = None,136headers: Mapping[str, str] | None = None,137description: str | None = None,138params: QueryParamTypes | None = None,139) -> Assistant: ...140141async def search(142*,143metadata: Json = None,144graph_id: str | None = None,145limit: int = 10,146offset: int = 0,147sort_by: AssistantSortBy | None = None,148sort_order: SortOrder | None = None,149select: list[AssistantSelectField] | None = None,150headers: Mapping[str, str] | None = None,151params: QueryParamTypes | None = None,152) -> list[Assistant]: ...153```154155[Assistant Management](./assistant-management.md)156157### Thread Management158159Manage conversation threads that maintain state across multiple interactions. Threads provide isolation for conversations and support state inspection, updates, and history tracking.160161```python { .api }162from collections.abc import Mapping, Sequence163from typing import Any164from langgraph_sdk.schema import (165Thread, ThreadSelectField, ThreadSortBy, ThreadState, ThreadStatus,166ThreadUpdateStateResponse, Checkpoint, Json, OnConflictBehavior, QueryParamTypes167)168169# Via client.threads170async def create(171*,172metadata: Json = None,173thread_id: str | None = None,174if_exists: OnConflictBehavior | None = None,175supersteps: Sequence[dict[str, Sequence[dict[str, Any]]]] | None = None,176graph_id: str | None = None,177ttl: int | Mapping[str, Any] | None = None,178headers: Mapping[str, str] | None = None,179params: QueryParamTypes | None = None,180) -> Thread: ...181182async def get(183thread_id: str,184*,185select: list[ThreadSelectField] | None = None,186headers: Mapping[str, str] | None = None,187params: QueryParamTypes | None = None,188) -> Thread: ...189190async def update(191thread_id: str,192*,193metadata: Mapping[str, Any],194ttl: int | Mapping[str, Any] | None = None,195headers: Mapping[str, str] | None = None,196params: QueryParamTypes | None = None,197) -> Thread: ...198199async def search(200*,201metadata: Json = None,202values: Json = None,203ids: Sequence[str] | None = None,204status: ThreadStatus | None = None,205limit: int = 10,206offset: int = 0,207sort_by: ThreadSortBy | None = None,208sort_order: SortOrder | None = None,209select: list[ThreadSelectField] | None = None,210headers: Mapping[str, str] | None = None,211params: QueryParamTypes | None = None,212) -> list[Thread]: ...213214async def get_state(215thread_id: str,216*,217checkpoint: Checkpoint | None = None,218checkpoint_id: str | None = None, # deprecated219subgraphs: bool = False,220headers: Mapping[str, str] | None = None,221params: QueryParamTypes | None = None,222) -> ThreadState: ...223224async def update_state(225thread_id: str,226values: dict[str, Any] | Sequence[dict] | None,227*,228as_node: str | None = None,229checkpoint: Checkpoint | None = None,230checkpoint_id: str | None = None, # deprecated231headers: Mapping[str, str] | None = None,232params: QueryParamTypes | None = None,233) -> ThreadUpdateStateResponse: ...234```235236[Thread Management](./thread-management.md)237238### Run Execution239240Execute assistant workflows on threads with support for streaming, interrupts, configuration, and completion handling. Runs represent individual executions of an assistant on a thread.241242```python { .api }243from collections.abc import AsyncIterator, Mapping, Sequence244from typing import Any245from langgraph_sdk.schema import (246Run, StreamPart, StreamMode, Config, Context, Checkpoint,247Command, CancelAction, QueryParamTypes248)249250# Via client.runs251def stream(252thread_id: str | None,253assistant_id: str,254*,255input: Mapping[str, Any] | None = None,256command: Command | None = None,257stream_mode: StreamMode | Sequence[StreamMode] = "values",258stream_subgraphs: bool = False,259stream_resumable: bool = False,260metadata: Mapping[str, Any] | None = None,261config: Config | None = None,262context: Context | None = None,263checkpoint: Checkpoint | None = None,264checkpoint_id: str | None = None, # deprecated265webhook: str | None = None,266webhook_mode: str | None = None,267headers: Mapping[str, str] | None = None,268params: QueryParamTypes | None = None,269) -> AsyncIterator[StreamPart]: ...270271async def create(272thread_id: str | None,273assistant_id: str,274*,275input: Mapping[str, Any] | None = None,276command: Command | None = None,277stream_mode: StreamMode | Sequence[StreamMode] = "values",278stream_subgraphs: bool = False,279stream_resumable: bool = False,280metadata: Mapping[str, Any] | None = None,281config: Config | None = None,282context: Context | None = None,283checkpoint: Checkpoint | None = None,284checkpoint_id: str | None = None, # deprecated285webhook: str | None = None,286webhook_mode: str | None = None,287headers: Mapping[str, str] | None = None,288params: QueryParamTypes | None = None,289) -> Run: ...290291async def wait(292thread_id: str | None,293assistant_id: str,294*,295input: Mapping[str, Any] | None = None,296command: Command | None = None,297metadata: Mapping[str, Any] | None = None,298config: Config | None = None,299context: Context | None = None,300checkpoint: Checkpoint | None = None,301checkpoint_id: str | None = None, # deprecated302webhook: str | None = None,303webhook_mode: str | None = None,304checkpoint_during: bool | None = None,305headers: Mapping[str, str] | None = None,306params: QueryParamTypes | None = None,307) -> Run: ...308309async def cancel(310thread_id: str,311run_id: str,312*,313wait: bool = False,314action: CancelAction = "interrupt",315headers: Mapping[str, str] | None = None,316params: QueryParamTypes | None = None,317) -> None: ...318```319320[Run Execution](./run-execution.md)321322### Scheduled Tasks323324Create and manage cron jobs for automated execution of assistants on threads or with dynamic thread creation. Supports timezone handling, webhook notifications, and flexible scheduling.325326```python { .api }327from collections.abc import Mapping328from typing import Any329from langgraph_sdk.schema import (330Cron, CronSelectField, CronSortBy, SortOrder,331Config, Context, All, QueryParamTypes332)333334# Via client.crons335async def create(336assistant_id: str,337*,338schedule: str,339input: Mapping[str, Any] | None = None,340metadata: Mapping[str, Any] | None = None,341config: Config | None = None,342context: Context | None = None,343checkpoint_during: bool | None = None,344interrupt_before: All | list[str] | None = None,345interrupt_after: All | list[str] | None = None,346webhook: str | None = None,347webhook_mode: str | None = None,348headers: Mapping[str, str] | None = None,349params: QueryParamTypes | None = None,350) -> Cron: ...351352async def create_for_thread(353thread_id: str,354assistant_id: str,355*,356schedule: str,357input: Mapping[str, Any] | None = None,358metadata: Mapping[str, Any] | None = None,359config: Config | None = None,360context: Context | None = None,361checkpoint_during: bool | None = None,362interrupt_before: All | list[str] | None = None,363interrupt_after: All | list[str] | None = None,364webhook: str | None = None,365webhook_mode: str | None = None,366headers: Mapping[str, str] | None = None,367params: QueryParamTypes | None = None,368) -> Cron: ...369370async def search(371*,372assistant_id: str | None = None,373thread_id: str | None = None,374limit: int = 10,375offset: int = 0,376sort_by: CronSortBy | None = None,377sort_order: SortOrder | None = None,378select: list[CronSelectField] | None = None,379headers: Mapping[str, str] | None = None,380params: QueryParamTypes | None = None,381) -> list[Cron]: ...382383async def delete(384cron_id: str,385*,386headers: Mapping[str, str] | None = None,387params: QueryParamTypes | None = None,388) -> None: ...389```390391[Scheduled Tasks](./scheduled-tasks.md)392393### Persistent Storage394395Cross-thread persistent memory system for storing and retrieving documents, configuration, and application data with namespacing, search capabilities, and flexible data organization.396397```python { .api }398from collections.abc import Mapping, Sequence399from typing import Any, Literal400from langgraph_sdk.schema import (401Item, SearchItemsResponse, ListNamespaceResponse, QueryParamTypes402)403404# Via client.store405async def put_item(406namespace: Sequence[str],407/,408key: str,409value: Mapping[str, Any],410index: Literal[False] | list[str] | None = None,411ttl: int | None = None,412headers: Mapping[str, str] | None = None,413params: QueryParamTypes | None = None,414) -> None: ...415416async def get_item(417namespace: Sequence[str],418/,419key: str,420*,421refresh_ttl: bool | None = None,422headers: Mapping[str, str] | None = None,423params: QueryParamTypes | None = None,424) -> Item: ...425426async def search_items(427namespace_prefix: Sequence[str],428/,429filter: Mapping[str, Any] | None = None,430limit: int = 10,431offset: int = 0,432query: str | None = None,433refresh_ttl: bool | None = None,434headers: Mapping[str, str] | None = None,435params: QueryParamTypes | None = None,436) -> SearchItemsResponse: ...437438async def delete_item(439namespace: Sequence[str],440/,441key: str,442headers: Mapping[str, str] | None = None,443params: QueryParamTypes | None = None,444) -> None: ...445446async def list_namespaces(447*,448prefix: Sequence[str] | None = None,449suffix: Sequence[str] | None = None,450limit: int = 100,451offset: int = 0,452headers: Mapping[str, str] | None = None,453params: QueryParamTypes | None = None,454) -> ListNamespaceResponse: ...455```456457[Persistent Storage](./persistent-storage.md)458459### Authentication & Authorization460461Comprehensive authentication and authorization system supporting custom authentication handlers, fine-grained authorization rules, and flexible security policies for all resources and actions.462463```python { .api }464from typing import Callable, TypeVar465from collections.abc import Sequence466from langgraph_sdk.auth import types, exceptions467468TH = TypeVar("TH", bound=types.Handler)469AH = TypeVar("AH", bound=types.Authenticator)470471class Auth:472types = types473exceptions = exceptions474475def __init__(self) -> None:476self.on: _On = ... # Authorization handlers477478def authenticate(self, fn: AH) -> AH: ...479480# Authorization context classes481class _On:482assistants: _AssistantsOn483threads: _ThreadsOn484crons: _CronsOn485store: _StoreOn486value: type[dict[str, Any]]487488def __call__(489self,490fn: Callable | None = None,491*,492resources: str | Sequence[str] | None = None,493actions: str | Sequence[str] | None = None,494) -> Callable: ...495```496497[Authentication & Authorization](./authentication.md)498499## Core Types500501```python { .api }502# Client Types503class LangGraphClient:504assistants: AssistantsClient505threads: ThreadsClient506runs: RunsClient507crons: CronClient508store: StoreClient509async def aclose(self) -> None: ...510511class SyncLangGraphClient:512assistants: SyncAssistantsClient513threads: SyncThreadsClient514runs: SyncRunsClient515crons: SyncCronClient516store: SyncStoreClient517def close(self) -> None: ...518519# Core Data Models520class Assistant(TypedDict):521assistant_id: str522graph_id: str523config: Config524created_at: str525updated_at: str526metadata: dict527528class Thread(TypedDict):529thread_id: str530created_at: str531updated_at: str532metadata: dict533status: ThreadStatus534535class Run(TypedDict):536run_id: str537thread_id: str538assistant_id: str539created_at: str540updated_at: str541status: RunStatus542kwargs: dict543544class Cron(TypedDict):545cron_id: str546thread_id: str547assistant_id: str548schedule: str549timezone: str550created_at: str551552class Item(TypedDict):553namespace: list[str]554key: str555value: dict556created_at: str557updated_at: str558559# Status Types560RunStatus = Literal["pending", "running", "error", "success", "timeout", "interrupted"]561ThreadStatus = Literal["idle", "busy", "interrupted", "error"]562StreamMode = Literal["values", "messages", "updates", "events", "tasks", "checkpoints", "debug"]563564# Configuration Types565class Config(TypedDict, total=False):566tags: list[str]567recursion_limit: int568configurable: dict569```