10
10
from guardrails .logger import logger , set_scope
11
11
from guardrails .prompt import Instructions , Prompt
12
12
from guardrails .schema import Schema , StringSchema
13
+ from guardrails .utils .exception_utils import UserFacingException
13
14
from guardrails .utils .llm_response import LLMResponse
14
15
from guardrails .utils .reask_utils import NonParseableReAsk , ReAsk , reasks_to_dict
15
16
from guardrails .validator_base import ValidatorError
@@ -185,9 +186,8 @@ def __call__(
185
186
prompt_params = prompt_params ,
186
187
include_instructions = include_instructions ,
187
188
)
188
- # TODO decide how to handle errors
189
- except (ValidatorError , ValueError ) as e :
190
- raise e
189
+ except UserFacingException as e :
190
+ raise e .original_exception
191
191
except Exception as e :
192
192
error_message = str (e )
193
193
return call_log , error_message
@@ -273,6 +273,7 @@ def step(
273
273
index , raw_output , output_schema
274
274
)
275
275
if parsing_error :
276
+ iteration .outputs .exception = parsing_error
276
277
iteration .outputs .error = str (parsing_error )
277
278
278
279
iteration .outputs .parsed_output = parsed_output
@@ -298,6 +299,7 @@ def step(
298
299
except Exception as e :
299
300
error_message = str (e )
300
301
iteration .outputs .error = error_message
302
+ iteration .outputs .exception = e
301
303
raise e
302
304
return iteration
303
305
@@ -322,16 +324,18 @@ def prepare(
322
324
"""
323
325
with start_action (action_type = "prepare" , index = index ) as action :
324
326
if api is None :
325
- raise ValueError ("API must be provided." )
327
+ raise UserFacingException ( ValueError ("API must be provided." ) )
326
328
327
329
if prompt_params is None :
328
330
prompt_params = {}
329
331
330
332
if msg_history :
331
333
if prompt_schema is not None or instructions_schema is not None :
332
- raise ValueError (
333
- "Prompt and instructions validation are "
334
- "not supported when using message history."
334
+ raise UserFacingException (
335
+ ValueError (
336
+ "Prompt and instructions validation are "
337
+ "not supported when using message history."
338
+ )
335
339
)
336
340
msg_history = copy .deepcopy (msg_history )
337
341
# Format any variables in the message history with the prompt params.
@@ -361,9 +365,11 @@ def prepare(
361
365
raise ValidatorError ("Message history validation failed" )
362
366
elif prompt is not None :
363
367
if msg_history_schema is not None :
364
- raise ValueError (
365
- "Message history validation is "
366
- "not supported when using prompt/instructions."
368
+ raise UserFacingException (
369
+ ValueError (
370
+ "Message history validation is "
371
+ "not supported when using prompt/instructions."
372
+ )
367
373
)
368
374
if isinstance (prompt , str ):
369
375
prompt = Prompt (prompt )
@@ -417,7 +423,9 @@ def prepare(
417
423
)
418
424
instructions = Instructions (validated_instructions )
419
425
else :
420
- raise ValueError ("Prompt or message history must be provided." )
426
+ raise UserFacingException (
427
+ ValueError ("Prompt or message history must be provided." )
428
+ )
421
429
422
430
action .log (
423
431
message_type = "info" ,
@@ -692,8 +700,8 @@ async def async_run(
692
700
output_schema ,
693
701
prompt_params = prompt_params ,
694
702
)
695
- except ( ValidatorError , ValueError ) as e :
696
- raise e
703
+ except UserFacingException as e :
704
+ raise e . original_exception
697
705
except Exception as e :
698
706
error_message = str (e )
699
707
@@ -777,6 +785,7 @@ async def async_step(
777
785
# Parse: parse the output.
778
786
parsed_output , parsing_error = self .parse (index , output , output_schema )
779
787
if parsing_error :
788
+ iteration .outputs .exception = parsing_error
780
789
iteration .outputs .error = str (parsing_error )
781
790
782
791
iteration .outputs .parsed_output = parsed_output
@@ -801,6 +810,7 @@ async def async_step(
801
810
except Exception as e :
802
811
error_message = str (e )
803
812
iteration .outputs .error = error_message
813
+ iteration .outputs .exception = e
804
814
raise e
805
815
return iteration
806
816
0 commit comments