Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions chromadb/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,20 @@ def _validate_single_sparse_vector_index(self, key: str) -> None:
f"Only one sparse vector index is allowed per collection."
)

def _validate_sparse_vector_config(self, config: SparseVectorIndexConfig) -> None:
"""
Validate that if source_key is provided then either embedding_function or bm25
must be provided since there is no default embedding function.
Raises ValueError otherwise.
"""
if (config.source_key is not None
and config.embedding_function is None
and config.bm25 is not True):
raise ValueError(
f"If source_key is provided then either embedding_function or bm25 must be provided "
f"since there is no default embedding function. Config: {config}"
)

def _set_index_for_key(self, key: str, config: IndexConfig, enabled: bool) -> None:
"""Set an index configuration for a specific key."""
config_name = self._get_config_class_name(config)
Expand All @@ -2052,6 +2066,7 @@ def _set_index_for_key(self, key: str, config: IndexConfig, enabled: bool) -> No
# Do this BEFORE creating the key entry
if config_name == "SparseVectorIndexConfig" and enabled:
self._validate_single_sparse_vector_index(key)
self._validate_sparse_vector_config(cast(SparseVectorIndexConfig, config))

if key not in self.keys:
self.keys[key] = ValueTypes()
Expand Down Expand Up @@ -2096,6 +2111,8 @@ def _enable_all_indexes_for_key(self, key: str) -> None:
if key not in self.keys:
self.keys[key] = ValueTypes()

self._validate_single_sparse_vector_index(key)

