[PR #4434] [MERGED] Sanitize Metadata Before PG Vector Database Insertion #4621

Closed
opened 2026-02-22 18:36:12 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/4434
Author: @angelplusultra
Created: 9/24/2025
Status: Merged
Merged: 9/29/2025
Merged by: @timothycarambat

Base: masterHead: 4339-unsupported-unicode-escape-sequence


📝 Commits (4)

  • 4101afc Fix JSDOC for updateOrCreateCollection
  • 47c8679 Add sanitizeForJsonb method to PGVector for safe JSONB handling
  • 0eb23b0 Add unit tests for PGVector.sanitizeForJsonb method
  • 7d4a648 Merge branch 'master' into 4339-unsupported-unicode-escape-sequence

📊 Changes

2 files changed (+132 additions, -4 deletions)

View changed files

server/__tests__/utils/vectorDbProviders/pgvector/index.test.js (+76 -0)
📝 server/utils/vectorDbProviders/pgvector/index.js (+56 -4)

📄 Description

Pull Request Type

  • feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 🔨 chore
  • 📝 docs

Relevant Issues

resolves #4339

What is in this change?

This PR adds a JSONB-safe sanitizer to the PGVector provider to address “unsupported Unicode escape sequence” errors that occur when inserting metadata containing disallowed control characters (most notably the NUL character, \u0000). Certain sources (e.g., some PDFs) can produce chunks with these control characters in the extracted text, causing Postgres to reject the jsonb payload.

  • Adds sanitizeForJsonb which recursively removes C0 control characters from strings while preserving tabs/newlines/CR, and traverses arrays/objects deeply.
  • Applies the sanitizer to submission.metadata immediately before insert into the metadata jsonb column.
  • No changes to schemas or query behavior; only non-printable, disallowed control chars are stripped. Printable content remains intact.
  • Scope is limited to the PGVector provider; other vector DB providers are unaffected.

Additional Information

Developer Validations

  • I ran yarn lint from the root of the repo & committed changes
  • Relevant documentation has been updated
  • I have tested my code functionality
  • Docker build succeeds locally

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Mintplex-Labs/anything-llm/pull/4434 **Author:** [@angelplusultra](https://github.com/angelplusultra) **Created:** 9/24/2025 **Status:** ✅ Merged **Merged:** 9/29/2025 **Merged by:** [@timothycarambat](https://github.com/timothycarambat) **Base:** `master` ← **Head:** `4339-unsupported-unicode-escape-sequence` --- ### 📝 Commits (4) - [`4101afc`](https://github.com/Mintplex-Labs/anything-llm/commit/4101afc2c490fab03c6b2a9303b92f10d8772d83) Fix JSDOC for updateOrCreateCollection - [`47c8679`](https://github.com/Mintplex-Labs/anything-llm/commit/47c8679ca0874deea4de8be11aabeb3470e584e1) Add sanitizeForJsonb method to PGVector for safe JSONB handling - [`0eb23b0`](https://github.com/Mintplex-Labs/anything-llm/commit/0eb23b0b2c2077dd4e966c8698258dea87385f41) Add unit tests for PGVector.sanitizeForJsonb method - [`7d4a648`](https://github.com/Mintplex-Labs/anything-llm/commit/7d4a6485469263d237a8377c079a3a24c4818c25) Merge branch 'master' into 4339-unsupported-unicode-escape-sequence ### 📊 Changes **2 files changed** (+132 additions, -4 deletions) <details> <summary>View changed files</summary> ➕ `server/__tests__/utils/vectorDbProviders/pgvector/index.test.js` (+76 -0) 📝 `server/utils/vectorDbProviders/pgvector/index.js` (+56 -4) </details> ### 📄 Description ### Pull Request Type <!-- For change type, change [ ] to [x]. --> - [ ] ✨ feat - [x] 🐛 fix - [ ] ♻️ refactor - [ ] 💄 style - [ ] 🔨 chore - [ ] 📝 docs ### Relevant Issues <!-- Use "resolves #xxx" to auto resolve on merge. Otherwise, please use "connect #xxx" --> resolves #4339 ### What is in this change? <!-- Describe the changes in this PR that are impactful to the repo. --> This PR adds a JSONB-safe sanitizer to the PGVector provider to address “unsupported Unicode escape sequence” errors that occur when inserting metadata containing disallowed control characters (most notably the NUL character, \u0000). Certain sources (e.g., some PDFs) can produce chunks with these control characters in the extracted text, causing Postgres to reject the jsonb payload. - Adds sanitizeForJsonb which recursively removes C0 control characters from strings while preserving tabs/newlines/CR, and traverses arrays/objects deeply. - Applies the sanitizer to submission.metadata immediately before insert into the metadata jsonb column. - No changes to schemas or query behavior; only non-printable, disallowed control chars are stripped. Printable content remains intact. - Scope is limited to the PGVector provider; other vector DB providers are unaffected. ### Additional Information <!-- Add any other context about the Pull Request here that was not captured above. --> ### Developer Validations <!-- All of the applicable items should be checked. --> - [x] I ran `yarn lint` from the root of the repo & committed changes - [x] Relevant documentation has been updated - [x] I have tested my code functionality - [ ] Docker build succeeds locally --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-22 18:36:12 -05:00
yindo closed this issue 2026-02-22 18:36:12 -05:00
yindo changed title from [PR #4434] Sanitize Metadata Before PG Vector Database Insertion to [PR #4434] [MERGED] Sanitize Metadata Before PG Vector Database Insertion 2026-06-05 15:19:30 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#4621