Skip to content

Commit 57ef72d

Browse files
committed
fix!: BREAKING - in ChatTemplateError add a BuffSizeError variant, and in LlamaModel's get_chat_template method, return it as an Err when the buffer size is too small instead of panicking
1 parent 1eb7a46 commit 57ef72d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

llama-cpp-2/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ pub enum LLamaCppError {
6464
/// There was an error while getting the chat template from a model.
6565
#[derive(Debug, Eq, PartialEq, thiserror::Error)]
6666
pub enum ChatTemplateError {
67+
/// the buffer was too small.
68+
#[error("The buffer was too small. However, a buffer size of {0} would be just large enough.")]
69+
BuffSizeError(usize),
6770
/// gguf has no chat template
6871
#[error("the model has no meta val - returned code {0}")]
6972
MissingTemplate(i32),

llama-cpp-2/src/model.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,22 +370,28 @@ impl LlamaModel {
370370
let chat_ptr = chat_temp.into_raw();
371371
let chat_name = CString::new("tokenizer.chat_template").expect("no null bytes");
372372

373-
let chat_template: String = unsafe {
374-
let ret = llama_cpp_sys_2::llama_model_meta_val_str(
373+
let ret = unsafe {
374+
llama_cpp_sys_2::llama_model_meta_val_str(
375375
self.model.as_ptr(),
376376
chat_name.as_ptr(),
377377
chat_ptr,
378378
buf_size,
379-
);
380-
if ret < 0 {
381-
return Err(ChatTemplateError::MissingTemplate(ret));
382-
}
383-
let template = CString::from_raw(chat_ptr).to_str()?.to_string();
384-
debug_assert_eq!(usize::try_from(ret).unwrap(), template.len(), "llama.cpp guarantees that the returned int {ret} is the length of the string {} but that was not the case", template.len());
385-
template
379+
)
386380
};
387381

388-
Ok(chat_template)
382+
if ret < 0 {
383+
return Err(ChatTemplateError::MissingTemplate(ret));
384+
}
385+
386+
let template_c = unsafe { CString::from_raw(chat_ptr) };
387+
let template = template_c.to_str()?;
388+
389+
let ret: usize = ret.try_into().unwrap();
390+
if template.len() < ret {
391+
return Err(ChatTemplateError::BuffSizeError(ret + 1));
392+
}
393+
394+
Ok(template.to_owned())
389395
}
390396

391397
/// loads a model from a file.

0 commit comments

Comments
 (0)