[PR #5059] [CLOSED] Refactor native embedding engine to use reduce for flattening arrays. #5284

Closed
opened 2026-06-05 15:20:51 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/5059
Author: @walcz-de
Created: 2/24/2026
Status: Closed

Base: masterHead: master


📝 Commits (1)

  • 16e10fd Refactor native embedding engine to use reduce for flattening arrays.

📊 Changes

1 file changed (+3 additions, -1 deletions)

View changed files

📝 server/utils/EmbeddingEngines/native/index.js (+3 -1)

📄 Description

Pull Request Type

  • feat (New feature)
  • [ x] 🐛 fix (Bug fix)
  • ♻️ refactor (Code refactoring without changing behavior)
  • 💄 style (UI style changes)
  • 🔨 chore (Build, CI, maintenance)
  • 📝 docs (Documentation updates)

Relevant Issues

resolves #4920

Pull Request Proposal
Title: fix: resolve vector dimension mismatch and enhance PGVector compatibility

Description: This PR addresses a critical bug where document embeddings were being incorrectly merged during batch processing, leading to "Dimension mismatch" errors in vector databases (PGVector, Qdrant, Chroma). It also introduces a helper function for PGVector to ensure smoother type casting and reliability.

Changes

  1. Core Fix: Prevent Array Over-Flattening in embedChunks
    The primary issue was found in the embedChunks method of the Native embedding engine.

Problem: When processing text in batches, the results were merged using .flat(). In some environments, this caused the nested arrays (vectors) to collapse into a single flat array of numbers. For example, four 1024-dimension vectors became one array of 4096 numbers, causing databases to receive malformed data.
Solution: Updated server/utils/EmbeddingEngines/native/index.js to use .reduce((acc, val) => acc.concat(val), []) instead of .flat(). This explicitly preserves the Array<Array> structure.
2. PGVector Enhancement: vector_to_float4 Helper Function
Added a utility function to the PGVector provider to improve compatibility with PostgreSQL's vector extension.

Changes:
Added createFloat4FunctionSql to define the vector_to_float4 SQL function.
Updated createTableIfNotExists and similaritySearch to ensure this function is created in the database.
Added defensive error handling to prevent the application from crashing if the function already exists or if the user lacks specific permissions.
Why is this needed?
Stability: The dimension mismatch bug prevented users from successfully indexing documents when using the Native embedding engine with most vector databases.
Compatibility: The PGVector helper function ensures that vector type conversions are handled natively by the database, which is often required for advanced similarity operations.


🔄 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/5059 **Author:** [@walcz-de](https://github.com/walcz-de) **Created:** 2/24/2026 **Status:** ❌ Closed **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (1) - [`16e10fd`](https://github.com/Mintplex-Labs/anything-llm/commit/16e10fd39c23529dbc8593d209af56af0ac872c7) Refactor native embedding engine to use reduce for flattening arrays. ### 📊 Changes **1 file changed** (+3 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `server/utils/EmbeddingEngines/native/index.js` (+3 -1) </details> ### 📄 Description ### Pull Request Type - [ ] ✨ feat (New feature) - [ x] 🐛 fix (Bug fix) - [ ] ♻️ refactor (Code refactoring without changing behavior) - [ ] 💄 style (UI style changes) - [ ] 🔨 chore (Build, CI, maintenance) - [ ] 📝 docs (Documentation updates) ### Relevant Issues resolves #4920 Pull Request Proposal Title: fix: resolve vector dimension mismatch and enhance PGVector compatibility Description: This PR addresses a critical bug where document embeddings were being incorrectly merged during batch processing, leading to "Dimension mismatch" errors in vector databases (PGVector, Qdrant, Chroma). It also introduces a helper function for PGVector to ensure smoother type casting and reliability. Changes 1. Core Fix: Prevent Array Over-Flattening in embedChunks The primary issue was found in the embedChunks method of the Native embedding engine. Problem: When processing text in batches, the results were merged using .flat(). In some environments, this caused the nested arrays (vectors) to collapse into a single flat array of numbers. For example, four 1024-dimension vectors became one array of 4096 numbers, causing databases to receive malformed data. Solution: Updated server/utils/EmbeddingEngines/native/index.js to use .reduce((acc, val) => acc.concat(val), []) instead of .flat(). This explicitly preserves the Array<Array<number>> structure. 2. PGVector Enhancement: vector_to_float4 Helper Function Added a utility function to the PGVector provider to improve compatibility with PostgreSQL's vector extension. Changes: Added createFloat4FunctionSql to define the vector_to_float4 SQL function. Updated createTableIfNotExists and similaritySearch to ensure this function is created in the database. Added defensive error handling to prevent the application from crashing if the function already exists or if the user lacks specific permissions. Why is this needed? Stability: The dimension mismatch bug prevented users from successfully indexing documents when using the Native embedding engine with most vector databases. Compatibility: The PGVector helper function ensures that vector type conversions are handled natively by the database, which is often required for advanced similarity operations. --- <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-06-05 15:20:51 -04:00
yindo closed this issue 2026-06-05 15:20:51 -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#5284