- 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
client-management.md docs/
1# Client Management23Core client creation and HTTP operations for connecting to LangGraph servers with automatic discovery, custom authentication, and connection management.45## Capabilities67### Client Factory Functions89Create async and sync clients for interacting with LangGraph servers, with automatic local server discovery and configurable connection settings.1011```python { .api }12from collections.abc import Mapping13from typing import Union, Optional14import httpx1516# Type aliases17TimeoutTypes = Union[18None,19float,20tuple[Optional[float], Optional[float]],21tuple[Optional[float], Optional[float], Optional[float], Optional[float]],22httpx.Timeout,23]2425def get_client(26*,27url: str | None = None,28api_key: str | None = None,29headers: Mapping[str, str] | None = None,30timeout: TimeoutTypes | None = None,31) -> LangGraphClient:32"""33Create an async LangGraph client.3435Args:36url: Base URL of the LangGraph API.37– If `None`, the client first attempts an in-process connection via ASGI transport.38If that fails, it falls back to `http://localhost:8123`.39api_key: API key for authentication. If omitted, the client reads from environment40variables in the following order:411. Function argument422. `LANGGRAPH_API_KEY`433. `LANGSMITH_API_KEY`444. `LANGCHAIN_API_KEY`45headers: Additional HTTP headers to include in requests. Merged with authentication headers.46timeout: HTTP timeout configuration. May be:47– `httpx.Timeout` instance48– float (total seconds)49– tuple of timeouts (connect, read, write, pool)5051Returns:52LangGraphClient: The top-level asynchronous client for accessing AssistantsClient,53ThreadsClient, RunsClient, and CronClient.54"""5556def get_sync_client(57*,58url: str | None = None,59api_key: str | None = None,60headers: Mapping[str, str] | None = None,61timeout: TimeoutTypes | None = None,62) -> SyncLangGraphClient:63"""64Create a sync LangGraph client.6566Args:67url: The URL of the LangGraph API.68api_key: The API key. If not provided, it will be read from the environment.69Precedence:701. explicit argument712. LANGGRAPH_API_KEY723. LANGSMITH_API_KEY734. LANGCHAIN_API_KEY74headers: Optional custom headers75timeout: Optional timeout configuration for the HTTP client.76Accepts an httpx.Timeout instance, a float (seconds), or a tuple of timeouts.77Tuple format is (connect, read, write, pool)7879Returns:80SyncLangGraphClient: The top-level synchronous client for accessing AssistantsClient,81ThreadsClient, RunsClient, and CronClient.82"""83```8485### Main Client Classes8687Primary client interfaces providing access to all LangGraph Platform resources through specialized resource clients.8889```python { .api }90class LangGraphClient:91"""92Async client for LangGraph Platform API.9394Attributes:95- assistants: AssistantsClient for managing AI assistants96- threads: ThreadsClient for managing conversation threads97- runs: RunsClient for executing workflows98- crons: CronClient for scheduled tasks99- store: StoreClient for persistent storage100"""101assistants: AssistantsClient102threads: ThreadsClient103runs: RunsClient104crons: CronClient105store: StoreClient106107async def aclose(self) -> None:108"""Close the HTTP client and cleanup resources."""109110class SyncLangGraphClient:111"""112Sync client for LangGraph Platform API.113114Attributes:115- assistants: SyncAssistantsClient for managing AI assistants116- threads: SyncThreadsClient for managing conversation threads117- runs: SyncRunsClient for executing workflows118- crons: SyncCronClient for scheduled tasks119- store: SyncStoreClient for persistent storage120"""121assistants: SyncAssistantsClient122threads: SyncThreadsClient123runs: SyncRunsClient124crons: SyncCronClient125store: SyncStoreClient126127def close(self) -> None:128"""Close the HTTP client and cleanup resources."""129```130131### HTTP Client Operations132133Low-level HTTP operations for direct API access when needed, supporting all standard HTTP methods with streaming capabilities.134135```python { .api }136from collections.abc import AsyncIterator, Iterator, Mapping, Callable137from typing import Any138from langgraph_sdk.schema import QueryParamTypes, StreamPart139import httpx140141class HttpClient:142"""Low-level HTTP client for direct API access."""143144async def get(145self,146path: str,147*,148params: QueryParamTypes | None = None,149headers: Mapping[str, str] | None = None,150on_response: Callable[[httpx.Response], None] | None = None,151) -> Any:152"""Send a GET request."""153154async def post(155self,156path: str,157*,158json: dict[str, Any] | list | None,159params: QueryParamTypes | None = None,160headers: Mapping[str, str] | None = None,161on_response: Callable[[httpx.Response], None] | None = None,162) -> Any:163"""Send a POST request."""164165async def put(166self,167path: str,168*,169json: dict,170params: QueryParamTypes | None = None,171headers: Mapping[str, str] | None = None,172on_response: Callable[[httpx.Response], None] | None = None,173) -> Any:174"""Send a PUT request."""175176async def patch(177self,178path: str,179*,180json: dict,181params: QueryParamTypes | None = None,182headers: Mapping[str, str] | None = None,183on_response: Callable[[httpx.Response], None] | None = None,184) -> Any:185"""Send a PATCH request."""186187async def delete(188self,189path: str,190*,191json: Any | None = None,192params: QueryParamTypes | None = None,193headers: Mapping[str, str] | None = None,194on_response: Callable[[httpx.Response], None] | None = None,195) -> None:196"""Send a DELETE request."""197198async def stream(199self,200path: str,201method: str,202*,203json: dict[str, Any] | None = None,204params: QueryParamTypes | None = None,205headers: Mapping[str, str] | None = None,206on_response: Callable[[httpx.Response], None] | None = None,207) -> AsyncIterator[StreamPart]:208"""Stream results using SSE."""209210class SyncHttpClient:211"""Sync HTTP client with identical methods but without async/await."""212213def get(214self,215path: str,216*,217params: QueryParamTypes | None = None,218headers: Mapping[str, str] | None = None,219on_response: Callable[[httpx.Response], None] | None = None,220) -> Any: ...221222def post(223self,224path: str,225*,226json: dict[str, Any] | list | None,227params: QueryParamTypes | None = None,228headers: Mapping[str, str] | None = None,229on_response: Callable[[httpx.Response], None] | None = None,230) -> Any: ...231232def put(233self,234path: str,235*,236json: dict,237params: QueryParamTypes | None = None,238headers: Mapping[str, str] | None = None,239on_response: Callable[[httpx.Response], None] | None = None,240) -> Any: ...241242def patch(243self,244path: str,245*,246json: dict,247params: QueryParamTypes | None = None,248headers: Mapping[str, str] | None = None,249on_response: Callable[[httpx.Response], None] | None = None,250) -> Any: ...251252def delete(253self,254path: str,255*,256json: Any | None = None,257params: QueryParamTypes | None = None,258headers: Mapping[str, str] | None = None,259on_response: Callable[[httpx.Response], None] | None = None,260) -> None: ...261262def stream(263self,264path: str,265method: str,266*,267json: dict[str, Any] | None = None,268params: QueryParamTypes | None = None,269headers: Mapping[str, str] | None = None,270on_response: Callable[[httpx.Response], None] | None = None,271) -> Iterator[StreamPart]: ...272```273274## Usage Examples275276### Basic Client Setup277278```python279from langgraph_sdk import get_client, get_sync_client280281# Async client with auto-discovery282async_client = await get_client()283284# Async client with specific server285async_client = await get_client(url="https://api.langgraph.com", api_key="your-api-key")286287# Sync client288sync_client = get_sync_client()289290# With custom headers291client = await get_client(292headers={"User-Agent": "MyApp/1.0"},293timeout=60.0294)295```296297### Resource Access298299```python300# Access different resource managers301assistants = await client.assistants.search()302threads = await client.threads.search()303runs = await client.runs.list(thread_id="thread-123")304305# Cleanup when done306await client.aclose()307```308309### Direct HTTP Operations310311```python312# Low-level API access313response = await client.http.get("/assistants", params={"limit": 20})314new_thread = await client.http.post("/threads", json={"metadata": {"user": "john"}})315316# Streaming317async for chunk in client.http.stream("/runs/stream", method="POST", json=run_data):318print(chunk)319```