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.
context(
self,
fn: types.ContextHandler,
) -> types.ContextHandlerExample:
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,
}| Name | Type | Description |
|---|---|---|
fn* | types.ContextHandler | The context handler function |