Refact: Add Embedding Dimension Validation in EmbeddingFunc#2368
Merged
danielaskdd merged 2 commits intoHKUDS:mainfrom Nov 17, 2025
Merged
Refact: Add Embedding Dimension Validation in EmbeddingFunc#2368danielaskdd merged 2 commits intoHKUDS:mainfrom
danielaskdd merged 2 commits intoHKUDS:mainfrom
Conversation
• Validate total elements divisibility • Check vector count matches input count • Raise clear error messages on mismatch • Ensure embedding output correctness • Add docstring for EmbeddingFunc class
Collaborator
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 Add Embedding Dimension Validation in EmbeddingFunc
Problem Statement
When using custom OpenAI-compatible embedding endpoints (or other embedding providers), dimension mismatches between expected and actual embedding outputs could cause runtime errors at the storage layer. This was particularly problematic with Milvus, where errors like:
would occur only when data reached the vector database, making debugging difficult and potentially causing data corruption.
Root Cause
Embedding providers may return results in varying formats or with incorrect dimensions due to:
Previously, dimension validation only happened implicitly at the storage layer, meaning invalid embeddings could propagate through the system before being detected.
Solution
This PR implements centralized dimension validation in the
EmbeddingFuncclass (lightrag/utils.py), ensuring all embedding results are validated immediately after generation, before reaching any storage backend.Implementation Details
Validation Logic (in
EmbeddingFunc.__call__):Key Advantages
O(1)) instead of shape inspectionsChanges Made
Modified Files
lightrag/utils.pyEmbeddingFunc.__call__methodlightrag/kg/milvus_impl.pynp.concatenate()implementationBenefits
✅ Data Integrity - Prevents invalid embeddings from entering the system
✅ Better Debugging - Clear error messages at the point of failure
✅ Storage Agnostic - Protects all vector storage implementations
✅ Zero Performance Impact - Minimal overhead (single modulo check)
✅ Backward Compatible - No breaking changes to existing functionality
Testing Recommendations
Breaking Changes
None - This change is fully backward compatible. Valid embeddings continue to work as before; only invalid embeddings now fail with clearer error messages.
Migration Guide
No migration required. This change adds validation without modifying any APIs or data formats.
Example Error Output
Before this PR:
After this PR:
Much clearer! 🎉
Related Issues: #2365
Type: Bug Fix / Improvement
Component: Core - Embedding System