Pydantic Integration
FactBaseModel
Bases: BaseModel
, Fact
A Pydantic BaseModel that mixes in typed-logic Fact.
You can use this class instead of Pydantic BaseModel
, it will map your class onto a
PredicateDefinition
.
Additionally, it allows for positional arguments, which is not the default in Pydantic.
Example:
class PersonAge(FactBaseModel):
name: str
age: int
This creates a PredicateDefinition for a two-place (arity=2) predicate relating a person to their age.
This can be instantiated positionally:
pa1 = PersonAge("Akira", 33)
Or with keywords:
pa1 = PersonAge(name="Akira", age=33)
This inherits from Fact
, which means that these can be used as ground terms in a theory.
To convert the domain instance to a generic object, using to_model_object
assert PersonAge(name="Akira", age=33).to_model_object() == Term("PersonAge", "Akira", 33)
Limitations
We currently use Pydantic's model_json_schema()
to convert a pydantic model to JSON-Schema to
extract type metadata, but this is currently lossy. You may find that your types are
converted to strings.
Source code in src/typedlogic/integrations/frameworks/pydantic/pydantic_bridge.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|