AI functions in Spice provide direct integration with large language models (LLMs) and embedding models within SQL queries. These functions process text through configured model providers and return generated responses, typed decisions, or vector embeddings.
aiInvokes large language models (LLMs) directly within SQL queries for text generation tasks. This asynchronous function processes prompts through configured model providers and returns generated text responses.
Returns a string containing the generated text response. Returns NULL for NULL input messages or when the model returns an empty response. If the underlying model call fails, the query errors out (errors are logged).
Queries execute asynchronously, processing LLM calls in parallel across rows for improved performance. Each invocation issues an asynchronous call to the specified model provider.
Concurrency: When a per-model rate controller is configured in the Spicepod, it manages concurrency and backpressure. Otherwise, concurrency falls back to DataFusion's execution.target_partitions setting. When multiple models with different providers are configured (e.g., OpenAI and Anthropic), each provider's requests are controlled independently.
Limits:
Models must be configured in spicepod.yaml under the models section. See Large Language Models (opens in a new tab) for detailed configuration.
Each ai() function invocation creates an ai span in the task_history (opens in a new tab) table, which tracks execution time, input prompts, model name, and row count. Child ai_completion spans capture per-row model call details.
When only one model is configured, the model name can be omitted:
Example output:
When multiple models are configured, specify which model to use:
Use the ai() function to generate search terms for enhanced search functionality:
Process multiple rows with parallel LLM calls. Each ai() invocation receives at most 1000 rows, and larger inputs are split across invocations. A LIMIT stops work only between batches, so a query can still call the model for more rows than the limit; narrow the rows with selective predicates when you need a strict bound:
Decision functions ask a model a typed question about each row and return a typed answer: a boolean, a probability, a label, a score, or a struct of answers. Any model in the Spicepod can answer, including a decision model such as TypeSafe (opens in a new tab) Jev or any chat model. See Decisions (opens in a new tab) for the full guide, the /v1/decisions HTTP API, and how a chat model answers.
input returns NULL without a model call.'fail' (the default) stops the query when the model cannot answer a row, after retrying rate limits and transient failures. 'null' returns NULL for that row. When the model declines one question of an ai_decide call, or of calls that share a request, 'null' sets only that question's answer to NULL and keeps the other answers.Every argument except input must be a constant. Constants are checked when the query is planned, before any model call.
ai_ifReturns BOOLEAN: true when the probability that condition holds for input is above 0.5.
ai_probabilityReturns DOUBLE from 0 to 1: the probability that condition holds for input.
ai_classifyReturns VARCHAR: the label that best fits input. A non-NULL result is always one of labels; the result is NULL for a NULL input, and for a row the model refuses or fails under on_error => 'null'. labels is a list of 2 to 255 labels, such as ['billing', 'technical'], or a JSON object of label to description, such as '{"billing": "Payments and refunds", "technical": null}'. Labels cannot be empty or repeated. Optional instructions => '...' adds guidance. Include a fallback label such as 'other' when no label may fit.
ai_scoreReturns DOUBLE from 0 to n−1: the probability-weighted, 0-based index of levels. levels is a list of 2 to 10 level descriptions, lowest first.
ai_decideReturns STRUCT with one field per question. questions is a JSON object of question ID to question in TypeSafe's grammar, the same grammar as Databricks' ai_decide: each question has a type of noul, choice, or score, and optional instructions. A choice maps 1 to 255 labels to descriptions in criteria, and a score lists 2 to 10 levels in criteria, lowest first. See Ask several questions with ai_decide (opens in a new tab) for the answer fields.
Calls to ai_if, ai_probability, ai_classify, and ai_score in one SELECT list or WHERE clause that share the same input, model, and on_error are answered by at most one request per row: identical non-NULL inputs within a batch are asked once, and a NULL input is not asked at all. In WHERE, the predicates a top-level AND joins to the call run first, so the model only sees rows that pass them; a predicate nested under OR does not prefilter. Decision functions are never pushed down to a federated source.
Each batch of calls is recorded in the task_history (opens in a new tab) table as an ai_decide task. The model's max_concurrency and requests_per_minute_limit apply.
embedGenerates vector embeddings for text using specified embedding models. Supports both single text strings and arrays of text for batch processing.
Utf8 / LargeUtf8 scalars and lists/arrays of strings are supported.ai(), embed() does not auto-select a default when only one model is configured.List<Float32> — a single embedding vector.List<List<Float32>> — one embedding vector per element, preserving the input length. NULL input elements produce NULL output elements.Embedding models must be configured in spicepod.yaml. See Embeddings (opens in a new tab) for configuration details.
Generate an embedding for a single piece of text:
Generate embeddings for multiple strings in one call:
Generate embeddings to use with vector search:
For more information on configuring and using AI models, see:
ai function. AI SQL Function (opens in a new tab)