LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraph-sdkencryptionEncryptioncontext
    Method●Since v0.2

    context

    Copy
    context(
        self,
        fn: types.ContextHandler,
    ) -> types.ContextHandler
    View source on GitHub

    Parameters

    NameTypeDescription
    fn*types.ContextHandler

    The context handler function

    Register a context handler to derive encryption context from auth.

    The handler receives the authenticated user and current EncryptionContext, and returns a dict that becomes ctx.metadata for encrypt/decrypt handlers.

    This allows encryption context to be derived from JWT claims or other auth-derived data instead of requiring a separate X-Encryption-Context header.

    Note: The context handler is called once per request in middleware, so ctx.model and ctx.field will be None in the handler.

    Example:

    from langgraph_sdk import Encryption, EncryptionContext
    from starlette.authentication import BaseUser
    
    encryption = Encryption()
    
    @encryption.context
    async def get_context(user: BaseUser, ctx: EncryptionContext) -> dict:
        # Derive encryption context from authenticated user
        return {
            **ctx.metadata,  # preserve X-Encryption-Context header if present
            "tenant_id": user.tenant_id,
        }