feat(python): add Pydantic model instance auto-conversion and from_pydantic_model() classmethod#7383
Open
vinaysurtani wants to merge 3 commits into
Open
feat(python): add Pydantic model instance auto-conversion and from_pydantic_model() classmethod#7383vinaysurtani wants to merge 3 commits into
vinaysurtani wants to merge 3 commits into
Conversation
- 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
Contributor
|
ACTION NEEDED 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 PydanticBaseModelinstances by calling.model_dump()on each item and inferring the PyArrow schema from the resulting dictsLanceDataset.from_pydantic_model(model_class, data, uri=None, **kwargs)classmethod that infers the dataset URI from the model class name (snake_case) and delegates towrite_dataset()Before (required manual conversion):
After (just works):
Files Changed
python/python/lance/dependencies.py— added_PYDANTIC_AVAILABLEflag and_check_for_pydantic()helper, following the same optional-dependency pattern used for pandas and HuggingFacepython/python/lance/types.py— added Pydantic branch in_coerce_reader()before the genericIterablebranch; handles both Pydantic v1 (.dict()) and v2 (.model_dump())python/python/lance/dataset.py— addedfrom_pydantic_model()classmethod toLanceDatasetpython/python/tests/test_dataset.py— added Pydantic instances to the existingtest_input_dataparametrized suite and addedtest_from_pydantic_modelTest Results
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 withPermissionError: [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 inwrite_dataset()test_from_pydantic_model— classmethod writes data, infers schema, round-trips correctlyChecklist
write_dataset()without manual.model_dump()LanceDataset.from_pydantic_model()classmethod exists and workstest_input_dataparametrized cases still pass