Skip to main content

rig_llama_cpp/
error.rs

1//! Public error types for `rig-llama-cpp`.
2//!
3//! Public-API constructors return [`LoadError`] (instead of `anyhow::Error` or
4//! `String`) so that downstream code can match on the failure mode without
5//! resorting to substring checks on a stringly-typed message.
6
7use thiserror::Error;
8
9/// Failure modes returned when constructing or reloading a [`crate::Client`]
10/// or [`crate::EmbeddingClient`].
11///
12/// Variants are intentionally coarse-grained: each one maps to a distinct
13/// stage of model bring-up, and the embedded `String` carries the underlying
14/// llama.cpp / FFI message verbatim for diagnostic purposes.
15#[derive(Debug, Error)]
16#[non_exhaustive]
17pub enum LoadError {
18    /// The process-wide [`llama_cpp_2::llama_backend::LlamaBackend`] could not
19    /// be initialised. This failure is sticky for the lifetime of the
20    /// process — typically it means there is no usable GPU device for the
21    /// selected backend (e.g. no Vulkan ICD loadable, no CUDA driver, etc.).
22    #[error("llama.cpp backend initialisation failed: {0}")]
23    BackendInit(String),
24
25    /// Configuring the requested set of GPU devices failed (e.g. an invalid
26    /// device list was passed to `LlamaModelParams::with_devices`).
27    #[error("failed to configure backend devices: {0}")]
28    ConfigureDevices(String),
29
30    /// Automatic parameter fitting failed — llama.cpp could not find a
31    /// layer-offload allocation that fits within the supplied memory
32    /// margins for the given context size.
33    #[error("automatic parameter fitting failed: {0}")]
34    Fit(String),
35
36    /// Loading the GGUF model file failed (file not found, invalid format,
37    /// out-of-memory during allocation, etc.).
38    #[error("model load failed: {0}")]
39    ModelLoad(String),
40
41    /// Initialising the multimodal projector (mmproj) for a vision model
42    /// failed. Only produced when the `mtmd` feature is enabled and a
43    /// `mmproj_path` was supplied.
44    #[cfg(feature = "mtmd")]
45    #[error("multimodal projector init failed: {0}")]
46    MmprojInit(String),
47
48    /// The model path contained an interior NUL byte and could not be
49    /// converted to a `CString` for the FFI call.
50    #[error("invalid model path: {0}")]
51    InvalidPath(String),
52
53    /// The inference worker thread exited before it could report the result
54    /// of initialisation. This usually means the worker panicked; check the
55    /// process's stderr for a backtrace.
56    #[error("inference worker thread exited during initialisation")]
57    WorkerInitDisconnected,
58
59    /// A reload was requested, but the inference worker thread is no longer
60    /// accepting commands (it has either been shut down or panicked on a
61    /// previous request).
62    #[error("inference worker thread is no longer running")]
63    WorkerNotRunning,
64}