1use thiserror::Error;
4
5pub type Result<T, E = crate::Error> = std::result::Result<T, E>;
6
7#[derive(Error, Debug)]
8pub enum Error {
9 #[error("Failed to build DFA {0}")]
11 IndexDfaError(#[from] Box<regex_automata::dfa::dense::BuildError>),
12 #[error("Index failed since anchored universal start state doesn't exist")]
13 DfaHasNoStartState,
14 #[error("EOS token should not be inserted into Vocabulary")]
16 EOSTokenDisallowed,
17 #[error(transparent)]
18 TokenizersError(#[from] tokenizers::Error),
19 #[error("Unsupported tokenizer for {model}: {reason}, please open an issue with the full error message: https://github.com/dottxt-ai/outlines-core/issues")]
20 UnsupportedTokenizer { model: String, reason: String },
21 #[error("Unable to locate EOS token for {model}")]
22 UnableToLocateEosTokenId { model: String },
23 #[error("Tokenizer is not supported by token processor")]
24 UnsupportedByTokenProcessor,
25 #[error("Decoder unpacking failed for token processor")]
26 DecoderUnpackingFailed,
27 #[error("Token processing failed for byte level processor")]
28 ByteProcessorFailed,
29 #[error("Token processing failed for byte fallback level processor")]
30 ByteFallbackProcessorFailed,
31 #[error("serde json error")]
33 SerdeJsonError(#[from] serde_json::Error),
34 #[error("Unsupported JSON Schema structure {0} \nMake sure it is valid to the JSON Schema specification and check if it's supported by Outlines.\nIf it should be supported, please open an issue.")]
35 UnsupportedJsonSchema(Box<serde_json::Value>),
36 #[error("'properties' not found or not an object")]
37 PropertiesNotFound,
38 #[error("'allOf' must be an array")]
39 AllOfMustBeAnArray,
40 #[error("'anyOf' must be an array")]
41 AnyOfMustBeAnArray,
42 #[error("'oneOf' must be an array")]
43 OneOfMustBeAnArray,
44 #[error("'prefixItems' must be an array")]
45 PrefixItemsMustBeAnArray,
46 #[error("Unsupported data type in enum: {0}")]
47 UnsupportedEnumDataType(Box<serde_json::Value>),
48 #[error("'enum' must be an array")]
49 EnumMustBeAnArray,
50 #[error("Unsupported data type in const: {0}")]
51 UnsupportedConstDataType(Box<serde_json::Value>),
52 #[error("'const' key not found in object")]
53 ConstKeyNotFound,
54 #[error("'$ref' must be a string")]
55 RefMustBeAString,
56 #[error("External references are not supported: {0}")]
57 ExternalReferencesNotSupported(Box<str>),
58 #[error("Invalid reference format: {0}")]
59 InvalidReferenceFormat(Box<str>),
60 #[error("'type' must be a string or an array of string")]
61 TypeMustBeAStringOrArray,
62 #[error("Unsupported type: {0}")]
63 UnsupportedType(Box<str>),
64 #[error("maxLength must be greater than or equal to minLength")]
65 MaxBoundError,
66 #[error("Format {0} is not supported by Outlines")]
67 StringTypeUnsupportedFormat(Box<str>),
68 #[error("Invalid reference path: {0}")]
69 InvalidRefecencePath(Box<str>),
70 #[error("Ref recusion limit reached: {0}")]
71 RefRecursionLimitReached(usize),
72}
73
74impl Error {
75 pub fn is_recursion_limit(&self) -> bool {
76 matches!(self, Self::RefRecursionLimitReached(_))
77 }
78}
79
80#[cfg(feature = "python-bindings")]
81impl From<Error> for pyo3::PyErr {
82 fn from(e: Error) -> Self {
83 use pyo3::exceptions::PyValueError;
84 use pyo3::PyErr;
85 PyErr::new::<PyValueError, _>(e.to_string())
86 }
87}