fix(python): normalize doc numpy vectors - #653
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aligns Python Doc construction with the documented vector normalization behavior by ensuring numpy vectors are consistently converted into JSON-serializable Python types regardless of which construction path is used.
Changes:
- Route
Doc.__init__vector handling through a shared_normalize_vectors()helper to convertnumpy.ndarrayvalues viatolist(). - Reuse the same normalization helper in
Doc._from_tuple()to avoid divergent behavior between initialization paths. - Add a regression test to ensure
repr(doc)remains JSON-serializable whenDoc(...)is constructed directly with numpy vectors.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/zvec/model/doc.py | Centralizes numpy vector normalization for both __init__ and _from_tuple() to keep Doc.vectors JSON-safe. |
| python/tests/test_doc.py | Adds a regression test asserting direct Doc(...) construction with numpy vectors normalizes correctly and keeps repr() JSON-decodable. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Updated this branch with the latest upstream main after the linux-x64 run hit an unrelated native HNSW/RaBitQ test failure. The PR still only changes the Python Doc normalization path and its focused regression test. Local focused checks continue to pass: ruff check, ruff format --check, and py_compile for the changed files. |
Summary
This makes direct Doc(...) construction follow the documented vector normalization behavior.
Previously, Doc._from_tuple() converted numpy vector values to plain Python lists, but Doc.init kept numpy.ndarray values unchanged. That could make repr(doc) fail JSON serialization and return the fallback error string for documents constructed directly with numpy vectors.
This routes both initialization paths through the same vector normalization helper and adds a regression test covering direct Doc(...) construction with a numpy vector.
Tests
Note: focused pytest against the local source tree is blocked in this workspace because zvec._zvec is not built locally.