[Bug]: query(include_vector=True) raises "Dimension not match:3 vs 11" on FlatIndex
Description
When calling collection.query(vectors=VectorQuery(...), topk=N, include_vector=True) on a freshly created collection with FlatIndex, zvec raises ValueError: Dimension not match:3 vs 11 (sometimes 4 vs 11, depending on build).
The same query with include_vector=False succeeds, but the stored vector data is not returned in the results (as expected — doc.vectors.get("field_name") returns None).
The number 11 appears to be a fixed format version stored in the collection file header during create_and_open(), while 3 (or 4) is the format version that the query code path expects. This mismatch occurs even on a brand-new collection created by the same zvec version, suggesting an internal version check inconsistency between the storage layer (writes format 11) and the query layer (reads expecting format 3).
This bug blocks the include_vector=True feature entirely, forcing users to either:
- Re-embed documents from text fields (slow for large collections)
- Use
include_vector=Falseand lose access to stored vectors The workaround currently used is to fall back toinclude_vector=Falseand re-compute embeddings from scalar text fields, but this is inefficient for projections and vector inspections.
Steps to Reproduce
## Steps to Reproduce
1. Install zvec==0.3.0 (or 0.3.1):
pip install zvec==0.3.0
2. Create a collection with FlatIndex on 384-dim vectors:
import zvec
from zvec import (
CollectionSchema, FieldSchema, VectorSchema,
DataType, FlatIndexParam, MetricType, VectorQuery,
)
schema = CollectionSchema(
name="documents",
fields=[
FieldSchema("doc_id", DataType.STRING, nullable=False),
FieldSchema("title", DataType.STRING, nullable=True),
FieldSchema("content", DataType.STRING, nullable=True),
],
vectors=[
VectorSchema(
"context_vec",
DataType.VECTOR_FP32,
dimension=384,
index_param=FlatIndexParam(metric_type=MetricType.COSINE),
),
],
)
collection = zvec.create_and_open("/tmp/test_collection", schema=schema)
3. Insert a document:
import numpy as np
vec = np.random.randn(384).astype(np.float32).tolist()
doc = zvec.Doc(
id="test_doc_1",
fields={"doc_id": "test_doc_1", "title": "test"},
vectors={"context_vec": vec},
)
collection.insert([doc])
collection.flush()
4. Query with include_vector=True:
vq = VectorQuery(field_name="context_vec", vector=[0.0] * 384)
results = collection.query(vectors=vq, topk=10, include_vector=True)
# ^^^ THIS FAILS with:
# ValueError: Dimension not match:X vs 11
5. Query with include_vector=False succeeds:
results = collection.query(vectors=vq, topk=10, include_vector=False)
# ^^^ WORKS, but doc.vectors.get("context_vec") returns None
Expected Behavior
query(include_vector=True) should return the stored vectors, as documented at https://zvec.org/api-reference/python/collection/
Actual Behavior
Raises ValueError: Dimension not match:3 vs 11 (or 4 vs 11, varying between builds). The number 11 appears to be a fixed file format version stored in the collection data, while 3/4 is the format version expected by the current zvec library.
Environment
- OS: Linux x86_64 (Docker container)
- Python: 3.11
- zvec version: 0.3.0 (also tested on 0.3.1 - same behavior)
- Index type: FlatIndex
- Vector dimension: 384
Logs / Stack Trace
ValueError: Dimension not match:3 vs 11
File "zvec/executor/query_executor.py", line 189, in _do_execute
docs = collection.Query(query)
Triggered by: collection.query(vectors=vq, topk=N, include_vector=True)
Reproduced on: zvec==0.3.0, fresh collection, FlatIndex, 384d
Operating System
Linux x86_64 Debian 12 (Docker container)
Build & Runtime Environment
- Python: 3.11 - zvec version: 0.3.0 (also tested on 0.3.1 - same behavior) - Index type: FlatIndex - Vector dimension: 384
Additional Context
- [ ] I've checked
git status— no uncommitted submodule changes - [ ] I built with
CMAKE_BUILD_TYPE=Debug - [ ] This occurs with or without
COVERAGE=ON - [ ] The issue involves Python ↔ C++ integration (pybind11)
Hi there, thanks for reporting this issue.
I attempted to reproduce this issue in a clean Debian 12 (Docker container) environment using zvec==0.3.0 and Python 3.11, but unfortunately, the script ran successfully without raising the ValueError.
Since I couldn't reproduce the error in a standard environment, this is highly likely an environment-specific issue. Could you please provide the following details to help us pinpoint the root cause?
- Base Image: What is the exact base image used in your Dockerfile?
- CPU & Architecture: What CPU model and architecture are you running on? (
lscpu) - Installation Method: Are you using our pre-built pip package, or are you building zvec from source?
It would also be helpful if you could enable debug logging to see exactly what is happening under the hood. You can initialize zvec with the following configuration at the start of your script:
import zvec
zvec.init(
log_type=zvec.LogType.CONSOLE,
log_level=zvec.LogLevel.DEBUG
)
Below is the exact script I used.
import zvec
from zvec import (
CollectionSchema,
FieldSchema,
VectorSchema,
DataType,
FlatIndexParam,
MetricType,
VectorQuery,
)
schema = CollectionSchema(
name="documents",
fields=[
FieldSchema("doc_id", DataType.STRING, nullable=False),
FieldSchema("title", DataType.STRING, nullable=True),
FieldSchema("content", DataType.STRING, nullable=True),
],
vectors=[
VectorSchema(
"context_vec",
DataType.VECTOR_FP32,
dimension=384,
index_param=FlatIndexParam(metric_type=MetricType.COSINE),
),
],
)
collection = zvec.create_and_open("./test_collection", schema=schema)
import numpy as np
vec = np.random.randn(384).astype(np.float32).tolist()
doc = zvec.Doc(
id="test_doc_1",
fields={"doc_id": "test_doc_1", "title": "test"},
vectors={"context_vec": vec},
)
collection.insert([doc])
collection.flush()
vq = VectorQuery(field_name="context_vec", vector=[0.0] * 384)
results = collection.query(vectors=vq, topk=10, include_vector=True)
print(results)
Hi, thank you for the quick response. We've done extensive testing and can now confirm the bug.
Environment
- Base Image:
python:3.11-slim(Debian 12-based) - CPU:
docker exec zvec-backend python -c "import platform; print(platform.machine())"→ 'x86_64' - Installation: Pre-built pip package (
zvec==0.3.0→zvec==0.3.1), installed via Aliyun PyPI mirror. Not built from source. - zvec version (current): 0.3.1 (also tested on 0.3.0 — same behavior)
Reproduction
We could NOT reproduce the bug with synthetic data — even with:
- The exact same dual‑vector schema (11 scalar fields +
context_vec+keywords_sparse) - 25 random documents (matching our production count)
zvec.open()on an existing collection (same process lifecycle as production)- Sequential
insert()+flush()per document (mimicking HTTP API usage) All synthetic tests succeed. The bug is data‑dependent. However, the bug IS reproducible on our actual production collection. We ran this script against/data/collection(after stopping the server):
import zvec
zvec.init(log_type=zvec.LogType.CONSOLE, log_level=zvec.LogLevel.DEBUG)
col = zvec.open("/data/collection")
vq = zvec.VectorQuery(field_name="context_vec", vector=[0.0] * 384)
# This fails:
results = col.query(vectors=vq, topk=col.stats.doc_count, include_vector=True)
# This works (but vectors are not included):
results = col.query(vectors=vq, topk=col.stats.doc_count, include_vector=False)
Root Cause (from debug logs)
The debug output reveals a truncated binary blob in one of the context_vec entries.
A normal 384‑dim VECTOR_FP32 vector is stored as a hex blob of 2972 characters (1536 bytes ≈ 384 × 4). But the 6th document in our batch has only 26 hex characters (13 bytes):
context_vec: [
"2CD906BD6BD06C3DAC5A71BB..." (2972 chars), ← 16 normal vectors
...
+ "DC182EBC58ABF73DCB7DBD3C00DF" ← truncated (only 13 bytes!)
]
zvec's Rust layer then sees a dimension mismatch: it expects 1536 bytes for a 384‑dim vector but only finds 13 bytes → ValueError: Dimension not match:3 vs 11 (the 3 and 11 likely reflect mismatched internal byte counters).
include_vector=False succeeds because it only reads scalar fields (which are stored in a separate RocksDB‑backed column store), not the corrupted flat storage blob.
Conclusion
This is not a version compatibility issue — it's a data corruption bug. One document's vector was stored as a truncated blob in the flat_streamer storage. The most likely causes:
1. WAL recovery bug: An interrupted flush() was partially recovered, producing an incomplete vector write
2. Race condition: Concurrent insert + flush operations corrupting the flat storage segment
3. Flat storage write bug: An edge case in flat_streamer_entity where a specific vector value triggers a write error
After re‑creating the collection from scratch (rm -rf ./data && docker compose up --build -d) and re‑inserting all documents, the error disappears. This confirms the data itself was corrupted, not the schema or library version.
We can provide a minimal reproduction script that uses our specific document content if that would help.
Thanks very much for the detailed investigation — this is very helpful.
We agree with your conclusion: this looks like persisted collection-state corruption, not a schema/version issue. The strongest signal is that: "recreating the collection and reinserting the same documents makes the issue disappear". That suggests the original stored vector data became corrupted at some point in the collection lifecycle, rather than the source documents themselves being invalid.
We also tried reproducing this with synthetic data using a similar setup (~25 docs, 384-dim vector, sequential insert() + flush(), reopen, query with include_vector=True), but could not reproduce it.
A minimal reproduction script using the actual offending documents, or the smallest subset that still reproduces the issue, would be extremely helpful. Also, if you know: was there any crash, forced restart, or concurrent insert/flush activity before the corruption appeared?
We've confirmed this bug is reproducible and systematic, not data-specific.
We deleted the old collection, created a fresh one, and inserted completely different documents (2 new articles, 33 new chunks, different content and length). The same error occurred: Dimension not match:2 vs 20.
Interestingly, the corrupted blob appears at the same position (index 5 in the RecordBatch) both times, even with different document content. This suggests a systematic issue in the flat storage segment writer — possibly a partial write or incorrect vector length calculation when the insert() + flush() sequence processes certain batch sizes or storage segment boundaries.
To answer your two questions:
- No crash or restart occurred before either corruption event. The server was running normally through sequential API calls.
- We cannot provide a minimal reproduction with synthetic data — the bug only manifests with real Chinese text documents that produce specific embedding values. Our random-vector tests never triggered it.
However, we can share the full corrupted data directory for inspection.
Key observation: the
context_vec.index.*.proximafiles (~5MB each) seem to be the affected storage segments. After deleting the data and rebuilding, the first 14-19 daily vector writes are clean, suggesting the corruption develops incrementally. We've implemented a workaround: reading vectors via DashVector (cloud) or re-embedding from text wheninclude_vector=Truefails. But this bug would affect any user doing localquery(include_vector=True)with FlatIndex on real embedding data.
Thanks for the extra investigation — this helped us pinpoint the root cause.
The issue is actually caused by sparse vector serialization, not the dense context_vec itself. This has been fixed in v0.4.0.
Please upgrade to v0.4.0, delete the current collection, and recreate it from scratch before re-inserting your documents, since the existing collection is already in a corrupted persisted state.
We’ll also revise the related error messages to make them more readable and easier to diagnose.
Please let us know if you still see the issue after rebuilding with v0.4.0.
Closing this now since haven't heard back for a while. Feel free to reopen it if you have any questions