From 186d045947aca116de9eb01bdb82b487689b189b Mon Sep 17 00:00:00 2001 From: Sydney Runkle Date: Tue, 21 Apr 2026 12:56:32 -0400 Subject: [PATCH] perf(sdk): lazy-load langgraph_sdk top-level exports Converting __init__.py to use __getattr__ defers loading of Auth, get_client/get_sync_client, Encryption, and EncryptionContext until first access. This removes ~13ms from langgraph.runtime import time, since importing BaseUser from langgraph_sdk.auth.types no longer triggers the full client/auth/encryption module graph. Co-Authored-By: Claude Sonnet 4.6 --- libs/sdk-py/langgraph_sdk/__init__.py | 30 +++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/libs/sdk-py/langgraph_sdk/__init__.py b/libs/sdk-py/langgraph_sdk/__init__.py index 25ef2935f..d8e8611db 100644 --- a/libs/sdk-py/langgraph_sdk/__init__.py +++ b/libs/sdk-py/langgraph_sdk/__init__.py @@ -1,8 +1,30 @@ -from langgraph_sdk.auth import Auth -from langgraph_sdk.client import get_client, get_sync_client -from langgraph_sdk.encryption import Encryption -from langgraph_sdk.encryption.types import EncryptionContext +from __future__ import annotations + +import importlib +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from langgraph_sdk.auth import Auth + from langgraph_sdk.client import get_client, get_sync_client + from langgraph_sdk.encryption import Encryption + from langgraph_sdk.encryption.types import EncryptionContext __version__ = "0.3.13" __all__ = ["Auth", "Encryption", "EncryptionContext", "get_client", "get_sync_client"] + +_LAZY: dict[str, str] = { + "Auth": "langgraph_sdk.auth", + "get_client": "langgraph_sdk.client", + "get_sync_client": "langgraph_sdk.client", + "Encryption": "langgraph_sdk.encryption", + "EncryptionContext": "langgraph_sdk.encryption.types", +} + + +def __getattr__(name: str) -> object: + if name in _LAZY: + mod = importlib.import_module(_LAZY[name]) + return getattr(mod, name) + msg = f"module {__name__!r} has no attribute {name!r}" + raise AttributeError(msg)