Files
langgraph/libs/sdk-py/tests/test_encryption.py
Connor BraaandGitHub 9eac53db4b fix(sdk-py): remove model-type specific custom json encryption annotations & document key preservation limitations (#6595)
In langgraph-api, custom-encrypted JSONs need to continue to be
SQL-json-mergable after encryption. Previous WIP docs
advocated for custom encryption impls where all encrypted kv pairs were
shoved into a `__encrypted__: $encrypted_kvs` meta-key. Turns out that
pattern causes data loss when running PATCH-style partial updates or in
the many places langgraph-api json-SQL-merges across model types.

This PR contains 2 SDK fixes:
1. remove model-type specific custom json encryption annotations - these
cause surprising behavior as config and context data propagates across
model types, specifically because today we can't guarantee that data
encrypted as one model-type will be decrypted as the same model-type
because kv pairs move across model-types in pure SQL
2. document limitations and validation around "key preservation" in
custom json encryption functions. langgraph-api now validates that
custom JSON encryption fns don't change keys. That validation prevents
customizers from writing custom encryption functions that cause data
loss through patch endpoints and x-model merge propagation.

---------

Signed-off-by: Connor Braa <cwlbraa@langchain.dev>
2025-12-18 08:38:01 -08:00

73 lines
2.0 KiB
Python

import pytest
from langgraph_sdk.encryption import DuplicateHandlerError, Encryption
class TestHandlerValidation:
"""Test duplicate handler and signature validation."""
def test_duplicate_handlers_raise_error(self):
"""Registering the same handler type twice raises DuplicateHandlerError."""
encryption = Encryption()
@encryption.encrypt.blob
async def blob_enc(_ctx, data):
return data
@encryption.decrypt.blob
async def blob_dec(_ctx, data):
return data
@encryption.encrypt.json
async def json_enc(_ctx, data):
return data
@encryption.decrypt.json
async def json_dec(_ctx, data):
return data
# All duplicates should raise
with pytest.raises(DuplicateHandlerError):
@encryption.encrypt.blob
async def dup(_ctx, data):
return data
with pytest.raises(DuplicateHandlerError):
@encryption.decrypt.blob
async def dup(_ctx, data):
return data
with pytest.raises(DuplicateHandlerError):
@encryption.encrypt.json
async def dup(_ctx, data):
return data
with pytest.raises(DuplicateHandlerError):
@encryption.decrypt.json
async def dup(_ctx, data):
return data
def test_handlers_must_be_async(self):
"""Sync functions raise TypeError."""
encryption = Encryption()
with pytest.raises(TypeError, match="must be an async function"):
@encryption.encrypt.blob
def sync_handler(_ctx, data):
return data
def test_handlers_must_have_two_params(self):
"""Wrong parameter count raises TypeError."""
encryption = Encryption()
with pytest.raises(TypeError, match="must accept exactly 2 parameters"):
@encryption.encrypt.blob # type: ignore[arg-type]
async def wrong_params(ctx):
return ctx