diff --git a/pytensor/compile/function/types.py b/pytensor/compile/function/types.py index 9cc85f3d24..14ffa223c8 100644 --- a/pytensor/compile/function/types.py +++ b/pytensor/compile/function/types.py @@ -949,32 +949,27 @@ def __call__(self, *args, output_subset=None, **kwargs): except Exception as e: i = input_storage.index(arg_container) - function_name = "pytensor function" - argument_name = "argument" - if self.name: - function_name += ' with name "' + self.name + '"' - if hasattr(arg, "name") and arg.name: - argument_name += ' with name "' + arg.name + '"' - where = get_variable_trace_string(self.maker.inputs[i].variable) - if len(e.args) == 1: - e.args = ( - "Bad input " - + argument_name - + " to " - + function_name - + f" at index {int(i)} (0-based). {where}" - + e.args[0], + function_name = ( + "pytensor function" + if self.name + else f"pytensor function '{self.name}'" + ) + argument_name = ( + f"argument {arg.name}" + if hasattr(arg, "name") + else "argument" + ) + where = ( + "" + if config.exception_verbosity == "low" + else get_variable_trace_string( + self.maker.inputs[i].variable ) - else: - e.args = ( - "Bad input " - + argument_name - + " to " - + function_name - + f" at index {int(i)} (0-based). {where}" - ) + e.args - self._restore_defaults() - raise + ) + e.add_note( + f"\nInvalid {argument_name} to {function_name} at index {i}.{where}" + ) + raise e arg_container.provided += 1 # Set keyword arguments diff --git a/pytensor/configdefaults.py b/pytensor/configdefaults.py index ca3c44bf6d..8b4c6c233e 100644 --- a/pytensor/configdefaults.py +++ b/pytensor/configdefaults.py @@ -638,15 +638,8 @@ def add_error_and_warning_configvars(): # on all important apply nodes. config.add( "exception_verbosity", - "If 'low', the text of exceptions will generally refer " - "to apply nodes with short names such as " - "Elemwise{add_no_inplace}. If 'high', some exceptions " - "will also refer to apply nodes with long descriptions " - """ like: - A. Elemwise{add_no_inplace} - B. log_likelihood_v_given_h - C. log_likelihood_h""", - EnumStr("low", ["high"]), + "Verbosity of exceptions generated by PyTensor functions.", + EnumStr("low", ["medium", "high"]), in_c_key=False, ) diff --git a/pytensor/link/utils.py b/pytensor/link/utils.py index 9cbc3838dd..780e4c8cce 100644 --- a/pytensor/link/utils.py +++ b/pytensor/link/utils.py @@ -313,6 +313,12 @@ def raise_with_op( # print a simple traceback from KeyboardInterrupt raise exc_value.with_traceback(exc_trace) + if verbosity == "low": + exc_value.add_note( + "\nHINT: Set PyTensor `config.exception_verbosity` to `medium` or `high` for more information about the source of the error." + ) + raise exc_value.with_traceback(exc_trace) + trace = getattr(node.outputs[0].tag, "trace", ()) exc_value.__thunk_trace__ = trace