diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 3ae7112f3e..c559932bcd 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -765,15 +765,20 @@ class ChatTable: ) elif dialect_name == "postgresql": - # PostgreSQL relies on proper JSON query for search + # PostgreSQL doesn't allow null bytes in text. We filter those out by checking + # the JSON representation for \u0000 before attempting text extraction postgres_content_sql = ( "EXISTS (" " SELECT 1 " " FROM json_array_elements(Chat.chat->'messages') AS message " - " WHERE LOWER(message->>'content') LIKE '%' || :content_key || '%'" + " WHERE message->'content' IS NOT NULL " + " AND (message->'content')::text NOT LIKE '%\\u0000%' " + " AND LOWER(message->>'content') LIKE '%' || :content_key || '%'" ")" ) postgres_content_clause = text(postgres_content_sql) + # Also filter out chats with null bytes in title + query = query.filter(text("Chat.title::text NOT LIKE '%\\x00%'")) query = query.filter( or_( Chat.title.ilike(bindparam("title_key")),