@@ -1198,14 +1198,25 @@ def apply_hf_chat_template(
1198
1198
"allowed, so you must provide a chat template if the tokenizer "
1199
1199
"does not define one." )
1200
1200
1201
- return tokenizer .apply_chat_template (
1202
- conversation = conversation , # type: ignore[arg-type]
1203
- tools = tools , # type: ignore[arg-type]
1204
- chat_template = hf_chat_template ,
1205
- tokenize = tokenize ,
1206
- ** kwargs ,
1207
- )
1201
+ try :
1202
+
1203
+ return tokenizer .apply_chat_template (
1204
+ conversation = conversation , # type: ignore[arg-type]
1205
+ tools = tools , # type: ignore[arg-type]
1206
+ chat_template = hf_chat_template ,
1207
+ tokenize = tokenize ,
1208
+ ** kwargs ,
1209
+ )
1208
1210
1211
+ # External library exceptions can sometimes occur despite the framework's
1212
+ # internal exception management capabilities.
1213
+ except Exception as e :
1214
+
1215
+ # Log and report any library-related exceptions for further
1216
+ # investigation.
1217
+ logger .exception (
1218
+ "An error occurred in `transformers` while applying chat template" )
1219
+ raise ValueError from e
1209
1220
1210
1221
def apply_mistral_chat_template (
1211
1222
tokenizer : MistralTokenizer ,
@@ -1214,6 +1225,8 @@ def apply_mistral_chat_template(
1214
1225
tools : Optional [list [dict [str , Any ]]],
1215
1226
** kwargs : Any ,
1216
1227
) -> list [int ]:
1228
+ from mistral_common .exceptions import MistralCommonException
1229
+
1217
1230
# The return value of resolve_mistral_chat_template is always None,
1218
1231
# and we won't use it.
1219
1232
resolve_mistral_chat_template (
@@ -1231,5 +1244,16 @@ def apply_mistral_chat_template(
1231
1244
# if input does not comply with the expected format.
1232
1245
# We convert those assertion errors to ValueErrors so they can be
1233
1246
# are properly caught in the preprocessing_input step
1234
- except AssertionError as e :
1247
+ except (AssertionError , MistralCommonException ) as e :
1248
+ raise ValueError from e
1249
+
1250
+ # External library exceptions can sometimes occur despite the framework's
1251
+ # internal exception management capabilities.
1252
+ except Exception as e :
1253
+
1254
+ # Log and report any library-related exceptions for further
1255
+ # investigation.
1256
+ logger .exception (
1257
+ "An error occurred in `mistral_common` while applying chat "
1258
+ "template" )
1235
1259
raise ValueError from e
0 commit comments