# Enable all index types with default configs
self.keys[key].string = StringValueType(
fts_index=FtsIndexType(enabled=True, config=FtsIndexConfig()),
Expand Down
36 changes: 28 additions & 8 deletions chromadb/test/api/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def test_chained_create_and_delete_operations(self) -> None:
# 1. Create sparse vector index on "embeddings_key"
# 2. Disable string inverted index on "text_key_1"
# 3. Disable string inverted index on "text_key_2"
sparse_config = SparseVectorIndexConfig(source_key="raw_text")
sparse_config = SparseVectorIndexConfig(source_key="raw_text", bm25=True)
string_config = StringInvertedIndexConfig()

result = (
Expand Down Expand Up @@ -1268,7 +1268,7 @@ def test_multiple_index_types_on_same_key(self) -> None:
schema = Schema()

# Enable sparse vector on "multi_field"
sparse_config = SparseVectorIndexConfig(source_key="source")
sparse_config = SparseVectorIndexConfig(source_key="source", bm25=True)
schema.create_index(config=sparse_config, key="multi_field")

# Also enable string_inverted_index on the same key
Expand Down Expand Up @@ -1454,7 +1454,7 @@ def test_multiple_serialize_deserialize_roundtrips(self) -> None:
hnsw=hnsw_config
)
original.create_index(config=vector_config)
original.create_index(config=SparseVectorIndexConfig(source_key="text"), key="embeddings")
original.create_index(config=SparseVectorIndexConfig(source_key="text", bm25=True), key="embeddings")
original.delete_index(config=StringInvertedIndexConfig(), key="tags")

# First roundtrip
Expand Down Expand Up @@ -1514,7 +1514,7 @@ def test_many_keys_stress(self) -> None:
key_name = f"field_{i}"
if i == 0:
# Enable sparse vector on ONE key only
schema.create_index(config=SparseVectorIndexConfig(source_key=f"source_{i}"), key=key_name)
schema.create_index(config=SparseVectorIndexConfig(source_key=f"source_{i}", bm25=True), key=key_name)
elif i % 2 == 1:
# Disable string inverted index
schema.delete_index(config=StringInvertedIndexConfig(), key=key_name)
Expand Down Expand Up @@ -1578,7 +1578,7 @@ def test_chained_operations(self) -> None:

# Chain multiple operations
result = (schema
.create_index(config=SparseVectorIndexConfig(source_key="text"), key="field1")
.create_index(config=SparseVectorIndexConfig(source_key="text", bm25=True), key="field1")
.delete_index(config=StringInvertedIndexConfig(), key="field2")
.delete_index(config=StringInvertedIndexConfig(), key="field3")
.delete_index(config=IntInvertedIndexConfig(), key="field4"))
Expand Down Expand Up @@ -1820,7 +1820,7 @@ def test_keys_have_independent_configs(self) -> None:
schema = Schema()

# Enable sparse vector on a key - it gets exactly what we specify
sparse_config = SparseVectorIndexConfig(source_key="default_source")
sparse_config = SparseVectorIndexConfig(source_key="default_source", bm25=True)
schema.create_index(config=sparse_config, key="field1")

# Verify field1 has the sparse vector with the specified source_key
Expand Down Expand Up @@ -1907,7 +1907,7 @@ def test_key_specific_overrides_are_independent(self) -> None:
schema = Schema()

# Create sparse vector on one key and string indexes on others
schema.create_index(config=SparseVectorIndexConfig(source_key="source_a"), key="key_a")
schema.create_index(config=SparseVectorIndexConfig(source_key="source_a", bm25=True), key="key_a")
schema.create_index(config=StringInvertedIndexConfig(), key="key_b")
schema.create_index(config=StringInvertedIndexConfig(), key="key_c")

Expand Down Expand Up @@ -1992,7 +1992,7 @@ def test_partial_override_fills_from_defaults(self) -> None:
schema = Schema()

# Enable sparse vector on a key
schema.create_index(config=SparseVectorIndexConfig(source_key="my_source"), key="multi_index_field")
schema.create_index(config=SparseVectorIndexConfig(source_key="my_source", bm25=True), key="multi_index_field")

# This key now has sparse_vector overridden, but string, int, etc. should still follow global defaults
field = schema.keys["multi_index_field"]
Expand Down Expand Up @@ -2337,3 +2337,23 @@ def test_config_source_key_validates_special_keys() -> None:
# Regular keys (no #) are allowed
config6 = SparseVectorIndexConfig(source_key="my_field")
assert config6.source_key == "my_field"


def test_sparse_vector_config_requires_ef_with_source_key() -> None:
"""Test that SparseVectorIndexConfig raises ValueError when source_key is provided without embedding_function."""
schema = Schema()

# Attempt to create sparse vector index with source_key but no embedding_function
with pytest.raises(ValueError) as exc_info:
schema.create_index(
key="invalid_sparse",
config=SparseVectorIndexConfig(
source_key="text_field",
# No embedding_function provided - should raise ValueError
),
)

# Verify the error message mentions both source_key and embedding_function
error_msg = str(exc_info.value)
assert "source_key" in error_msg.lower()
assert "embedding_function" in error_msg.lower()
4 changes: 2 additions & 2 deletions chromadb/test/api/test_schema_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -2355,10 +2355,10 @@ def test_modify_collection_preserves_other_schema_fields(client: ClientAPI) -> N
collection_refreshed = client.get_collection(collection_name)
refreshed_schema = collection_refreshed.schema
assert refreshed_schema is not None

# Verify vector index was updated on server
assert refreshed_schema.defaults.float_list.vector_index.config.spann.search_nprobe == 128 # type: ignore

# Verify other value types are still intact on server
assert refreshed_schema.defaults.string is not None
assert refreshed_schema.defaults.string.string_inverted_index is not None
Expand Down
5 changes: 5 additions & 0 deletions rust/types/src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ pub fn validate_schema(schema: &Schema) -> Result<(), ValidationError> {
.into(),
));
}
if svit.config.source_key.is_some() && svit.config.embedding_function.is_none() {
Copy link
Contributor

Choose a reason for hiding this comment

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

[CriticalError]

Validation logic discrepancy between Python and Rust implementations:

Python validation (line 2053-2055):

if (config.source_key is not None
        and config.embedding_function is None
        and config.bm25 is not True):

Rust validation (line 2284):

if svit.config.source_key.is_some() && svit.config.embedding_function.is_none() {

The Rust validation is missing the bm25 check entirely. This means:

  • Python: SparseVectorIndexConfig(source_key="text", bm25=True) ✅ passes validation
  • Rust: Same config ❌ fails validation

This will cause runtime failures when the Python client sends valid configs to a Rust server.

Fix the Rust validation:

if svit.config.source_key.is_some() 
    && svit.config.embedding_function.is_none() 
    && !svit.config.bm25.unwrap_or(false) {
Context for Agents
[**CriticalError**]

Validation logic discrepancy between Python and Rust implementations:

**Python validation (line 2053-2055):**
```python
if (config.source_key is not None
        and config.embedding_function is None
        and config.bm25 is not True):
```

**Rust validation (line 2284):**
```rust
if svit.config.source_key.is_some() && svit.config.embedding_function.is_none() {
```

The Rust validation is missing the `bm25` check entirely. This means:
- Python: `SparseVectorIndexConfig(source_key="text", bm25=True)` ✅ passes validation
- Rust: Same config ❌ fails validation

This will cause runtime failures when the Python client sends valid configs to a Rust server.

**Fix the Rust validation:**
```rust
if svit.config.source_key.is_some() 
    && svit.config.embedding_function.is_none() 
    && !svit.config.bm25.unwrap_or(false) {
```

File: rust/types/src/validators.rs
Line: 284

return Err(ValidationError::new("schema").with_message(
"If source_key is provided then embedding_function must also be provided since there is no default embedding function.".into(),
));
}
}
// Validate source_key for sparse vector index
if let Some(source_key) = &svit.config.source_key {
Expand Down
Loading