Khroma
An idiomatic, asynchronous, and high-level Rust SDK for interacting with a ChromaDB vector database.
This library provides a safe and ergonomic interface, abstracting away the raw HTTP requests into a stateful, object-oriented API that is a joy to use.
Features
- Fluent, High-Level API: Interact with Chroma through clean, stateful objects:
Khroma->Tenant->Database->Collection. - Fully Asynchronous: Built on
tokioandreqwestfor non-blocking I/O, perfect for high-performance applications. - Type-Safe: All API models are strongly typed, ensuring correctness at compile time.
- Ergonomic Error Handling: A single
KhromaErrorenum makes handling API and network errors straightforward. - Complete API Coverage: Provides access to the full range of ChromaDB v2 API endpoints, from server health checks to complex collection queries.
Installation
Add the following to your Cargo.toml file:
[]
= "0.1.0"
Quick Start
Here is a complete example of how to connect to Chroma, ensure a collection exists, upsert some data, and perform a query.
use Khroma;
use ;
async
API Concepts
The SDK is designed around a hierarchy of stateful handles. This makes the API intuitive and reduces the need to pass IDs repeatedly.
Khroma: The main entry point. Used for server-level operations (version,heartbeat) and for gettingTenanthandles.Tenant: Represents a specific tenant. Used to manage databases within that tenant (create_database,get_database).Database: Represents a database within a tenant. Used to manage collections (create_collection,list_collections).Collection: Represents a collection. This is where most of the work happens:add,upsert,query,get,delete, etc.
Detailed Examples
Filtering with where clauses
You can filter get, query, and delete operations using where and where_document clauses. Use the serde_json::json! macro for easy filter creation.
use ;
use json;
// Assume `collection` is a valid handle from the Quick Start example.
// Add metadata to your records
collection.upsert.await?;
// Get records where topic is "rust"
let get_result = collection.get.await?;
println!;
// Expected output: ["id3"]
Deleting Records
You can delete records by ID or by a where filter.
use DeleteCollectionRecordsPayload;
use json;
// Delete by ID
collection.delete.await?;
// Delete by metadata filter
collection.delete.await?;
Error Handling
All fallible API calls return a Result<T, KhromaError>. The KhromaError enum provides detailed information about the cause of the failure:
KhromaError::Reqwest: For network or transport-level errors.KhromaError::Api: For errors returned by the ChromaDB server (e.g., 404 Not Found, 401 Unauthorized). Includes the status code and server message.KhromaError::Parse: For issues deserializing the server's response.KhromaError::Url: For malformed base URLs.
Contributing
Contributions are welcome! Please feel free to open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.