Skip to content

Result-returning try_add / try_search* (parallel to the panicking API) #86

@Fieldnote-Echo

Description

@Fieldnote-Echo

Summary

The current contract panics on invalid input (wrong dimension, non-finite values, over-capacity). Fail-loud is correct default behaviour for a library and should stay. But callers feeding embeddings from an external model API can't practically catch panics.

Ask

Add a parallel, Result-returning API alongside (not replacing) the panicking one:

pub fn try_add(&mut self, vectors: &[f32]) -> Result<(), OrdvecError>;
pub fn try_search(&self, queries: &[f32], k: usize) -> Result<SearchResults, OrdvecError>;
pub fn try_search_asymmetric(&self, queries: &[f32], k: usize) -> Result<SearchResults, OrdvecError>;

Implement by hoisting the existing validation (assert_all_finite, dim checks, the util::checked_new_len capacity guard) into functions that return Err(OrdvecError::…) instead of panicking; the panicking methods delegate to the try_* form to avoid duplicated logic. Introduce a small OrdvecError enum (e.g. DimMismatch, NonFinite, CapacityExceeded).

Notes

  • Towards 1.0.
  • Mirrors the Python binding, which already turns these core panics into clean ValueErrors — this would let the binding call the try_* path directly and would pair naturally with the C-ABI issue (error reporting across the boundary).

Filed from an external technical review (May 2026); verified against v0.2.0 (search methods currently return SearchResults, not Result).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions