From 06828e2842f9c40e9d4d1f65acb99468c3fb1e45 Mon Sep 17 00:00:00 2001 From: DogerW <51895873+DogerW666@users.noreply.github.com> Date: Mon, 30 Nov 2026 19:07:29 +0800 Subject: [PATCH] fix(python): handle JSON schema callbacks on LanceModel fields (#5300) Fixes #5299. Pydantic accepts a callback in `Field(json_schema_extra=...)` for customizing a field's JSON Schema. Creating an Embedded table from a model with such a field raised `AttributeError` when LanceDB tried to read embedding metadata with `.get()`. Read LanceDB metadata from mapping-style extras and leave callbacks to Pydantic. The regression test creates a table with the expected Arrow string column, then verifies that the callback still removes the field's default from its JSON Schema. ### Validation - The new test failed with the reported exception on `9a79107` before the fix. - All 97 tests in `test_pydantic.py` and `test_embedding_field_metadata.py` passed on Python 3.11.15 with Pydantic 2.13.4, using the checkout's Python sources and the published 0.42.0b38 native extension. - Verified create, insert and read-back on Python 3.12.7 with Pydantic 2.14.4. Ruff lint and formatting checks passed for the Python SDK. --- python/python/lancedb/pydantic.py | 8 ++++---- python/python/tests/test_pydantic.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/python/python/lancedb/pydantic.py b/python/python/lancedb/pydantic.py index 4d77ecd15..df38ec53b 100644 --- a/python/python/lancedb/pydantic.py +++ b/python/python/lancedb/pydantic.py @@ -9,6 +9,7 @@ import inspect import sys import types from abc import ABC, abstractmethod +from collections.abc import Mapping from datetime import date, datetime, time, timedelta from enum import Enum from typing import ( @@ -570,10 +571,9 @@ class LanceModel(pydantic.BaseModel): def get_extras(field_info: FieldInfo, key: str) -> Any: - """ - Get the extra metadata from a Pydantic FieldInfo. - """ - return (field_info.json_schema_extra or {}).get(key) + """Read LanceDB metadata from the mapping form of Pydantic's JSON Schema extras.""" + extras = field_info.json_schema_extra + return extras.get(key) if isinstance(extras, Mapping) else None def model_to_dict(model: pydantic.BaseModel) -> dict[str, Any]: diff --git a/python/python/tests/test_pydantic.py b/python/python/tests/test_pydantic.py index 93c42e6b7..ed0423868 100644 --- a/python/python/tests/test_pydantic.py +++ b/python/python/tests/test_pydantic.py @@ -592,6 +592,19 @@ def test_lance_model(): assert t == TestModel(vec=[0.0] * 16, li=[1, 2, 3]) +def test_lance_model_with_json_schema_callback(mem_db): + def remove_default(schema): + del schema["default"] + + class Document(LanceModel): + text: str = Field(default="", json_schema_extra=remove_default) + + table = mem_db.create_table("documents", schema=Document) + assert table.schema == pa.schema([pa.field("text", pa.string(), nullable=False)]) + field_schema = Document.model_json_schema()["properties"]["text"] + assert "default" not in field_schema + + def test_optional_nested_model(): class WAMedia(BaseModel): url: str