Skip to content

feat(python): add Pydantic model instance auto-conversion and from_pydantic_model() classmethod#7383

Open
vinaysurtani wants to merge 3 commits into
lance-format:mainfrom
vinaysurtani:fix-issue-1106
Open

feat(python): add Pydantic model instance auto-conversion and from_pydantic_model() classmethod#7383
vinaysurtani wants to merge 3 commits into
lance-format:mainfrom
vinaysurtani:fix-issue-1106

Conversation

@vinaysurtani

Copy link
Copy Markdown

Summary

Closes #1106

This PR adds first-class Pydantic model support to Lance so users no longer need to manually call .model_dump() before writing data.

Two changes:

  • write_dataset() now auto-converts a list of Pydantic BaseModel instances by calling .model_dump() on each item and inferring the PyArrow schema from the resulting dicts
  • New LanceDataset.from_pydantic_model(model_class, data, uri=None, **kwargs) classmethod that infers the dataset URI from the model class name (snake_case) and delegates to write_dataset()

Before (required manual conversion):

from pydantic import BaseModel
import pyarrow as pa, lance

class MyModel(BaseModel):
    name: str
    score: float

data = [MyModel(name="alice", score=0.9)]
schema = pa.schema([pa.field("name", pa.string()), pa.field("score", pa.float64())])
lance.write_dataset([m.model_dump() for m in data], "/tmp/test.lance", schema=schema)

After (just works):

lance.write_dataset(data, "/tmp/test.lance")
# or
ds = lance.LanceDataset.from_pydantic_model(MyModel, data, uri="/tmp/test.lance")

Files Changed

  • python/python/lance/dependencies.py — added _PYDANTIC_AVAILABLE flag and _check_for_pydantic() helper, following the same optional-dependency pattern used for pandas and HuggingFace
  • python/python/lance/types.py — added Pydantic branch in _coerce_reader() before the generic Iterable branch; handles both Pydantic v1 (.dict()) and v2 (.model_dump())
  • python/python/lance/dataset.py — added from_pydantic_model() classmethod to LanceDataset
  • python/python/tests/test_dataset.py — added Pydantic instances to the existing test_input_data parametrized suite and added test_from_pydantic_model

Test Results

python/python/tests/test_dataset.py: 196 passed, 1 skipped, 4 failed

The 4 failures (test_random_dataset_recall_accelerated, test_random_dataset_recall_accelerated_one_pass, test_count_index_rows_accelerated, test_count_index_rows_accelerated_one_pass) all fail with PermissionError: [Errno 13] Permission denied: 'nvcc' — these require the CUDA compiler toolkit and are pre-existing failures unrelated to this change. They do not touch any code modified in this PR.

New tests added:

  • test_input_data[NoneType-pydantic] — Pydantic instances auto-convert correctly in write_dataset()
  • test_from_pydantic_model — classmethod writes data, infers schema, round-trips correctly

Checklist

  • Pydantic instances can be passed directly to write_dataset() without manual .model_dump()
  • LanceDataset.from_pydantic_model() classmethod exists and works
  • Pydantic is treated as an optional dependency (no unconditional import)
  • Both Pydantic v1 and v2 are supported
  • All existing test_input_data parametrized cases still pass
  • New tests cover both new entry points
  • Rebased cleanly on upstream/main with no conflicts

- Add _PYDANTIC_AVAILABLE flag and _check_for_pydantic() to dependencies.py
- Add Pydantic branch in _coerce_reader() in types.py before generic
  Iterable branch to auto-convert BaseModel instances via model_dump()
- Supports both Pydantic v1 (.dict()) and v2 (.model_dump())

Fixes part of issue lance-format#1106
- Add from_pydantic_model() classmethod to LanceDataset that infers
  table name (snake_case) and schema from a Pydantic model class
- Add Pydantic instances to test_input_data parametrized test suite
- Add test_from_pydantic_model() to verify classmethod behavior

Fixes issue lance-format#1106
@github-actions github-actions Bot added the A-python Python bindings label Jun 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

ACTION NEEDED
Lance follows the Conventional Commits specification for release automation.

The PR title and description are used as the merge commit message. Please update your PR title and description to match the specification.

For details on the error please inspect the "PR Title Check" action.

@vinaysurtani vinaysurtani changed the title Add Pydantic model instance auto-conversion and from_pydantic_model() classmethod feat(python): add Pydantic model instance auto-conversion and from_pydantic_model() classmethod Jun 20, 2026
@github-actions github-actions Bot added the enhancement New feature or request label Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-python Python bindings enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

working with pydantic model instances

1 participant