Registry / serialization / pydantic-function-models

pydantic-function-models

JSON →
library0.1.12pypypi✓ verified 87d ago

A library designed to model Python function signatures using Pydantic, offering a modern alternative to the now-deprecated `ValidatedFunction` from Pydantic v1. It facilitates the validation of function arguments based on type hints, bridging compatibility gaps between Pydantic v1 and v2. The library is currently at version 0.1.11, with updates reflecting ongoing maintenance.

pip install pydantic-function-models
INSTALL
IMPORT
SIG · PYDANTIC-FUNCTION-
P
pydantic-function-models
serializationpythonv0.1.12
Install
3.2s avg
Import
522ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.12 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.439s · 27.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.2s · import 0.397s · 28MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ValidatedFunction
from pydantic_function_models import ValidatedFunction
from pydantic import ValidatedFunction
In Pydantic v2, `pydantic.ValidatedFunction` is deprecated. Use `pydantic_function_models.ValidatedFunction` as its modern replacement for modeling function signatures.

This example demonstrates how to wrap a Python function with `ValidatedFunction` to enable Pydantic-style argument validation. It shows both successful validation and how `ValidationError` is raised for invalid inputs.

from pydantic_function_models import ValidatedFunction def add(a: int, b: int) -> int: return a + b vf = ValidatedFunction(add) # Example of validating arguments args_to_validate = (1,) kwargs_to_validate = {"b": 2} # The library builds an internal Pydantic model for validation # You need to map positional/keyword args to model fields validated_data = vf.model.model_validate({ "a": args_to_validate[0], "b": kwargs_to_validate["b"] }) result = add(**validated_data.model_dump(exclude_unset=True)) print(f"Result: {result}") # Example of invalid input try: invalid_data = vf.model.model_validate({"a": "one", "b": 2}) except Exception as e: print(f"Validation error caught: {e}")
Debug
Known issues
deprecatedThe `ValidatedFunction` class from Pydantic v1 is deprecated in Pydantic v2. This library provides a compatible `ValidatedFunction` for Pydantic v2 environments.
fix
Migrate usage from `pydantic.ValidatedFunction` to `pydantic_function_models.ValidatedFunction` to ensure compatibility and leverage Pydantic v2's features.
affects: Pydantic >= 2.0
gotchaAvoid using reserved parameter names such as `v__args` and `v__kwargs` in your function signatures, as these could conflict with the internal validation logic of `pydantic-function-models`.
fix
Rename function parameters if they clash with `v__args` or `v__kwargs` to prevent unexpected behavior.
affects: All versions
gotchaFor simple function argument validation without needing the full function signature modeling provided by `ValidatedFunction`, Pydantic v2 offers the `@validate_call` decorator, which might be a more direct solution.
fix
Consider using `from pydantic import validate_call` directly on your function if only argument validation is required, rather than full signature modeling.
affects: Pydantic >= 2.0
gotchaMixing Pydantic v1 and v2 models within a single application can lead to compatibility issues, especially with nested models. While `pydantic-function-models` helps with function validation, general migration strategies for `BaseModel` compatibility should be followed.
fix
Ensure a consistent Pydantic version across your codebase or use compatibility tools if incremental migration is necessary. Refer to Pydantic's official migration guide.
affects: Applications transitioning from Pydantic v1 to v2
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pydantic_function_models'
The 'pydantic-function-models' library has not been installed or is not accessible in the current Python environment.
fix
Install the library using pip: `pip install pydantic-function-models`
ModuleNotFoundError: No module named 'pydantic'
The 'pydantic' library, a core dependency of 'pydantic-function-models', is either not installed or not correctly configured in your Python environment.
fix
Install Pydantic using pip: `pip install pydantic` or `pip install pydantic==2.*` for Pydantic v2.
TypeError: 'type' object is not iterable
This error typically occurs when defining type hints for generic types like `list` or `dict` using parentheses `()` instead of square brackets `[]`, such as `list(str)` instead of `list[str]` or `List[str]`.
fix
Correct the type hint syntax by using square brackets for generic types: `param: list[str]` (Python 3.9+) or `param: List[str]` (from `typing` for older Python versions).
pydantic.ValidationError
Input arguments provided to a function wrapped by `pydantic-function-models` do not conform to the type hints defined in the function's signature, or additional Pydantic validation rules are violated.
fix
Ensure that all arguments passed to the validated function match their declared types and any Pydantic validation constraints. Inspect the detailed error message for specific field validation failures.
ImportError: cannot import name 'ValidatedFunction' from 'pydantic'
You are attempting to import the `ValidatedFunction` class directly from the top-level `pydantic` package in a Pydantic v2 environment, where it has been deprecated and removed. 'pydantic-function-models' provides a v2-compatible alternative.
fix
Import `ValidatedFunction` from `pydantic_function_models` instead: `from pydantic_function_models import ValidatedFunction`.
Upgrade
Version history
0.1.12latest on PyPI · released Apr 16, 2026
Audit
Dependencies
pydanticrequiredCore dependency for function signature modeling and validation (supports both v1 and v2 due to library's bridging purpose).
Agent activity
19 hits · last 30 days
node
16
Bingbot
1
OpenAI (training)
1
Resources
pydantic-function-models — pip install pydantic-function-models · libregistry