Skip to content

fix(python): normalize doc numpy vectors - #653

Open
HosniBelfeki wants to merge 2 commits into
alibaba:mainfrom
HosniBelfeki:fix-python-doc-vector-normalization
Open

fix(python): normalize doc numpy vectors#653
HosniBelfeki wants to merge 2 commits into
alibaba:mainfrom
HosniBelfeki:fix-python-doc-vector-normalization

Conversation

@HosniBelfeki

Copy link
Copy Markdown
Contributor

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

  • python -m ruff check python/zvec/model/doc.py python/tests/test_doc.py
  • python -m ruff format --check python/zvec/model/doc.py python/tests/test_doc.py
  • python -m py_compile python/zvec/model/doc.py python/tests/test_doc.py

Note: focused pytest against the local source tree is blocked in this workspace because zvec._zvec is not built locally.

Copilot AI lite review requested due to automatic review settings August 4, 2026 22:26
@HosniBelfeki
HosniBelfeki requested a review from Cuiyus as a code owner August 4, 2026 22:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 convert numpy.ndarray values via tolist().
  • 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 when Doc(...) 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.

Copilot AI review requested due to automatic review settings August 5, 2026 06:54
@HosniBelfeki

Copy link
Copy Markdown
Contributor Author

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants