Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions chromadb/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,17 @@ 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 ef is also provided since
there is no default embedding function. Raises ValueError otherwise.
"""
if config.source_key is not None and config.embedding_function is None:
raise ValueError(
f"If source_key is provided then embedding_function must also 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 +2063,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 +2108,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
20 changes: 20 additions & 0 deletions chromadb/test/api/test_schema.py
Original file line number Diff line number Diff line change
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