Pydantic

Pydantic is a data validation library in Python. LanceDB integrates with Pydantic for schema inference, data ingestion, and query result casting. Using [LanceModel][lancedb.pydantic.LanceModel], users can seamlessly integrate Pydantic with the rest of the LanceDB APIs.

python
--8<-- "python/python/tests/docs/test_pydantic_integration.py:imports"

--8<-- "python/python/tests/docs/test_pydantic_integration.py:base_model"

--8<-- "python/python/tests/docs/test_pydantic_integration.py:set_url"
--8<-- "python/python/tests/docs/test_pydantic_integration.py:base_example"

Vector Field

LanceDB provides a Vector(dim) method to define a vector Field in a Pydantic Model.

::: lancedb.pydantic.Vector

Type Conversion

LanceDB automatically convert Pydantic fields to Apache Arrow DataType .

Current supported type conversions:

Pydantic Field Type PyArrow Data Type
int pyarrow.int64
float pyarrow.float64
bool pyarrow.bool
str pyarrow.utf8()
list pyarrow.List
BaseModel pyarrow.Struct
Vector(n) pyarrow.FixedSizeList(float32, n)

LanceDB supports to create Apache Arrow Schema from a [Pydantic BaseModel][pydantic.BaseModel] via pydantic_to_schema() method.

::: lancedb.pydantic.pydantic_to_schema