uf.pydantic_support#

Pydantic integration for uf.

Provides seamless integration with Pydantic models, automatically generating forms with validation from Pydantic models.

Functions

create_pydantic_spec(func)

Create RJSF spec from function with Pydantic parameters.

dict_to_pydantic(data, model_class)

Convert dictionary to Pydantic model instance.

extract_field_validators(model_class)

Extract field validators from Pydantic model.

extract_pydantic_params(func)

Extract Pydantic model parameters from function signature.

function_uses_pydantic(func)

Check if a function uses Pydantic models in its signature.

get_pydantic_registry()

Get the global Pydantic registry.

is_pydantic_model(obj)

Check if an object is a Pydantic model.

pydantic_error_to_user_friendly(error)

Convert Pydantic ValidationError to user-friendly format.

pydantic_model_to_json_schema(model_class)

Convert a Pydantic model to JSON Schema.

pydantic_to_dict(obj)

Convert Pydantic model instance to dictionary.

wrap_pydantic_function(func)

Wrap a function that uses Pydantic models.

Classes

PydanticRegistry()

Registry for Pydantic models used in uf.

class uf.pydantic_support.PydanticRegistry[source]#

Bases: object

Registry for Pydantic models used in uf.

Tracks models and provides utilities for working with them.

get(name)[source]#

Get a registered model by name.

Parameters:

name (str) – Model name

Return type:

Optional[Any]

Returns:

Pydantic model class or None

list_models()[source]#

List all registered model names.

Return type:

list[str]

Returns:

List of model name strings

register(name, model_class)[source]#

Register a Pydantic model.

Parameters:
  • name (str) – Name to register under

  • model_class (Any) – Pydantic model class

Return type:

None

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:

Optional[dict]

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.

Parameters:
  • data (dict) – Dictionary of data

  • model_class – Pydantic model class

Return type:

Any

Returns:

Pydantic model instance

Raises:

ValidationError – If data doesn’t match model schema

uf.pydantic_support.extract_field_validators(model_class)[source]#

Extract field validators from Pydantic model.

Parameters:

model_class – Pydantic model class

Return type:

dict

Returns:

Dictionary mapping field names to validator info

uf.pydantic_support.extract_pydantic_params(func)[source]#

Extract Pydantic model parameters from function signature.

Parameters:

func (Callable) – Function to analyze

Return type:

dict[str, Any]

Returns:

Dictionary mapping parameter names to Pydantic model classes

uf.pydantic_support.function_uses_pydantic(func)[source]#

Check if a function uses Pydantic models in its signature.

Parameters:

func (Callable) – Function to check

Return type:

bool

Returns:

True if any parameter is a Pydantic model

uf.pydantic_support.get_pydantic_registry()[source]#

Get the global Pydantic registry.

Return type:

PydanticRegistry

Returns:

Global PydanticRegistry instance

uf.pydantic_support.is_pydantic_model(obj)[source]#

Check if an object is a Pydantic model.

Parameters:

obj (Any) – Object to check

Return type:

bool

Returns:

True if object is a Pydantic BaseModel

uf.pydantic_support.pydantic_error_to_user_friendly(error)[source]#

Convert Pydantic ValidationError to user-friendly format.

Parameters:

error – Pydantic ValidationError

Return type:

dict

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:

dict

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.pydantic_to_dict(obj)[source]#

Convert Pydantic model instance to dictionary.

Parameters:

obj (Any) – Pydantic model instance

Return type:

dict

Returns:

Dictionary representation

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.

Parameters:

func (Callable) – Function to wrap

Return type:

Callable

Returns:

Wrapped function

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