Files
lancedb/docs/src/python/pydantic.md
Lei Xu 6fa1f37506 docs: improve pydantic integration docs (#2136)
Address usage mistakes in
https://github.com/lancedb/lancedb/issues/2135.

* Add example of how to use `LanceModel` and `Vector` decorator
* Add test for pydantic doc
* Fix the example to directly use LanceModel instead of calling
`MyModel.to_arrow_schema()` in the example.
* Add cross-reference link to pydantic doc site
* Configure mkdocs to watch code changes in python directory.
2025-02-21 12:48:37 -08:00

1.6 KiB

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.


--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