Skip to main content
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 sets global.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:
Set 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.
Run the lifecycle and missing-Secret checks as a database/namespace owner. To delegate creation instead, a caller needs database and namespace 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.
Registration validates binding shape: variable syntax, reserved names, and duplicate variables. It does not resolve the Secret. 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 needs USAGE 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:
A new refresh under the second caller’s credentials is now refused with HTTP 403. Regrant Secret usage to allow it again. Perform this check with the second caller’s connection: the owner’s connection retains ownership and will still succeed. Reading already filled rows requires only table 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

Set OPENAI_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:
The same Function version and column binding use the new value for the next job. Altering the Secret does not invalidate already computed rows. Running jobs and their retries keep their captured values. UPDATE on the Secret permits altering its value; OWNERSHIP permits dropping it.
Dropping removes the Secret and its grants. It leaves the Function and column binding in place, so their next refresh fails. Running jobs retain their captured values. To stop provider calls immediately, revoke the key at the provider. Recreating the Secret under the same name supplies future jobs with the replacement value, but does not restore the deleted grants.

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 raise lancedb.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:
Catch refresh admission errors at submission and execution failures while waiting on the returned handle:
Recreate the dropped Secret with the lifecycle handler above before running this refresh. Catching only 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:
Every name starting with one of these prefixes is also reserved:
Other variable names must match [A-Za-z_][A-Za-z0-9_]*. A Function can bind at most 16 Secrets, with each environment variable bound once.

Clean up the example

After finishing the missing-Secret checks, drop the disposable tables and Function, then drop the Secret recreated in the error-handling example:
Review and remove any database or namespace grants added solely for this example. See Secret management and Secret delivery for the lifecycle guarantees.