3434 SearchResult ,
3535 GetResult ,
3636)
37+ from open_webui .utils .misc import sanitize_text_for_db
3738from open_webui .config import (
3839 PGVECTOR_DB_URL ,
3940 PGVECTOR_INITIALIZE_MAX_VECTOR_LENGTH ,
@@ -289,7 +290,9 @@ def insert(self, collection_name: str, items: List[VectorItem]) -> None:
289290 vector = self .adjust_vector_length (item ['vector' ])
290291 # Use raw SQL for BYTEA/pgcrypto
291292 # Ensure metadata is converted to its JSON text representation
292- json_metadata = json .dumps (item ['metadata' ])
293+ # Sanitize to strip null bytes / surrogates that PostgreSQL cannot store
294+ json_metadata = sanitize_text_for_db (json .dumps (item ['metadata' ]))
295+ item_text = sanitize_text_for_db (item ['text' ])
293296 self .session .execute (
294297 text ("""
295298 INSERT INTO document_chunk
@@ -305,7 +308,7 @@ def insert(self, collection_name: str, items: List[VectorItem]) -> None:
305308 'id' : item ['id' ],
306309 'vector' : vector ,
307310 'collection_name' : collection_name ,
308- 'text' : item [ 'text' ] ,
311+ 'text' : item_text ,
309312 'metadata_text' : json_metadata ,
310313 'key' : PGVECTOR_PGCRYPTO_KEY ,
311314 },
@@ -338,7 +341,9 @@ def upsert(self, collection_name: str, items: List[VectorItem]) -> None:
338341 if PGVECTOR_PGCRYPTO :
339342 for item in items :
340343 vector = self .adjust_vector_length (item ['vector' ])
341- json_metadata = json .dumps (item ['metadata' ])
344+ # Sanitize to strip null bytes / surrogates that PostgreSQL cannot store
345+ json_metadata = sanitize_text_for_db (json .dumps (item ['metadata' ]))
346+ item_text = sanitize_text_for_db (item ['text' ])
342347 self .session .execute (
343348 text ("""
344349 INSERT INTO document_chunk
@@ -358,7 +363,7 @@ def upsert(self, collection_name: str, items: List[VectorItem]) -> None:
358363 'id' : item ['id' ],
359364 'vector' : vector ,
360365 'collection_name' : collection_name ,
361- 'text' : item [ 'text' ] ,
366+ 'text' : item_text ,
362367 'metadata_text' : json_metadata ,
363368 'key' : PGVECTOR_PGCRYPTO_KEY ,
364369 },
0 commit comments