3737from nemoguardrails .colang .v1_0 .runtime .flows import _normalize_flow_id
3838from nemoguardrails .colang .v2_x .lang .utils import format_colang_parsing_error_message
3939from nemoguardrails .colang .v2_x .runtime .errors import ColangParsingError
40+ from nemoguardrails .exceptions import (
41+ InvalidModelConfigurationError ,
42+ InvalidRailsConfigurationError ,
43+ )
4044
4145log = logging .getLogger (__name__ )
4246
@@ -136,8 +140,8 @@ def set_and_validate_model(cls, data: Any) -> Any:
136140 model_from_params = parameters .get ("model_name" ) or parameters .get ("model" )
137141
138142 if model_field and model_from_params :
139- raise ValueError (
140- "Model name must be specified in exactly one place: either in the ' model' field or in parameters, not both."
143+ raise InvalidModelConfigurationError (
144+ "Model name must be specified in exactly one place: either the ` model` field, or in ` parameters` (`parameters.model` or `parameters.model_name`)." ,
141145 )
142146 if not model_field and model_from_params :
143147 data ["model" ] = model_from_params
@@ -151,8 +155,8 @@ def set_and_validate_model(cls, data: Any) -> Any:
151155 def model_must_be_none_empty (self ) -> "Model" :
152156 """Validate that a model name is present either directly or in parameters."""
153157 if not self .model or not self .model .strip ():
154- raise ValueError (
155- "Model name must be specified either directly in the ' model' field or through 'model_name'/' model' in parameters"
158+ raise InvalidModelConfigurationError (
159+ "Model name must be specified in exactly one place: either the ` model` field, or in `parameters` (`parameters. model` or ` parameters.model_name`). "
156160 )
157161 return self
158162
@@ -334,10 +338,10 @@ class TaskPrompt(BaseModel):
334338 @root_validator (pre = True , allow_reuse = True )
335339 def check_fields (cls , values ):
336340 if not values .get ("content" ) and not values .get ("messages" ):
337- raise ValueError ("One of `content` or `messages` must be provided." )
341+ raise InvalidRailsConfigurationError ("One of `content` or `messages` must be provided." )
338342
339343 if values .get ("content" ) and values .get ("messages" ):
340- raise ValueError ("Only one of `content` or `messages` must be provided." )
344+ raise InvalidRailsConfigurationError ("Only one of `content` or `messages` must be provided." )
341345
342346 return values
343347
@@ -1414,7 +1418,11 @@ def check_model_exists_for_input_rails(cls, values):
14141418 if not flow_model :
14151419 continue
14161420 if flow_model not in model_types :
1417- raise ValueError (f"No `{ flow_model } ` model provided for input flow `{ _normalize_flow_id (flow )} `" )
1421+ flow_id = _normalize_flow_id (flow )
1422+ available_types = ", " .join (f"'{ str (t )} '" for t in sorted (model_types )) if model_types else "none"
1423+ raise InvalidRailsConfigurationError (
1424+ f"Input flow '{ flow_id } ' references model type '{ flow_model } ' that is not defined in the configuration. Detected model types: { available_types } ."
1425+ )
14181426 return values
14191427
14201428 @root_validator (pre = True )
@@ -1436,7 +1444,11 @@ def check_model_exists_for_output_rails(cls, values):
14361444 if not flow_model :
14371445 continue
14381446 if flow_model not in model_types :
1439- raise ValueError (f"No `{ flow_model } ` model provided for output flow `{ _normalize_flow_id (flow )} `" )
1447+ flow_id = _normalize_flow_id (flow )
1448+ available_types = ", " .join (f"'{ str (t )} '" for t in sorted (model_types )) if model_types else "none"
1449+ raise InvalidRailsConfigurationError (
1450+ f"Output flow '{ flow_id } ' references model type '{ flow_model } ' that is not defined in the configuration. Detected model types: { available_types } ."
1451+ )
14401452 return values
14411453
14421454 @root_validator (pre = True )
@@ -1450,9 +1462,13 @@ def check_prompt_exist_for_self_check_rails(cls, values):
14501462
14511463 # Input moderation prompt verification
14521464 if "self check input" in enabled_input_rails and "self_check_input" not in provided_task_prompts :
1453- raise ValueError ("You must provide a `self_check_input` prompt template." )
1465+ raise InvalidRailsConfigurationError (
1466+ "Missing a `self_check_input` prompt template, which is required for the `self check input` rail."
1467+ )
14541468 if "llama guard check input" in enabled_input_rails and "llama_guard_check_input" not in provided_task_prompts :
1455- raise ValueError ("You must provide a `llama_guard_check_input` prompt template." )
1469+ raise InvalidRailsConfigurationError (
1470+ "Missing a `llama_guard_check_input` prompt template, which is required for the `llama guard check input` rail."
1471+ )
14561472
14571473 # Only content-safety and topic-safety include a $model reference in the rail flow text
14581474 # Need to match rails with flow_id (excluding $model reference) and match prompts
@@ -1462,20 +1478,28 @@ def check_prompt_exist_for_self_check_rails(cls, values):
14621478
14631479 # Output moderation prompt verification
14641480 if "self check output" in enabled_output_rails and "self_check_output" not in provided_task_prompts :
1465- raise ValueError ("You must provide a `self_check_output` prompt template." )
1481+ raise InvalidRailsConfigurationError (
1482+ "Missing a `self_check_output` prompt template, which is required for the `self check output` rail."
1483+ )
14661484 if (
14671485 "llama guard check output" in enabled_output_rails
14681486 and "llama_guard_check_output" not in provided_task_prompts
14691487 ):
1470- raise ValueError ("You must provide a `llama_guard_check_output` prompt template." )
1488+ raise InvalidRailsConfigurationError (
1489+ "Missing a `llama_guard_check_output` prompt template, which is required for the `llama guard check output` rail."
1490+ )
14711491 if (
14721492 "patronus lynx check output hallucination" in enabled_output_rails
14731493 and "patronus_lynx_check_output_hallucination" not in provided_task_prompts
14741494 ):
1475- raise ValueError ("You must provide a `patronus_lynx_check_output_hallucination` prompt template." )
1495+ raise InvalidRailsConfigurationError (
1496+ "Missing a `patronus_lynx_check_output_hallucination` prompt template, which is required for the `patronus lynx check output hallucination` rail."
1497+ )
14761498
14771499 if "self check facts" in enabled_output_rails and "self_check_facts" not in provided_task_prompts :
1478- raise ValueError ("You must provide a `self_check_facts` prompt template." )
1500+ raise InvalidRailsConfigurationError (
1501+ "Missing a `self_check_facts` prompt template, which is required for the `self check facts` rail."
1502+ )
14791503
14801504 # Only content-safety and topic-safety include a $model reference in the rail flow text
14811505 # Need to match rails with flow_id (excluding $model reference) and match prompts
@@ -1528,7 +1552,7 @@ def validate_models_api_key_env_var(cls, models):
15281552 api_keys = [m .api_key_env_var for m in models ]
15291553 for api_key in api_keys :
15301554 if api_key and not os .environ .get (api_key ):
1531- raise ValueError (f"Model API Key environment variable '{ api_key } ' not set." )
1555+ raise InvalidRailsConfigurationError (f"Model API Key environment variable '{ api_key } ' not set." )
15321556 return models
15331557
15341558 raw_llm_call_action : Optional [str ] = Field (
@@ -1801,4 +1825,6 @@ def _validate_rail_prompts(rails: list[str], prompts: list[Any], validation_rail
18011825 prompt_flow_id = flow_id .replace (" " , "_" )
18021826 expected_prompt = f"{ prompt_flow_id } $model={ flow_model } "
18031827 if expected_prompt not in prompts :
1804- raise ValueError (f"You must provide a `{ expected_prompt } ` prompt template." )
1828+ raise InvalidRailsConfigurationError (
1829+ f"Missing a `{ expected_prompt } ` prompt template, which is required for the `{ validation_rail } ` rail."
1830+ )
0 commit comments