File tree Expand file tree Collapse file tree 2 files changed +14
-9
lines changed Expand file tree Collapse file tree 2 files changed +14
-9
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ public enum TokenizerError: LocalizedError {
19
19
case missingVocab
20
20
case malformedVocab
21
21
case chatTemplate( String )
22
+ case missingChatTemplate
22
23
case tooLong( String )
23
24
case mismatchedConfig( String )
24
25
@@ -36,6 +37,8 @@ public enum TokenizerError: LocalizedError {
36
37
String ( localized: " The vocabulary file is malformed or corrupted. " , comment: " Error when vocab file is malformed " )
37
38
case let . chatTemplate( message) :
38
39
String ( localized: " Chat template error: \( message) " , comment: " Error with chat template " )
40
+ case . missingChatTemplate:
41
+ String ( localized: " This tokenizer does not have a chat template, and no template was passed. " )
39
42
case let . tooLong( message) :
40
43
String ( localized: " Input is too long: \( message) " , comment: " Error when input exceeds maximum length " )
41
44
case let . mismatchedConfig( message) :
@@ -514,7 +517,7 @@ public class PreTrainedTokenizer: Tokenizer {
514
517
}
515
518
516
519
guard let selectedChatTemplate else {
517
- throw TokenizerError . chatTemplate ( " This tokenizer does not have a chat template, and no template was passed. " )
520
+ throw TokenizerError . missingChatTemplate
518
521
}
519
522
520
523
let template = try Template ( selectedChatTemplate)
Original file line number Diff line number Diff line change @@ -220,14 +220,16 @@ class ChatTemplateTests: XCTestCase {
220
220
func testApplyTemplateError( ) async throws {
221
221
let tokenizer = try await AutoTokenizer . from ( pretrained: " google-bert/bert-base-uncased " )
222
222
XCTAssertFalse ( tokenizer. hasChatTemplate)
223
- XCTAssertThrowsError ( try tokenizer. applyChatTemplate ( messages: [ ] ) )
224
- do {
225
- _ = try tokenizer. applyChatTemplate ( messages: [ ] )
226
- XCTFail ( )
227
- } catch let TokenizerError . chatTemplate( message) {
228
- XCTAssertEqual ( message, " This tokenizer does not have a chat template, and no template was passed. " )
229
- } catch {
230
- XCTFail ( )
223
+ XCTAssertThrowsError ( try tokenizer. applyChatTemplate ( messages: [ ] ) ) { error in
224
+ guard let tokenizerError = error as? TokenizerError else {
225
+ XCTFail ( " Expected error of type TokenizerError, but got \( type ( of: error) ) " )
226
+ return
227
+ }
228
+ if case . missingChatTemplate = tokenizerError {
229
+ // Correct error caught, test passes
230
+ } else {
231
+ XCTFail ( " Expected .missingChatTemplate, but got \( tokenizerError) " )
232
+ }
231
233
}
232
234
}
233
235
}
You can’t perform that action at this time.
0 commit comments