Enterprise only
A Function binds a Secret by name. Its credential is
resolved when a job uses the Function, so altering the Secret does not require
registering another Function version.
Connect and create a Secret
Use an Enterprise database with Functions enabled and a configured Secrets encryption key. The deployment operator setsglobal.secretsKmsKeyId and grants the query nodes and execution services
access to that key. Without a sealing key, Secret operations are unavailable;
there is no plaintext fallback. Secrets also require a native manifest-backed
database. In this SDK, the Secret lifecycle uses Flight SQL, including its
error channel (gRPC UNIMPLEMENTED, code 12, for an unavailable feature).
Install the SDK and the client dependency used by this example:
LANCEDB_DATABASE, LANCEDB_ENDPOINT (the HTTP or HTTPS REST URL),
LANCEDB_SQL_ENDPOINT (the corresponding Flight SQL URL), LANCEDB_API_KEY,
and OPENAI_API_KEY in your environment. For example, use
grpc://your-sql-host:10025 for plaintext or
grpc+tls://your-sql-host:10026 for TLS, as configured by your operator.
Supply the SQL endpoint explicitly, especially for HTTPS REST endpoints.
Run the Python
examples in a notebook or a file so that the SDK can inspect the Function’s
source. Use a disposable database or keep the generated names for cleanup.
USAGE,
plus CREATE_SECRET, CREATE_FUNCTION, and CREATE_TABLE on the namespace.
Using an existing Secret also requires USAGE on that Secret. The creator owns
each new object. These examples use the root namespace; for another namespace,
pass the same namespace_path to object operations and
secret_namespace_path to EnvVarSecret.
Register, bind, and refresh
Declare the remote dependency on the Function as well as installing it in the client environment. The class reads the credential in__init__, once per
remote instance, and never returns it.
add_columns validates
that the bound Secret exists; refresh admission checks it again and authorizes
the caller. Declaring the column does not fill its rows. Remote tables require
refresh_column_async(...).wait(); synchronous refresh_column raises
NotImplementedError in this SDK.
Adding a Function column requires table OWNERSHIP. Refreshing an existing
binding requires table UPDATE. Both operations also require Function and
Secret USAGE.
Grant and revoke access from Python
A second caller needsUSAGE on both the Function and every bound Secret.
Use the catalog’s authorization API from an account that owns those objects.
Replace secret_refreshers with an existing role assigned to the second
caller. This example also grants database/namespace access and table read and
update privileges so the role can refresh and inspect the existing column.
add_acl returns HTTP 409 if that exact grant already exists. Inspect
catalog.authz.list_acls(object=secret_object) when sharing existing objects.
For a nested namespace, pass its path to the Object builders too.
Revoke just Secret usage, leaving Function and table access in place:
SELECT
and database/namespace access; it does not invoke the Function.
On deployments that expose SQL grants, the Python SDK can also run
db.execute_query("GRANT USAGE ON SECRET ... TO ROLE ...").read_all() and
the corresponding REVOKE. See privileges for the
operation matrix and grant syntax for quoting object names.
Alter, refresh new work, and drop
SetOPENAI_API_KEY_NEXT to a replacement provider key. Alter the existing
Secret, then add an unfilled row so the next refresh has work to perform:
UPDATE on the Secret permits
altering its value; OWNERSHIP permits dropping it.
Observe a missing Secret at column use
After the drop above, registering another Function version with the same binding still succeeds. Adding a column using it fails before row execution:table.refresh_column_async("embedding") on the original table fails at admission
for the same reason. The error identifies the missing Secret and its bound
variable. An owner is used here so a missing USAGE grant does not mask the
missing-object check.
Handle Python errors
The SDK uses two transports. Secret creation, alteration, description, listing, and drop use Flight SQL. General SQL failures raiselancedb.remote.errors.SqlError, with grpc_code and no HTTP status. When the
server includes structured resource metadata, this SDK maps SQL not_found
and already_exists errors to SecretNotFoundError and AlreadyExistsError
(both HttpError subclasses), with mapped HTTP statuses. A terminal query
failure can instead raise lancedb.exceptions.JobFailedError, with either
kind of structured error in __cause__ when available. Keep the job/request
IDs when no structured cause exists.
Function registration, column declaration, refresh submission, and catalog ACL
calls use REST. Catch HttpError and inspect status_code for these requests.
After a refresh is admitted, a failed .wait() raises JobFailedError.
For example, recreate the Secret dropped above, attaching an actionable
explanation to an unavailable-feature failure. A duplicate name still fails
without overwriting a credential:
HttpError misses general SQL lifecycle failures.
In particular, an unavailable feature uses gRPC 12 rather than an HTTP 501
exception from these Python methods.
Reserved environment variables
Names are case-sensitive. The complete reserved exact names are:[A-Za-z_][A-Za-z0-9_]*. A Function can bind
at most 16 Secrets, with each environment variable bound once.