From b83e7c1cf9aa5806acfb35154ea04ce29c53fd90 Mon Sep 17 00:00:00 2001 From: cthomas Date: Tue, 30 Dec 2025 10:02:37 -0800 Subject: [PATCH] fix: sanitize null bytes before persisting in db [LET-6710] (#8147) fix: sanitize null bytes before persisting in db --- letta/services/passage_manager.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/letta/services/passage_manager.py b/letta/services/passage_manager.py index 6616671c..8f8f2994 100644 --- a/letta/services/passage_manager.py +++ b/letta/services/passage_manager.py @@ -167,9 +167,15 @@ class PassageManager: if np_embedding.shape[0] != MAX_EMBEDDING_DIM: embedding = np.pad(np_embedding, (0, MAX_EMBEDDING_DIM - np_embedding.shape[0]), mode="constant").tolist() + # Sanitize text to remove null bytes which PostgreSQL rejects + text = data["text"] + if text and "\x00" in text: + text = text.replace("\x00", "") + logger.warning(f"Removed null bytes from passage text (length: {len(data['text'])} -> {len(text)})") + common_fields = { "id": data.get("id"), - "text": data["text"], + "text": text, "embedding": embedding, "embedding_config": data["embedding_config"], "organization_id": data["organization_id"], @@ -228,9 +234,15 @@ class PassageManager: if np_embedding.shape[0] != MAX_EMBEDDING_DIM: embedding = np.pad(np_embedding, (0, MAX_EMBEDDING_DIM - np_embedding.shape[0]), mode="constant").tolist() + # Sanitize text to remove null bytes which PostgreSQL rejects + text = data["text"] + if text and "\x00" in text: + text = text.replace("\x00", "") + logger.warning(f"Removed null bytes from passage text (length: {len(data['text'])} -> {len(text)})") + common_fields = { "id": data.get("id"), - "text": data["text"], + "text": text, "embedding": embedding, "embedding_config": data["embedding_config"], "organization_id": data["organization_id"],