feat(python)!: rename the @udf decorator to @function (#4837)

The client registers Functions, but the decorator that packages one was
called `udf` and produced a `UdfDefinition`. Rename them to `function`
and `FunctionDefinition`, and say "function" instead of "UDF" in the
docstrings, error messages and docs around them.

`udf` and `UdfDefinition` stay as deprecated aliases that emit a
`DeprecationWarning`. The source packager strips either spelling of the
decorator, so `@udf` and `@function` produce the same artifact and
digest, and the wire contract is unchanged. DataFusion and Lance UDF
types, and Geneva's `@udf` in the integration docs, are unrelated and
untouched.
This commit is contained in:
Wyatt Alt
2026-11-05 00:36:20 -08:00
committed by GitHub
parent 7abc57a4d0
commit b5c5ede39f
16 changed files with 367 additions and 289 deletions
@@ -32,11 +32,11 @@ from lancedb.functions import (
PythonRuntimeSpec,
SecretBinding,
SecretReference,
UdfDefinition,
FunctionDefinition,
_canonical_arrow_type,
_exact_arrow_type,
_GRAMMAR_PRIMITIVES,
udf,
function,
)
from lancedb.secrets import EnvVarSecret
from lancedb.remote.db import RemoteDBConnection
@@ -62,7 +62,7 @@ FUNCTION_VERSION = json.loads(
)["version"]
@udf(
@function(
pip=["numpy>=2"],
env={"MODE": "test"},
python_version="3.12",
@@ -71,7 +71,7 @@ def normalize_score(value: float) -> float:
return value / 100.0
@udf(
@function(
pip=["openai==3.7.0"],
env={"MODE": "test"},
python_version="3.12",
@@ -80,8 +80,8 @@ def analyze_caption(caption: str) -> str:
return caption.strip()
def test_scalar_udf_matches_shared_registration_golden_and_remains_callable():
assert isinstance(normalize_score, UdfDefinition)
def test_scalar_function_matches_shared_registration_golden_and_remains_callable():
assert isinstance(normalize_score, FunctionDefinition)
assert normalize_score(25.0) == 0.25
assert (
normalize_score.registration_request.to_canonical_json()
@@ -96,13 +96,13 @@ def test_scalar_udf_matches_shared_registration_golden_and_remains_callable():
}
def test_udf_declares_pinned_hugging_face_models():
def test_function_declares_pinned_hugging_face_models():
model = HuggingFaceModel(
repo="sentence-transformers/all-MiniLM-L6-v2",
revision="1110a243fdf4706b3f48f1d95db1a4f5529b4d41",
)
@udf(pip=["sentence-transformers==3.3.1"], models=[model])
@function(pip=["sentence-transformers==3.3.1"], models=[model])
def embed_text(value: str) -> list[float]:
return [1.0]
@@ -117,19 +117,19 @@ def test_udf_declares_pinned_hugging_face_models():
assert "models" not in undeclared
with pytest.raises(ValueError, match="once"):
udf(name="duplicate_model", models=[model, model])(lambda value: value)
function(name="duplicate_model", models=[model, model])(lambda value: value)
with pytest.raises(ValueError, match="40-character"):
HuggingFaceModel(repo="owner/model", revision="main")
with pytest.raises(ValueError, match="reserve"):
udf(
function(
name="reserved_model_env",
models=[model],
env={"HF_HOME": "/tmp/model"},
)(lambda value: value)
def test_udf_execution_controls_match_shared_registration_golden():
controlled = udf(
def test_function_execution_controls_match_shared_registration_golden():
controlled = function(
pip=["numpy>=2"],
env={"MODE": "test"},
python_version="3.12",
@@ -144,8 +144,8 @@ def test_udf_execution_controls_match_shared_registration_golden():
)
def test_udf_execution_controls_apply_to_vectorized_functions():
@udf(
def test_function_execution_controls_apply_to_vectorized_functions():
@function(
input_schema=pa.schema([pa.field("value", pa.int64(), nullable=False)]),
output_schema=pa.int64(),
batch_size=3,
@@ -171,28 +171,28 @@ def test_udf_execution_controls_apply_to_vectorized_functions():
({"row_timeout_seconds": True}, "row_timeout_seconds"),
],
)
def test_udf_execution_controls_require_their_positive_ranges(options, field):
def test_function_execution_controls_require_their_positive_ranges(options, field):
def identity(value: int) -> int:
return value
with pytest.raises(ValueError, match=field):
udf(**options)(identity)
function(**options)(identity)
@pytest.mark.parametrize(
"name", ["examples.image_info", "image-info", "image_info.", "image_info-"]
)
def test_udf_rejects_function_names_with_periods_or_hyphens(name):
def test_function_rejects_function_names_with_periods_or_hyphens(name):
with pytest.raises(ValueError, match="invalid Function name"):
@udf(name=name)
@function(name=name)
def image_info(value: int) -> int:
return value
@pytest.mark.parametrize("name", ["image_info", "ImageInfo123", "_image_info", "f"])
def test_udf_accepts_function_names_with_letters_digits_and_underscores(name):
@udf(name=name)
def test_function_accepts_function_names_with_letters_digits_and_underscores(name):
@function(name=name)
def image_info(value: int) -> int:
return value
@@ -200,7 +200,7 @@ def test_udf_accepts_function_names_with_letters_digits_and_underscores(name):
assert image_info(1) == 1
def test_secret_bound_udf_matches_its_shared_registration_golden():
def test_secret_bound_function_matches_its_shared_registration_golden():
assert analyze_caption(" hello ") == "hello"
bound = analyze_caption.bind_secrets(
[EnvVarSecret(secret_name="openai-prod", env_variable="OPENAI_API_KEY")]
@@ -446,15 +446,15 @@ def test_a_secret_name_admits_what_a_namespace_name_does():
)
def _main_udf_source(
def _main_function_source(
*, threshold: int = 20, input_annotation: str = "int", comparison: str = ">="
) -> str:
return (
"from __future__ import annotations\n"
"from lancedb.functions import udf\n"
"from lancedb.functions import function\n"
f"THRESHOLD = {threshold}\n"
"\n"
"@udf\n"
"@function\n"
f"def label(value: {input_annotation}) -> str:\n"
f" return 'big' if value {comparison} THRESHOLD else 'small'\n"
"\n"
@@ -463,7 +463,7 @@ def _main_udf_source(
)
def _run_main_udf(path: Path, source: str) -> dict:
def _run_main_function(path: Path, source: str) -> dict:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(source)
result = subprocess.run(
@@ -475,15 +475,15 @@ def _run_main_udf(path: Path, source: str) -> dict:
return json.loads(result.stdout)
def test_main_udf_registration_identity_is_stable_across_processes_and_paths(
def test_main_function_registration_identity_is_stable_across_processes_and_paths(
tmp_path,
):
source = _main_udf_source()
source = _main_function_source()
original_path = tmp_path / "original" / "job.py"
moved_path = tmp_path / "moved" / "renamed_job.py"
original_runs = [_run_main_udf(original_path, source) for _ in range(2)]
moved_run = _run_main_udf(moved_path, source)
original_runs = [_run_main_function(original_path, source) for _ in range(2)]
moved_run = _run_main_function(moved_path, source)
assert len({run["artifact"]["digest"] for run in [*original_runs, moved_run]}) == 1
assert all(
@@ -492,15 +492,15 @@ def test_main_udf_registration_identity_is_stable_across_processes_and_paths(
)
assert original_runs[0] == original_runs[1] == moved_run
body_change = _run_main_udf(
tmp_path / "changes" / "body.py", _main_udf_source(comparison=">")
body_change = _run_main_function(
tmp_path / "changes" / "body.py", _main_function_source(comparison=">")
)
global_change = _run_main_udf(
tmp_path / "changes" / "global.py", _main_udf_source(threshold=21)
global_change = _run_main_function(
tmp_path / "changes" / "global.py", _main_function_source(threshold=21)
)
annotation_change = _run_main_udf(
annotation_change = _run_main_function(
tmp_path / "changes" / "annotation.py",
_main_udf_source(input_annotation="float"),
_main_function_source(input_annotation="float"),
)
baseline = original_runs[0]
@@ -524,11 +524,13 @@ def _run_packaged(definition, *args):
"""Execute the shipped artifact in a fresh namespace, as a worker would."""
source = base64.b64decode(definition.registration_request.artifact.content.data)
namespace: dict = {}
exec(compile(source, "<udf>", "exec"), namespace)
exec(compile(source, "<function>", "exec"), namespace)
return namespace[definition.registration_request.artifact.entrypoint](*args)
def _execute_registered_array_udf_v1(request: dict, batch: pa.RecordBatch) -> pa.Array:
def _execute_registered_array_function_v1(
request: dict, batch: pa.RecordBatch
) -> pa.Array:
"""Exercise adapter-v1 annotation dispatch over a registered artifact."""
assert request["artifact"]["adapter"] == {
"kind": "scalar_to_arrow_batch",
@@ -542,7 +544,7 @@ def _execute_registered_array_udf_v1(request: dict, batch: pa.RecordBatch) -> pa
"RecordBatch": pa.RecordBatch,
"typing": typing,
}
exec(compile(source, "<registered-udf>", "exec"), namespace)
exec(compile(source, "<registered-function>", "exec"), namespace)
function = namespace[request["artifact"]["entrypoint"]]
parameters = tuple(inspect.signature(function).parameters.values())
annotations = typing.get_type_hints(function)
@@ -555,8 +557,8 @@ def _execute_registered_array_udf_v1(request: dict, batch: pa.RecordBatch) -> pa
return result
def test_udf_conda_environment():
@udf(conda=["scipy", "numpy"], conda_channels=["conda-forge", "defaults"])
def test_function_conda_environment():
@function(conda=["scipy", "numpy"], conda_channels=["conda-forge", "defaults"])
def halve(value: float) -> float:
return value / 2
@@ -570,13 +572,13 @@ def test_udf_conda_environment():
assert "channels" not in pip_request["runtime"]["environment"]
with pytest.raises(ValueError, match="not both"):
udf(name="both", pip=["numpy"], conda=["numpy"])(lambda value: value)
function(name="both", pip=["numpy"], conda=["numpy"])(lambda value: value)
with pytest.raises(ValueError, match="requires conda"):
udf(name="channels", conda_channels=["conda-forge"])(lambda value: value)
function(name="channels", conda_channels=["conda-forge"])(lambda value: value)
def test_udf_apt_system_packages():
@udf(pip=["torchcodec"], apt=["libavutil58", "libavcodec60", "libavutil58"])
def test_function_apt_system_packages():
@function(pip=["torchcodec"], apt=["libavutil58", "libavcodec60", "libavutil58"])
def decode(value: float) -> float:
return value
@@ -587,7 +589,7 @@ def test_udf_apt_system_packages():
"system_packages": ["libavcodec60", "libavutil58"],
}
@udf(conda=["numpy"], apt=["libsndfile1"])
@function(conda=["numpy"], apt=["libsndfile1"])
def conda_decode(value: float) -> float:
return value
@@ -596,14 +598,14 @@ def test_udf_apt_system_packages():
pip_request = json.loads(normalize_score.registration_request.to_canonical_json())
assert "system_packages" not in pip_request["runtime"]["environment"]
with pytest.raises(ValueError, match="trimmed"):
udf(name="padded", apt=[" libsndfile1"])(lambda value: value)
function(name="padded", apt=[" libsndfile1"])(lambda value: value)
@pytest.mark.parametrize("packages", [{"pip": ["numpy"]}, {"conda": ["numpy"]}])
def test_udf_image(packages):
def test_function_image(packages):
image = "ghcr.io/acme/embedder@sha256:" + "a" * 64
@udf(image=image, **packages)
@function(image=image, **packages)
def embed(value: float) -> float:
return value
@@ -616,13 +618,13 @@ def test_udf_image(packages):
@pytest.mark.parametrize(
"image", ["ghcr.io/acme/embedder:latest", "embedder@sha256:" + "a" * 64, ""]
)
def test_udf_image_requires_digest_reference(image):
def test_function_image_requires_digest_reference(image):
with pytest.raises(ValueError, match="sha256"):
udf(name="pinned", image=image)(lambda value: value)
function(name="pinned", image=image)(lambda value: value)
def test_udf_gpu_marker_uses_gpu_runtime():
@udf(pip=["cupy-cuda12x"], gpu=True, gpu_class="a10g")
def test_function_gpu_marker_uses_gpu_runtime():
@function(pip=["cupy-cuda12x"], gpu=True, gpu_class="a10g")
def double_on_gpu(value: int) -> int:
return value * 2
@@ -631,7 +633,7 @@ def test_udf_gpu_marker_uses_gpu_runtime():
assert request["runtime"]["gpu"] is True
assert request["runtime"]["gpu_class"] == "a10g"
@udf(pip=["pyarrow"])
@function(pip=["pyarrow"])
def cpu_function(value: int) -> int:
return value
@@ -647,12 +649,12 @@ def test_udf_gpu_marker_uses_gpu_runtime():
for invalid in [None, 0, 1, -1, 1.5, "", "true", "1", "H100"]:
with pytest.raises(ValueError, match="gpu must be a boolean"):
udf(name="invalid_gpu", gpu=invalid)(identity)
function(name="invalid_gpu", gpu=invalid)(identity)
for invalid in ["H100", "a_b", "-a10g", "a10g-", "a" * 64]:
with pytest.raises(ValueError, match="gpu_class"):
udf(name="invalid_gpu_class", gpu=True, gpu_class=invalid)(identity)
function(name="invalid_gpu_class", gpu=True, gpu_class=invalid)(identity)
with pytest.raises(ValueError, match="requires gpu=True"):
udf(name="gpu_class_without_gpu", gpu_class="a10g")(identity)
function(name="gpu_class_without_gpu", gpu_class="a10g")(identity)
base_runtime = {
"kind": "python_v2",
@@ -692,8 +694,8 @@ def test_unknown_runtime_discards_payload_before_known_field_validation():
assert runtime.to_canonical_json() == '{"kind":"python_v3"}'
def test_udf_packages_attribute_access_and_body_imports():
@udf
def test_function_packages_attribute_access_and_body_imports():
@function
def word_norm(body: str) -> float:
import numpy as np
@@ -706,14 +708,14 @@ def test_udf_packages_attribute_access_and_body_imports():
assert _run_packaged(word_norm, "aa bb") == pytest.approx(8**0.5)
def test_udf_packages_module_globals_and_global_caches():
@udf
def test_function_packages_module_globals_and_global_caches():
@function
def label(value: int) -> str:
return "big" if value >= THRESHOLD else "small"
assert _run_packaged(label, 21) == "big"
@udf
@function
def cached(value: int) -> int:
global _CACHE
if _CACHE is None:
@@ -723,16 +725,16 @@ def test_udf_packages_module_globals_and_global_caches():
assert _run_packaged(cached, 2) == 42
def test_udf_annotations_are_not_runtime_names():
@udf
def test_function_annotations_are_not_runtime_names():
@function
def identity(value: date) -> date:
return value
assert _run_packaged(identity, date(2026, 8, 25)) == date(2026, 8, 25)
def test_udf_nested_scopes_resolve_lexically():
@udf
def test_function_nested_scopes_resolve_lexically():
@function
def score(value: int) -> int:
offset = 2
@@ -744,7 +746,7 @@ def test_udf_nested_scopes_resolve_lexically():
assert _run_packaged(score, 3) == 5
def test_udf_resolves_module_globals_before_builtins(tmp_path):
def test_function_resolves_module_globals_before_builtins(tmp_path):
module_path = tmp_path / "shadowing_udfs.py"
module_path.write_text(
"max = 7\n"
@@ -765,10 +767,10 @@ def test_udf_resolves_module_globals_before_builtins(tmp_path):
spec.loader.exec_module(module)
# The module's `max = 7` is what the interpreter would use, so it ships.
assert _run_packaged(udf(module.uses_literal_shadow), 1) == 8
assert _run_packaged(function(module.uses_literal_shadow), 1) == 8
# A callable global cannot ship; it must not be silently swapped for the builtin.
with pytest.raises(TypeError, match="unsupported global value of type function"):
udf(module.uses_callable_shadow)
function(module.uses_callable_shadow)
def test_canonical_arrow_type_prefers_the_compact_grammar():
@@ -801,8 +803,8 @@ def test_canonical_arrow_type_prefers_the_compact_grammar():
_canonical_arrow_type(outside)
def test_udf_nested_annotations_are_postponed_in_the_artifact():
@udf
def test_function_nested_annotations_are_postponed_in_the_artifact():
@function
def score(value: int) -> int:
def identity(item: date) -> date:
return item
@@ -813,8 +815,8 @@ def test_udf_nested_annotations_are_postponed_in_the_artifact():
assert _run_packaged(score, 3) == 3
def test_udf_ships_globals_the_body_deletes():
@udf
def test_function_ships_globals_the_body_deletes():
@function
def clear(value: int) -> int:
global _CACHE
del _CACHE
@@ -823,7 +825,7 @@ def test_udf_ships_globals_the_body_deletes():
assert _run_packaged(clear, 3) == 3
def test_udf_rejects_a_module_global_that_does_not_import_as_itself(tmp_path):
def test_function_rejects_a_module_global_that_does_not_import_as_itself(tmp_path):
module_path = tmp_path / "fake_module_udfs.py"
module_path.write_text(
"import types\n"
@@ -837,10 +839,10 @@ def test_udf_rejects_a_module_global_that_does_not_import_as_itself(tmp_path):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
with pytest.raises(TypeError, match="does not import as 'numpy'"):
udf(module.score)
function(module.score)
def test_udf_rejects_a_module_level_namespace_alias(tmp_path):
def test_function_rejects_a_module_level_namespace_alias(tmp_path):
module_path = tmp_path / "aliasing_udfs.py"
module_path.write_text(
"import builtins as b\n"
@@ -853,7 +855,7 @@ def test_udf_rejects_a_module_level_namespace_alias(tmp_path):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
with pytest.raises(ValueError, match="dynamic namespace access"):
udf(module.score)
function(module.score)
@pytest.mark.parametrize(
@@ -866,7 +868,7 @@ def test_udf_rejects_a_module_level_namespace_alias(tmp_path):
"sys.modules[__name__].THRESHOLD",
],
)
def test_udf_rejects_dynamic_namespace_access(access):
def test_function_rejects_dynamic_namespace_access(access):
namespace: dict = {}
exec(
f"def score(value: int) -> int:\n return value + {access}\n",
@@ -886,9 +888,11 @@ def _package_from_text(source: str, module_globals: dict | None = None):
import tempfile
directory = tempfile.mkdtemp()
path = Path(directory) / "generated_udf_module.py"
path = Path(directory) / "generated_function_module.py"
path.write_text(source)
spec = importlib.util.spec_from_file_location(f"generated_udf_{id(source)}", path)
spec = importlib.util.spec_from_file_location(
f"generated_function_{id(source)}", path
)
module = importlib.util.module_from_spec(spec)
if module_globals:
module.__dict__.update(module_globals)
@@ -898,10 +902,10 @@ def _package_from_text(source: str, module_globals: dict | None = None):
for value in vars(module).values()
if callable(value) and getattr(value, "__module__", None) == module.__name__
]
return udf(functions[0])
return function(functions[0])
def test_udf_rejects_a_non_standard_builtins_environment():
def test_function_rejects_a_non_standard_builtins_environment():
def score(value: int) -> int:
return len([1]) + value
@@ -916,7 +920,7 @@ def test_udf_rejects_a_non_standard_builtins_environment():
patched.__annotations__ = score.__annotations__
assert patched(3) == 102
with pytest.raises(ValueError, match="non-standard builtins environment"):
udf(patched)
function(patched)
class ReportingDict(dict): # reports standard entries, resolves differently
def __missing__(self, key):
@@ -928,7 +932,7 @@ def test_udf_rejects_a_non_standard_builtins_environment():
disguised.__annotations__ = score.__annotations__
assert disguised(3) == 102
with pytest.raises(ValueError, match="non-standard builtins environment"):
udf(disguised)
function(disguised)
hooked = types.FunctionType(
score.__code__,
@@ -937,10 +941,10 @@ def test_udf_rejects_a_non_standard_builtins_environment():
)
hooked.__annotations__ = score.__annotations__
with pytest.raises(ValueError, match="non-standard builtins environment"):
udf(hooked)
function(hooked)
def test_udf_recursion_versus_a_rebound_module_name(tmp_path):
def test_function_recursion_versus_a_rebound_module_name(tmp_path):
module_path = tmp_path / "rebound_udfs.py"
module_path.write_text(
"def fact(value: int) -> int:\n"
@@ -952,20 +956,20 @@ def test_udf_recursion_versus_a_rebound_module_name(tmp_path):
spec = importlib.util.spec_from_file_location("rebound_udfs", module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
assert _run_packaged(udf(module.fact), 5) == 120
assert _run_packaged(function(module.fact), 5) == 120
raw = module.score
module.score = 10
with pytest.raises(ValueError, match="binds that name to another value"):
udf(raw)
function(raw)
# A wrapper that merely exposes __wrapped__ is not the function.
module.score = functools.wraps(raw)(lambda value: 41)
with pytest.raises(ValueError, match="binds that name to another value"):
udf(raw)
function(raw)
# The decorator's own result is; a subclass of it is not.
module.fact = udf(module.fact)
module.fact = function(module.fact)
assert _run_packaged(module.fact, 4) == 24
class Twisted(UdfDefinition):
class Twisted(FunctionDefinition):
def __call__(self, *args, **kwargs):
return 41
@@ -980,7 +984,7 @@ def test_udf_recursion_versus_a_rebound_module_name(tmp_path):
python_version=None,
)
with pytest.raises(ValueError, match="binds that name to another value"):
udf(raw_fact)
function(raw_fact)
def test_canonical_arrow_type_uses_exact_json_for_list_child_properties():
@@ -1051,12 +1055,12 @@ def _shadows_missing_in_a_lambda(value: int) -> int:
@pytest.mark.parametrize(
"function",
"target",
[_calls_missing, _shadows_missing_in_a_comprehension, _shadows_missing_in_a_lambda],
)
def test_udf_rejects_a_truly_unresolved_global(function):
def test_function_rejects_a_truly_unresolved_global(target):
with pytest.raises(ValueError, match=r"unresolved global names: \['missing'\]"):
udf(function)
function(target)
def _arrow_type_from_golden(spec: dict) -> pa.DataType:
@@ -1111,7 +1115,7 @@ def test_explicit_arrow_schema_is_deterministic():
nullable=False,
)
@udf(input_schema=input_schema, output_schema=output_schema)
@function(input_schema=input_schema, output_schema=output_schema)
def explicit(value):
return [value, value, value]
@@ -1123,7 +1127,7 @@ def test_explicit_arrow_schema_is_deterministic():
def test_blob_fields_use_the_scalar_function_semantic_type():
@udf(
@function(
input_schema=pa.schema([lancedb.blob("image", nullable=False)]),
output_schema=lancedb.blob("result", nullable=False),
)
@@ -1137,12 +1141,12 @@ def test_blob_fields_use_the_scalar_function_semantic_type():
def test_blob_input_access_is_inferred_from_annotations():
@udf
@function
def read_blob(video: BlobReader, timestamp_s: float) -> bytes:
video.seek(int(timestamp_s))
return video.read(4)
@udf
@function
def read_bytes(video: bytes) -> int:
return len(video)
@@ -1159,11 +1163,11 @@ def test_blob_input_access_is_inferred_from_annotations():
def test_nullable_blob_reader_and_explicit_blob_bytes():
@udf
@function
def nullable(video: Optional[BlobReader]) -> int:
return 0 if video is None else video.size()
@udf(
@function(
input_schema=pa.schema([lancedb.blob("video", nullable=False)]),
output_schema=pa.field("size", pa.int64(), nullable=False),
)
@@ -1183,7 +1187,7 @@ def test_blob_reader_rejects_an_incompatible_explicit_schema():
return video.size()
with pytest.raises(TypeError, match="requires a Blob V2 field"):
udf(
function(
read,
input_schema=pa.schema([pa.field("video", pa.int64(), nullable=False)]),
output_schema=pa.field("size", pa.int64(), nullable=False),
@@ -1191,7 +1195,7 @@ def test_blob_reader_rejects_an_incompatible_explicit_schema():
def test_whole_named_struct_function_can_include_a_blob_result_field():
@udf(
@function(
input_schema=pa.schema([lancedb.blob("image", nullable=False)]),
output_schema=pa.field(
"payload",
@@ -1245,7 +1249,7 @@ def test_struct_blob_signature_fields_preserve_exact_metadata_and_nullability():
nullable=False,
)
@udf(input_schema=pa.schema([nested_input]), output_schema=nested_output)
@function(input_schema=pa.schema([nested_input]), output_schema=nested_output)
def copy_payload(payload):
return payload
@@ -1289,7 +1293,7 @@ def test_struct_blob_signature_supports_multiple_struct_levels():
nullable=False,
)
@udf(
@function(
input_schema=pa.schema([recursive]),
output_schema=pa.field("size", pa.int64(), nullable=False),
)
@@ -1310,7 +1314,7 @@ def test_struct_blob_signature_supports_multiple_struct_levels():
],
)
def test_blob_output_accepts_list_ancestors(data_type):
@udf(
@function(
input_schema=pa.schema([lancedb.blob("video", nullable=False)]),
output_schema=pa.field("frames", data_type, nullable=False),
)
@@ -1335,7 +1339,7 @@ def test_blob_output_accepts_list_ancestors(data_type):
def test_blob_output_rejects_map_and_fixed_size_list_ancestors(data_type):
with pytest.raises(TypeError, match="nested under collection types"):
@udf(
@function(
input_schema=pa.schema([pa.field("id", pa.int64(), nullable=False)]),
output_schema=pa.field("frames", data_type, nullable=False),
)
@@ -1346,7 +1350,7 @@ def test_blob_output_rejects_map_and_fixed_size_list_ancestors(data_type):
def test_blob_output_named_struct_accepts_list_ancestor():
frames_type = pa.list_(lancedb.blob("item", nullable=False))
@udf(
@function(
input_schema=pa.schema([pa.field("id", pa.int64(), nullable=False)]),
output_schema=pa.schema(
[
@@ -1384,7 +1388,7 @@ def test_blob_signature_rejects_collection_ancestors(data_type):
match="Blob v2 fields nested under collection types are not supported",
):
@udf(
@function(
input_schema=pa.schema([pa.field("value", data_type, nullable=False)]),
output_schema=pa.field("size", pa.int64(), nullable=False),
)
@@ -1411,7 +1415,7 @@ def test_blob_signature_rejects_collection_below_a_struct():
match="Blob v2 fields nested under collection types are not supported",
):
@udf(
@function(
input_schema=pa.schema([nested]),
output_schema=pa.field("size", pa.int64(), nullable=False),
)
@@ -1420,7 +1424,7 @@ def test_blob_signature_rejects_collection_below_a_struct():
def test_blob_fields_support_vectorized_pyarrow_arrays():
@udf(
@function(
input_schema=pa.schema([lancedb.blob("image", nullable=True)]),
output_schema=lancedb.blob("result", nullable=False),
)
@@ -1433,7 +1437,7 @@ def test_blob_fields_support_vectorized_pyarrow_arrays():
values = pa.array([b"large blob", b"", None], type=pa.large_binary())
request = json.loads(copy_blobs.registration_request.to_canonical_json())
batch = pa.RecordBatch.from_arrays([values], names=["image"])
result = _execute_registered_array_udf_v1(request, batch)
result = _execute_registered_array_function_v1(request, batch)
assert isinstance(result, pa.LargeBinaryArray)
assert result.to_pylist() == [b"large blob", b"", b""]
@@ -1454,7 +1458,7 @@ def test_blob_fields_support_vectorized_pyarrow_arrays():
def test_documented_blob_array_identity_executes_packaged_artifact():
@udf(
@function(
input_schema=pa.schema([lancedb.blob("image", nullable=False)]),
output_schema=lancedb.blob("copy", nullable=False),
)
@@ -1463,7 +1467,7 @@ def test_documented_blob_array_identity_executes_packaged_artifact():
values = pa.array([b"large blob", b""], type=pa.large_binary())
request = json.loads(copy_blobs.registration_request.to_canonical_json())
result = _execute_registered_array_udf_v1(
result = _execute_registered_array_function_v1(
request,
pa.RecordBatch.from_arrays([values], names=["image"]),
)
@@ -1471,7 +1475,7 @@ def test_documented_blob_array_identity_executes_packaged_artifact():
def test_named_struct_function_can_include_a_blob_result_field():
@udf(
@function(
input_schema=pa.schema([lancedb.blob("image", nullable=False)]),
output_schema=pa.schema(
[
@@ -1492,7 +1496,7 @@ def test_named_struct_function_can_include_a_blob_result_field():
def test_named_struct_function_preserves_nullable_result_fields():
@udf(
@function(
input_schema=pa.schema([pa.field("value", pa.int64(), nullable=False)]),
output_schema=pa.schema(
[
@@ -1510,7 +1514,7 @@ def test_named_struct_function_preserves_nullable_result_fields():
("failure_code", False),
]
@udf(
@function(
input_schema=pa.schema([pa.field("value", pa.int64(), nullable=False)]),
output_schema=pa.schema(
[
@@ -1540,7 +1544,7 @@ def test_metadata_marked_blob_field_uses_the_semantic_type():
metadata={"ARROW:extension:name": "lance.blob.v2"},
)
@udf(
@function(
input_schema=pa.schema([metadata_blob]),
output_schema=pa.field("size", pa.int64(), nullable=False),
)
@@ -1560,7 +1564,7 @@ def test_blob_marker_rejects_invalid_storage_layout():
with pytest.raises(TypeError, match="requires a supported Blob storage layout"):
@udf(
@function(
input_schema=pa.schema([malformed]),
output_schema=pa.field("size", pa.int64(), nullable=False),
)
@@ -1587,7 +1591,7 @@ def test_nested_non_blob_extension_is_not_silently_unwrapped():
)
with pytest.raises(TypeError, match="unsupported Arrow type"):
@udf(
@function(
input_schema=pa.schema([nested]),
output_schema=pa.field("result", pa.int64(), nullable=False),
)
@@ -1599,7 +1603,7 @@ def test_explicit_large_utf8_schemas_use_the_canonical_function_name():
input_schema = pa.schema([pa.field("text", pa.large_string(), nullable=True)])
output_schema = pa.field("result", pa.large_string(), nullable=False)
@udf(input_schema=input_schema, output_schema=output_schema)
@function(input_schema=input_schema, output_schema=output_schema)
def preserve(text):
return text
@@ -1625,7 +1629,7 @@ def test_explicit_large_utf8_schemas_use_the_canonical_function_name():
def test_large_binary_schemas_use_the_canonical_function_name():
payload = pa.field("data", pa.large_binary(), nullable=True)
@udf(
@function(
input_schema=pa.schema([payload]),
output_schema=pa.field("result", pa.large_binary(), nullable=False),
)
@@ -1696,7 +1700,7 @@ def test_nested_struct_output_uses_canonical_exact_json():
]
)
@udf(
@function(
input_schema=pa.schema([pa.field("text", pa.string(), nullable=False)]),
output_schema=pa.field("analysis", analysis, nullable=False),
)
@@ -1746,31 +1750,31 @@ def test_nested_struct_output_uses_canonical_exact_json():
def test_annotation_and_explicit_schema_validation_fail_closed():
with pytest.raises(TypeError, match="missing Function annotations"):
@udf
@function
def missing(value):
return value
with pytest.raises(TypeError, match="unsupported Function annotation"):
@udf
@function
def unsupported(value: set[str]) -> str:
return ""
with pytest.raises(ValueError, match="output must be non-nullable"):
@udf
@function
def nullable_output(value: int) -> Optional[int]:
return value
with pytest.raises(TypeError, match=r"missing Function annotations: \['return'\]"):
@udf(input_schema=pa.schema([pa.field("value", pa.int64())]))
@function(input_schema=pa.schema([pa.field("value", pa.int64())]))
def partial_schema(value):
return value
with pytest.raises(ValueError, match="exactly match callable parameters"):
@udf(
@function(
input_schema=pa.schema([pa.field("other", pa.int64())]),
output_schema=pa.int64(),
)
@@ -1779,7 +1783,7 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
with pytest.raises(ValueError, match="output must be non-nullable"):
@udf(
@function(
input_schema=pa.schema([pa.field("value", pa.int64())]),
output_schema=pa.field("result", pa.int64(), nullable=True),
)
@@ -1792,7 +1796,7 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
]:
with pytest.raises(TypeError, match="unsupported Arrow type"):
@udf(
@function(
input_schema=pa.schema([pa.field("value", pa.int64())]),
output_schema=pa.schema([invalid_field]),
)
@@ -1801,7 +1805,7 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
with pytest.raises(TypeError, match="unsupported Arrow type"):
@udf(
@function(
input_schema=pa.schema(
[pa.field("value", pa.int64(), metadata={"k": "v"})]
),
@@ -1812,7 +1816,7 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
with pytest.raises(TypeError, match="unsupported Arrow type"):
@udf(
@function(
input_schema=pa.schema([pa.field("value", pa.int64())]),
output_schema=pa.field(
"result", pa.int64(), nullable=False, metadata={"k": "v"}
@@ -1824,7 +1828,7 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
struct_type = pa.struct([pa.field("value", pa.int64(), nullable=False)])
with pytest.raises(TypeError, match="unsupported Arrow type"):
@udf(
@function(
input_schema=pa.schema([pa.field("value", pa.int64())]),
output_schema=pa.field(
"result", struct_type, nullable=False, metadata={"k": "v"}
@@ -1848,7 +1852,7 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
]:
with pytest.raises(TypeError, match="schema metadata"):
@udf(input_schema=input_schema, output_schema=output_schema)
@function(input_schema=input_schema, output_schema=output_schema)
def schema_metadata(value):
return value
@@ -2093,8 +2097,8 @@ def test_building_a_binding_contacts_no_server():
assert state["requests"] == []
def test_vectorized_blob_udf_executes_packaged_artifact_and_publishes(tmp_path):
@udf(
def test_vectorized_blob_function_executes_packaged_artifact_and_publishes(tmp_path):
@function(
input_schema=pa.schema([lancedb.blob("image", nullable=True)]),
output_schema=lancedb.blob("copy", nullable=False),
)
@@ -2135,7 +2139,7 @@ def test_vectorized_blob_udf_executes_packaged_artifact_and_publishes(tmp_path):
)
hydrated = source.fetch_blobs("image", input_hits)
assert isinstance(hydrated, pa.LargeBinaryArray)
result = _execute_registered_array_udf_v1(
result = _execute_registered_array_function_v1(
registration,
pa.RecordBatch.from_arrays([hydrated], names=["image"]),
)
@@ -2181,7 +2185,7 @@ def test_blob_list_output_fresh_artifact_survives_local_reopen(
input_type = physical_list(pa.int64())
item = lancedb.blob("item", nullable=True)
@udf(
@function(
input_schema=pa.schema([pa.field("frames", input_type, nullable=False)]),
output_schema=pa.field("frames", declared_list(item), nullable=False),
)
@@ -2199,7 +2203,7 @@ def test_blob_list_output_fresh_artifact_survives_local_reopen(
item_field = declared["fields"][0]
assert item_field["metadata"]["ARROW:extension:name"] == "lance.blob.v2"
result = _execute_registered_array_udf_v1(
result = _execute_registered_array_function_v1(
request,
pa.RecordBatch.from_arrays(
[pa.array([[0], [1]], type=input_type)],
@@ -2264,11 +2268,11 @@ def test_a_function_namespace_path_must_be_a_list_of_segments():
def test_one_explicit_schema_infers_the_other_from_annotations():
@udf(input_schema=pa.schema([pa.field("value", pa.int64())]))
@function(input_schema=pa.schema([pa.field("value", pa.int64())]))
def explicit_input(value) -> float:
return float(value or 0)
@udf(output_schema=pa.field("result", pa.float32(), nullable=False))
@function(output_schema=pa.field("result", pa.float32(), nullable=False))
def explicit_output(value: Optional[int]):
return float(value or 0)