Migrating v1 Pydantic ValidatedFunction to v2.
Maintained. Last release 8 months ago, commits in 5 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install pydantic-function-modelsPydantic Function Models
A library to model Python function signatures using Pydantic, inspired by the now-deprecated ValidatedFunction from Pydantic v1. This package helps validate function arguments according to type hints, bridging the gap between Pydantic v1 and v2.
Installation
pip install pydantic-function-models
Features
- Signature modelling: Wrap any Python function to model its signature (positional, keyword, etc.) using Pydantic. Note that if you are after just validation (whether the arguments are valid) then you want validate call
- Painless migration from Pydantic v1: Provides a
ValidatedFunctionclass ported to v2, for those who relied onValidatedFunctionin v1. - Clear error messages: Raises Pydantic
ValidationErrorwith helpful details if input arguments do not match the declared types. - Reserved name checks: Prevents usage of special parameter names (e.g.,
v__args,v__kwargs) that could conflict with internal validation logic.
Usage
Below is a general overview of how to use pydantic-function-models in your own code.
Example: Basic Validated Function
from pydantic_function_models import ValidatedFunction
def add(a: int, b: int) -> int:
return a + b
vf = ValidatedFunction(add)
# Build arguments for validation
args_to_validate = (1,)
kwargs_to_validate = {"b": 2}
validated = vf.model.model_validate({
"a": args_to_validate[0],
"b": kwargs_to_validate["b"]
})
result = add(**validated.model_dump(exclude_unset=True))
print(result) # 3