feat: support pinned Hugging Face model declarations (#4821)

Python Function declarations can now pin Hugging Face snapshots with
`models=[HuggingFaceModel(repo=..., revision=...)]`. The native Rust
registration path carries those declarations into `CREATE FUNCTION`, and
create/get/list results retain `image.models`. Functions without
declarations preserve their existing payload shape. The API reference
includes a model-loading example that initializes once per Function
instance.

Validation:
- The real SQL mock-service regression fails on the previous native
implementation and passes after the fix. It covers immediate/deferred
execution, requests with/without models, and create/get/list responses.
- Rust Function wire tests, strict Clippy, the public-type doctest, and
formatting.
- Rebuilt Python extension and targeted registration/golden tests.

The service-side snapshot preparation and cache are delivered separately
for ENT-3483.
This commit is contained in:
Xuanwo
2026-11-04 11:26:49 +08:00
committed by GitHub
parent aeab69e86a
commit f4271b9dcb
6 changed files with 202 additions and 18 deletions
@@ -26,6 +26,7 @@ import pytest
import lancedb
from lancedb import BlobReader
from lancedb.functions import (
HuggingFaceModel,
PythonRuntimeSpec,
SecretBinding,
SecretReference,
@@ -92,6 +93,38 @@ def test_scalar_udf_matches_shared_registration_golden_and_remains_callable():
}
def test_udf_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])
def embed_text(value: str) -> list[float]:
return [1.0]
request = json.loads(embed_text.registration_request.to_canonical_json())
assert request["models"] == [
{
"repo": "sentence-transformers/all-MiniLM-L6-v2",
"revision": "1110a243fdf4706b3f48f1d95db1a4f5529b4d41",
}
]
undeclared = json.loads(normalize_score.registration_request.to_canonical_json())
assert "models" not in undeclared
with pytest.raises(ValueError, match="once"):
udf(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(
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(
pip=["numpy>=2"],