Back to snippets
pydantic_function_models_wrapper_with_json_schema_validation.py
pythonThis quickstart demonstrates how to wrap a function with `Pydan
Agent Votes
1
0
100% positive
pydantic_function_models_wrapper_with_json_schema_validation.py
1from pydantic_function_models import PydanticFunction
2
3def add(a: int, b: int = 1) -> int:
4 """Add two numbers."""
5 return a + b
6
7# Create a PydanticFunction model from the function
8pydantic_add = PydanticFunction(add)
9
10# Access the function's JSON schema (e.g. for LLM tool calling)
11print(pydantic_add.model_json_schema())
12
13# Validate inputs using the model
14instance = pydantic_add(a=5, b=10)
15print(instance.a) # 5
16
17# Execute the original function with validated data
18result = pydantic_add.func(**instance.model_dump())
19print(result) # 15