Skip to content

Commit d6d5b12

Browse files
fix(router): Handle tokenizer errors
1 parent feb7806 commit d6d5b12

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

router/src/validation.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,26 @@ fn validation_worker(
123123
}
124124

125125
// Get the number of tokens in the input
126-
let inputs = tokenizer.encode(request.inputs.clone(), false).unwrap();
127-
let input_length = inputs.len();
128-
129-
if input_length > max_input_length {
130-
response_tx
131-
.send(Err(ValidationError::InputLength(
132-
input_length,
133-
max_input_length,
134-
)))
135-
.unwrap_or(());
136-
continue;
137-
}
138-
139-
response_tx.send(Ok((input_length, request))).unwrap_or(());
126+
match tokenizer.encode(request.inputs.clone(), false) {
127+
Ok(inputs) => {
128+
let input_length = inputs.len();
129+
130+
if input_length > max_input_length {
131+
response_tx
132+
.send(Err(ValidationError::InputLength(
133+
input_length,
134+
max_input_length,
135+
)))
136+
.unwrap_or(());
137+
continue;
138+
}
139+
140+
response_tx.send(Ok((input_length, request))).unwrap_or(());
141+
}
142+
Err(err) => response_tx
143+
.send(Err(ValidationError::Tokenizer(err.to_string())))
144+
.unwrap_or(()),
145+
};
140146
}
141147
}
142148

@@ -157,6 +163,8 @@ pub enum ValidationError {
157163
MaxNewTokens,
158164
#[error("inputs must have less than {1} tokens. Given: {0}")]
159165
InputLength(usize, usize),
166+
#[error("tokenizer error {0}")]
167+
Tokenizer(String),
160168
}
161169

162170
impl From<ValidationError> for (StatusCode, Json<ErrorResponse>) {

0 commit comments

Comments
 (0)