Drex for Apache Spark™
Ask typed questions about rows from PySpark, Apache Spark SQL and Databricks SQL.
Ask typed questions about your rows from Apache Spark through PySpark and SQL,
or from Databricks SQL. drex-spark sends each row to
Drex and returns calibrated probabilities as Spark values.
The PySpark interface uses pandas UDFs. A separate generator installs Unity Catalog SQL functions for Databricks SQL warehouses. CI tests Python 3.10 with Spark 3.5, and Python 3.12 and 3.13 with Spark 4.
Getting started · Databricks SQL · API reference · Requests and cost · Development
What it does
| Function | Result | Use case |
|---|---|---|
drex_noul |
DOUBLE probability of yes |
Flag refund requests |
drex_choice |
Struct: label, probabilities, confidence | Route tickets to a team |
drex_score |
Struct: score, probabilities, confidence | Rank urgency on an ordered scale |
drex_decide |
JSON string of the full response | Ask several questions per row |
drex_decide_image |
JSON string about image bytes | Classify photos or receipts |
Apache Spark integration
The PySpark interface runs on Apache Spark and exposes pandas UDFs as columns and SQL functions. The Databricks SQL interface uses separately generated Unity Catalog functions.
Getting started
Requires Python 3.10+ and Spark 3.5+. For the local setup below, use Python 3.12 and Java 17, as in the tests.
python3.12 -m venv .venv
. .venv/bin/activate
python -m pip install drex-spark 'pyspark==4.2.1' 'pandas<3'
On a managed cluster, install drex-spark on the driver and executors and use the platform's PySpark: on Databricks,
%pip install drex-spark in a notebook, or a PyPI library on the cluster or job. See
Databricks SQL for SQL warehouses.
The key
Get an API key from the Nace console. The functions read NACE_API_KEY (and an optional
NACE_BASE_URL) on the executors, never from a function argument. Set them from your platform's secret store:
- Databricks clusters: in the cluster's Spark config,
spark.executorEnv.NACE_API_KEY {{secrets/<scope>/<key>}}. Serverless compute takes no Spark config, so its executors can't get the key: on serverless notebooks and jobs, call the Unity Catalog functions withspark.sqlor%sqlinstead. - Spark elsewhere:
--conf spark.executorEnv.NACE_API_KEY=...from your secret manager, or the environment of your executors. Inlocalmode the driver's environment is used.
For OpenRouter, set NACE_BASE_URL=https://openrouter.ai/api and an OpenRouter key as
OPENROUTER_API_KEY (an sk-or- key in NACE_API_KEY also works); a nace_ key is refused before any request. The
requests carry HTTP-Referer: https://nace.ai and X-OpenRouter-Title: Drex for Apache Spark unless you set
NACE_OPENROUTER_REFERER and NACE_OPENROUTER_TITLE the same way, to report your own app. On Databricks, pass
--openrouter-referer and --openrouter-title to python -m drex_spark.databricks, which bakes them into the functions.
Query
For local mode, set NACE_API_KEY in the driver environment before starting Python, using your secret manager.
Then run the following in one Python session:
from pyspark.sql import SparkSession
from drex_spark.functions import drex_noul, drex_score
spark = SparkSession.builder.master("local[2]").appName("drex-example").getOrCreate()
tickets = spark.createDataFrame([
(1, "I was charged twice. Please refund the duplicate payment."),
(2, "How do I change my email address?"),
], "id long, body string")
tickets.createOrReplaceTempView("tickets")
answers = tickets.select(
"id",
drex_noul("body", "Is the customer asking for a refund?").alias("refund"),
drex_score("body", "How urgent is this ticket?", ["low", "medium", "high"]).alias("urgency"),
)
answers.write.saveAsTable("ticket_answers")
refund is the probability of yes; urgency is a struct with a score, probabilities and confidence.
Answers come from the API and can vary. Saving them in a table lets you query them without sending the rows again; see
requests and cost.
Examples
Apache Spark SQL
Continue in the Python session above, which defines the tickets temporary view.
import drex_spark
drex_spark.register(spark)
spark.sql("""
SELECT id, drex_choice(body, 'Which team?', '["billing", "technical"]', NULL).choice AS team
FROM tickets
""").show(truncate=False)
Registered functions take every argument (Spark's pandas UDFs have no defaults): a NULL model is the default model.
Databricks SQL
On a SQL warehouse, the functions are Unity Catalog functions that run drex-spark inside a batch Python UDF. Create
them once, as the principal they should run as:
-
The key as a Unity Catalog secret, for example
main.drex.nace_api_key(Catalog Explorer › Create › Secret). The functions run with their owner's permissions: the owner needsREAD SECRETon it; callers need onlyEXECUTE. -
Network access. Python UDFs on a serverless SQL warehouse can't reach the internet until a workspace admin turns on the Public Preview Enable networking for isolated workloads in Serverless SQL Warehouses (Previews page), then restarts the warehouse: one that was running keeps refusing. If the workspace has a serverless network policy that restricts egress, allow
console.nace.aiandpypi.org. Pro warehouses and classic compute use your workspace's own network, which must reach both. -
The functions:
python -m pip install drex-spark python -m drex_spark.databricks --catalog main --schema drex --secret main.drex.nace_api_key > drex.sql
Use an existing catalog and schema that your function owner can access. Run
drex.sqlin the SQL editor. Each function installs the same version ofdrex-sparkfrom PyPI into its own environment (environment_version6); to install a wheel from a Unity Catalog volume instead, add--dependency /Volumes/<catalog>/<schema>/<volume>/<wheel>.
SELECT id,
main.drex.drex_noul(body, 'Is the customer asking for a refund?') AS refund,
main.drex.drex_choice(body, 'Which team?', '["billing", "technical"]', model => 'drex-max') AS team
FROM VALUES (1, 'I was charged twice. Please refund one.') AS tickets(id, body);
Unity Catalog has no overloading, so input is text (STRING): ask about JSON state with drex_decide, and about
images with drex_decide_image. Requires Databricks SQL (pro or serverless) or classic compute on Databricks Runtime 19
in standard access mode; functions with a SECRETS clause can't be called directly on dedicated access mode compute.
Configuration and behavior
Text and JSON calls default to drex-v2.0; images default to drex-max. PySpark helpers accept model=;
registered SQL functions require all arguments and use NULL for the default model. Read the
API reference for structured inputs, image bytes, validation and retry rules.
A missing choice or score is a struct with NULL fields: check team.choice IS NULL.
Requests and cost
Inputs, questions and any image bytes leave the Spark workers for the configured API. Use only data you may send there. A custom endpoint receives the configured key as well as the data.
Each non-NULL row sends one request, and Drex bills only a 200. A row with no answer is NULL, never a guess; treat
NULL as "unanswered", not as a negative answer. Spark can send a row more than once, and repeated successful requests can be billed again:
- A retried task (an executor lost, a failed stage) sends its rows again.
- A sort over a Drex column samples its input first:
orderByafter a Drex column sends every row twice. - Reusing a DataFrame (two actions, a self-join) runs it again.
Save answers first: write them to a table, or cache() the DataFrame and run count() before you sort, filter on, or
reuse it. Filter and limit the input before calling Drex.
Concurrency
spark.drex.concurrency (default 8) is a budget divided among Python workers using
max(1, budget // spark.default.parallelism). This is not a strict cluster-wide cap: more workers than the budget
can exceed it. When Spark Connect cannot expose parallelism, each worker gets the whole budget.
concurrency= overrides the budget for one call. The Unity Catalog
functions cap each batch at 2 (--concurrency when you create them), since a UDF can't see the warehouse's
parallelism and a warehouse runs many batches at once (about 20 on a Small serverless warehouse). Above your account's
limit, Drex answers 429 with retry-after-ms: the request waits its turn, and a row returns NULL only if Drex answers
nothing for two minutes.
Development
git clone https://github.com/nace-ai/drex-spark.git && cd drex-spark
uv sync
uv run pytest # needs Java 17 for the Spark tests (JAVA_HOME, or Homebrew's openjdk@17)
uv run ruff check src tests && uv run ruff format --check src tests && uv run mypy src
The tests call tests/mock/drex_mock.py, a mock of Drex's API copied from nace-ai/drex-duckdb (and checked against
Drex's openapi.json by python -m unittest discover -s tests/mock). tests/live calls the real API and runs only
with DREX_LIVE=1 (LiveTests.yml, on dispatch).
Support and contributing
See CONTRIBUTING.md for local checks and pull requests, and the API reference for the complete contract. Report bugs through GitHub Issues, with your runtime version and a minimal example using synthetic data. Remove keys, signed URLs and private data from reports.
License and trademarks
MIT. Nace artwork belongs to Nace AI. The software license does not grant trademark rights. See asset sources and branding guidance.
This integration is maintained by Nace AI. References to the platform describe compatibility, not sponsorship or endorsement by its owner.
Apache Spark, Spark, and the Spark logo are trademarks of the Apache Software Foundation.
Metadata
Release files for drex-spark 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drex_spark-0.1.1.tar.gz | 73.9 kB | With provenance | Details for drex_spark-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drex_spark-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for drex_spark-0.1.1-py3-none-any.whl |
Total release size: 103.9 kB
Release files / drex_spark-0.1.1.tar.gz
| Download URL | drex_spark-0.1.1.tar.gz |
|---|---|
| Size | 73.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53bf6d9a1903404a05a52cc37d40ce911adfd298bf834108ac58d1ff44829ea2
|
|
BLAKE2b-256 checksum How to use checksums |
580f88476157ad51b99305d6e0c88b156f813831c5f8d35fed63ef570ffb3626
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logRelease files / drex_spark-0.1.1-py3-none-any.whl
| Download URL | drex_spark-0.1.1-py3-none-any.whl |
|---|---|
| Size | 30.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fdb365c4e725648ad7e138fdb635a373a662bd2c2cae136874148bcc30169821
|
|
BLAKE2b-256 checksum How to use checksums |
b4a71c8b5506d00f67f0a18e0730d06713907b8bc910e795e1572d0b3ef4163d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logSecurity for drex-spark 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for drex_spark-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.