uf.pydantic_support#
Pydantic integration for uf.
Provides seamless integration with Pydantic models, automatically generating forms with validation from Pydantic models.
Functions
|
Create RJSF spec from function with Pydantic parameters. |
|
Convert dictionary to Pydantic model instance. |
|
Extract field validators from Pydantic model. |
|
Extract Pydantic model parameters from function signature. |
|
Check if a function uses Pydantic models in its signature. |
Get the global Pydantic registry. |
|
|
Check if an object is a Pydantic model. |
Convert Pydantic ValidationError to user-friendly format. |
|
|
Convert a Pydantic model to JSON Schema. |
|
Convert Pydantic model instance to dictionary. |
|
Wrap a function that uses Pydantic models. |
Classes
Registry for Pydantic models used in uf. |
- class uf.pydantic_support.PydanticRegistry[source]#
Bases:
objectRegistry for Pydantic models used in uf.
Tracks models and provides utilities for working with them.
- uf.pydantic_support.create_pydantic_spec(func)[source]#
Create RJSF spec from function with Pydantic parameters.
- Parameters:
func (
Callable) – Function that uses Pydantic models- Return type:
- Returns:
RJSF specification dictionary or None
Example
>>> from pydantic import BaseModel, EmailStr >>> class UserCreate(BaseModel): ... email: EmailStr ... age: int >>> def create_user(user: UserCreate): ... pass >>> spec = create_pydantic_spec(create_user)
- uf.pydantic_support.dict_to_pydantic(data, model_class)[source]#
Convert dictionary to Pydantic model instance.
- uf.pydantic_support.extract_field_validators(model_class)[source]#
Extract field validators from Pydantic model.
- Parameters:
model_class – Pydantic model class
- Return type:
- Returns:
Dictionary mapping field names to validator info
- uf.pydantic_support.extract_pydantic_params(func)[source]#
Extract Pydantic model parameters from function signature.
- uf.pydantic_support.function_uses_pydantic(func)[source]#
Check if a function uses Pydantic models in its signature.
- uf.pydantic_support.get_pydantic_registry()[source]#
Get the global Pydantic registry.
- Return type:
- Returns:
Global PydanticRegistry instance
- uf.pydantic_support.pydantic_error_to_user_friendly(error)[source]#
Convert Pydantic ValidationError to user-friendly format.
- Parameters:
error – Pydantic ValidationError
- Return type:
- Returns:
Dictionary with field-level errors
- uf.pydantic_support.pydantic_model_to_json_schema(model_class)[source]#
Convert a Pydantic model to JSON Schema.
- Parameters:
model_class – Pydantic model class
- Return type:
- Returns:
JSON Schema dictionary
Example
>>> from pydantic import BaseModel >>> class User(BaseModel): ... name: str ... age: int >>> schema = pydantic_model_to_json_schema(User)
- uf.pydantic_support.wrap_pydantic_function(func)[source]#
Wrap a function that uses Pydantic models.
The wrapper converts dict inputs to Pydantic models before calling the function, and converts Pydantic outputs back to dicts.
Example
>>> from pydantic import BaseModel >>> class User(BaseModel): ... name: str >>> def create_user(user: User) -> User: ... return user >>> wrapped = wrap_pydantic_function(create_user) >>> result = wrapped({'name': 'Alice'}) # Pass dict, not User