Repository navigation
fix(python): write Enum fields of Pydantic models - #11083
Open
maharanay22 wants to merge 2 commits into
Open
maharanay22 wants to merge 2 commits into
maharanay22 wants to merge 2 commits into
Conversation
pydantic_to_schema stores an Enum field as the type of its values, but model_dump keeps the Enum members. Arrow cannot convert members of a plain Enum, so write_dataset and LanceDataset.from_pydantic_model failed with ArrowTypeError for any model with such a field. Store each member's value, including in nested models and lists.
Walking every element visited each float of embedding vectors, making Pydantic writes several times slower even without enums. Return lists of plain scalars unchanged, as lancedb#4357 does.
Contributor
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The scalar-list fast path fixes the previous vector-ingestion regression. Enum values still round-trip through both write entry points, including nested models and lists, and vector ingestion is back near its previous throughput.
This branch has not been deployed
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
Writing Pydantic model instances with a plain
Enumfield failed.pydantic_to_schemamaps the field to the type of the enum's values (a dictionary of strings for string values), but the rows come frommodel_dump(), which keeps the enum members. Arrow can only convert members that also subclassstrorint, solance.write_dataset(models, uri)andLanceDataset.from_pydantic_modelraisedArrowTypeError: Expected bytes, got a 'Color' objectfor a model like this.model_to_dictnow replaces each enum member with its value, including inside nested models and lists, which matches the schema the model maps to.Testing
Added
test_pydantic_enum_fields_store_enum_valuestotest_dataset.pyfor bothwrite_datasetandfrom_pydantic_model, with a string enum, an integer enum and an enum inside a nested model list. It fails on main and passes with the fix. The existing Pydantic tests intest_dataset.pyandtest_pydantic.pystill pass, andruff format --checkandruff checkare clean on the touched files.