Skip to content

Validate Call

Decorator for validating function calls.

validate_call

validate_call(
    *,
    config: ConfigDict | None = None,
    validate_return: bool = False
) -> Callable[[AnyCallableT], AnyCallableT]
validate_call(func: AnyCallableT) -> AnyCallableT
validate_call(
    func: AnyCallableT | None = None,
    /,
    *,
    config: ConfigDict | None = None,
    validate_return: bool = False,
) -> AnyCallableT | Callable[[AnyCallableT], AnyCallableT]

Usage Documentation

Validation Decorator

Returns a wrapper around the function that validates the arguments and, optionally, the return value.

Usage may be either as a plain decorator @validate_call or with arguments (@validate_call(...)).

When a decorated call fails, the ValidationError reports the rejected argument and value. Logfire can retain the complete call input and surrounding trace context — see Troubleshooting validation errors.

Parameters:

Name Type Description Default
func AnyCallableT | None

The function to be wrapped.

None
config ConfigDict | None

The configuration dictionary.

None
validate_return bool

Whether to validate the return value.

False

Returns:

Type Description
AnyCallableT | Callable[[AnyCallableT], AnyCallableT]

The decorated function.

Source code in pydantic/validate_call_decorator.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def validate_call(
    func: AnyCallableT | None = None,
    /,
    *,
    config: ConfigDict | None = None,
    validate_return: bool = False,
) -> AnyCallableT | Callable[[AnyCallableT], AnyCallableT]:
    """!!! abstract "Usage Documentation"
        [Validation Decorator](../concepts/validation_decorator.md)

    Returns a wrapper around the function that validates the arguments and, optionally, the return value.

    Usage may be either as a plain decorator `@validate_call` or with arguments (`@validate_call(...)`).

    When a decorated call fails, the [`ValidationError`][pydantic_core.ValidationError] reports the
    rejected argument and value. [Logfire](../integrations/logfire.md) can retain the complete call input
    and surrounding trace context — see [Troubleshooting validation errors](../errors/troubleshooting.md).

    Args:
        func: The function to be wrapped.
        config: The configuration dictionary.
        validate_return: Whether to validate the return value.

    Returns:
        The decorated function.
    """
    parent_namespace = _typing_extra.parent_frame_namespace()

    def validate(function: AnyCallableT) -> AnyCallableT:
        _check_function_type(function)
        validate_call_wrapper = _validate_call.ValidateCallWrapper(
            cast(_generate_schema.ValidateCallSupportedTypes, function), config, validate_return, parent_namespace
        )
        return _validate_call.update_wrapper_attributes(function, validate_call_wrapper.__call__)  # type: ignore

    if func is not None:
        return validate(func)
    else:
        return validate