fix(rag): guard against None embeddings in LlamaIndex pipeline#347
Merged
fix(rag): guard against None embeddings in LlamaIndex pipeline#347
Conversation
…#346) When an embedding provider returns null for a chunk's embedding vector, the None value gets stored in the vector index and causes a TypeError in LlamaIndex's similarity computation (np.dot with NoneType). Two-layer fix: 1. _extract_embeddings_from_response: use 'or []' instead of get(key, default) so explicit None values are caught (get() only uses the default when the key is absent, not when it's None). 2. CustomEmbedding._get_text_embeddings: validate the batch result and replace any None vectors with zero vectors, logging an error to surface the upstream issue. Closes HKUDS#346
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.
Summary
Fixes #346 — RAG queries crash with
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'when a stored embedding vector isNone.Root Cause
When an embedding provider returns
{"embedding": null}for a chunk, two things go wrong:_extract_embeddings_from_responseusesitem.get("embedding", [])— butdict.get()only returns the default when the key is absent, not when the value is explicitlyNone. SoNonepasses through.CustomEmbedding._get_text_embeddingstrusts the result without validation, allowingNonevectors to be stored in the index and crashnp.dotat query time.Fix
Two-layer defense:
openai_compatible.py): Changeditem.get("embedding", [])→item.get("embedding") or []so explicitNonevalues are caught.llamaindex.py): Added post-embed validation in_get_text_embeddings— anyNonevectors are replaced with zero vectors and logged as errors. This prevents silent storage corruption regardless of which adapter is used.Testing
Noneembedding extraction intest_extract_embeddings.py