AI and multimodal workloads
Daft AI expressions add model inference to a DataFrame workflow. They turn text, images, files, and mixed message inputs into generated responses, embeddings, or labels while the DataFrame engine manages row alignment and execution. A provider layer keeps these expressions separate from the service or model that performs the work.
Workload model
AI expressions
AI expressions are column operations that apply one model action to each input row. They fit into the same lazy workflow as other Daft expressions, so a result can be selected, filtered, joined, or written as part of a larger data flow. See Expressions and functions for the wider expression model.
The public AI function set covers four kinds of work:
| Work | Input | Result |
|---|---|---|
| Prompting | One message expression or a list of message expressions | Generated text or a structured result |
| Text embedding | Text expression | Fixed-size embedding vector |
| Image embedding | Image expression | Fixed-size embedding vector |
| Classification | Text or image expression plus candidate labels | One selected label per row |
An expression resolves a provider, asks it for a model descriptor, and uses that descriptor to define the result type and execution settings. Empty input batches produce empty results. This preserves the row-oriented contract between inference work and the surrounding DataFrame operation.
References: daft/…/__init__.py• L34-40 daft/…/__init__.py• L72-154 daft/…/__init__.py• L157-242 daft/…/__init__.py• L250-326 daft/…/__init__.py• L329-421
Multimodal prompting
Multimodal prompting lets one prompt combine text with content from another modality. A message can be plain text, an image, a URL, raw bytes, or a File object. The selected provider converts each value to the provider's input form before it sends one user request.
The supported input meaning depends on the provider:
| Provider family | Prompt input scope |
|---|---|
| OpenAI-compatible | Text, images, image and video URLs, and non-text files. Text files become text content. |
| Text, images, and binary files such as images or PDFs. Text files become text content. | |
| Transformers | Text and text-like bytes or files. Non-text media is not supported by this prompt path. |
| vLLM prefix caching | One text expression. It does not support a message list, system message, or structured return format. |
OpenAI-compatible prompting can use either the Chat Completions or Responses protocol. The selected protocol changes the wire format for text, images, video URLs, and files, but not the DataFrame-facing prompt contract. Google converts bytes and non-text files to typed content and can convert image arrays when its optional image dependency is present.
Prompt results can remain plain text. When the provider supports it, a Pydantic model can define a structured return shape. Provider-specific input rules therefore matter when a workload moves from text-only data to images, documents, audio, or video.
References: daft/…/__init__.py• L453-476 daft/…/__init__.py• L572-652 daft/…/prompter.py• L160-221 daft/…/prompter.py• L262-302 daft/…/prompter.py• L100-174 daft/…/prompter.py• L113-169 daft/…/prompter.py• L27-72
Provider layer
Provider resolution
The provider layer is the boundary between a Daft expression and a model service or runtime. A provider describes which model actions it can supply and creates a serializable descriptor that carries the provider name, model identity, and options into execution. The common provider contract exposes prompting, embedding, and classification capabilities; unsupported actions fail as provider-specific not-implemented operations.
Provider selection follows a clear order:
- Use a provider instance passed to the expression.
- Use a named provider attached to the active session.
- Load a named provider from the built-in provider registry.
- Use the active session's current provider.
- Load the expression's default provider.
Applications can attach a configured provider to a session or set a known provider by name. This allows several provider configurations to coexist and lets a workload choose a provider per expression without changing its data flow.
flowchart LR
E[AI expression] --> R{Provider specified?}
R -->|instance| P[Provider]
R -->|name in session| P
R -->|name not in session| L[Provider registry]
R -->|not specified| C{Current session provider?}
C -->|yes| P
C -->|no| D[Expression default]
L --> P
D --> P
P --> X[Model descriptor]
X --> U[DataFrame execution]
References: daft/…/__init__.py• L43-65 daft/…/provider.py• L33-101 daft/…/provider.py• L104-155 daft/session.py• L219-232 daft/session.py• L742-769
Provider protocols
A protocol is the small capability contract that a model implementation must satisfy. The expression layer uses the same protocol for local models and remote services, while descriptors report model properties before the model runs.
| Protocol | Model action | Main contract |
|---|---|---|
Prompter | Generate a response from messages | Async prompt result of any supported output type |
TextEmbedder | Map text to vectors | One vector for each input text |
ImageEmbedder | Map images to vectors | One vector for each input image |
TextClassifier | Select a label for text | One label for each input text |
ImageClassifier | Select a label for an image | One label for each input image |
Descriptors add the information needed to plan the call. For embeddings, that includes vector size and data type. For all protocols, the descriptor can expose model options and DataFrame execution options such as batch size, retry behavior, concurrency, and GPU use.
References: daft/…/protocols.py• L14-91 daft/…/typing.py• L16-37 daft/…/typing.py• L94-187 daft/…/_expressions.py• L25-117
Supported providers
The built-in registry currently names five provider families. A check mark means that the provider exposes the capability through the current public AI expressions; an em dash means that the provider is registered but does not provide that capability.
| Capability | OpenAI | vLLM prefix caching | LM Studio | Transformers | |
|---|---|---|---|---|---|
| Prompt | Yes | Yes | Yes | Yes | Yes |
| Text embedding | Yes | No | No | Yes | Yes |
| Image embedding | No | No | No | No | Yes |
| Text classification | No | No | No | No | Yes |
| Image classification | No | No | No | No | Yes |
OpenAI-compatible providers include OpenAI itself and LM Studio. LM Studio uses the OpenAI protocol against a local server. Its embedding path can use a supplied vector size or discover the size from the loaded model. The same discovery path supports custom OpenAI-compatible embedding servers when the server accepts a dimension probe. The Transformers provider supplies local model support for embeddings and classification. Google and OpenAI provide remote prompt paths, including multimodal input handling in their prompt implementations. vLLM prefix caching is a specialized prompt path for local inference.
Provider packages are optional. If the package needed by a chosen provider is not installed, provider loading reports an install hint instead of making the base Daft import require every AI dependency.
References: daft/…/provider.py• L33-101 daft/…/provider.py• L20-72 daft/…/provider.py• L19-65 daft/…/provider.py• L19-58 daft/…/text_embedder.py• L23-103 daft/…/text_embedder.py• L88-176 daft/…/provider.py• L33-141 daft/…/provider.py• L20-63 docs/…/ai.md• L3-10
Model execution
Descriptor and model lifecycle
The descriptor is the hand-off point between planning and inference. It carries model identity and options through the DataFrame plan, then creates the concrete model inside the UDF or specialized execution path that will use it.
For a standard AI expression, execution follows this sequence:
- Resolve the provider and request a capability descriptor.
- Read the descriptor's result shape and execution options.
- Instantiate the model in the worker that runs the expression.
- Send row values in batches to the model protocol.
- Return responses, vectors, or labels as a new DataFrame column.
Provider options configure the model client. API options configure a request. Daft UDF options control how the DataFrame engine runs the work. The provider helpers keep these groups separate so client settings do not become worker settings and worker settings do not leak into provider requests. See UDFs and aggregations for the wider UDF execution model.
The expression chooses synchronous or asynchronous model calls when the protocol supports both. OpenAI-compatible prompt and embedding clients handle their own retries, so their descriptors prevent the same failed request from also being retried by the UDF layer.
References: daft/…/__init__.py• L127-154 daft/…/__init__.py• L215-242 daft/…/__init__.py• L304-326 daft/…/__init__.py• L399-421 daft/…/typing.py• L128-187 daft/…/utils.py• L64-76 daft/…/prompter.py• L40-63 daft/…/text_embedder.py• L88-176
Batch and resource execution
AI work is executed as batched DataFrame work rather than as one unmanaged model call per row. Batch size limits the values sent to a model call. Concurrency controls the number of active model instances or calls, and GPU settings place model work on GPU-backed workers when the provider supports them.
Provider behavior can add another level of batching. OpenAI-compatible text embedding uses an estimated token budget for each request and a model-specific limit for one input. A long input is split and its chunk vectors are combined into one normalized vector so the output still has one vector for the source row. If a batch request is rate-limited, the embedding path falls back to individual requests.
Failures follow the UDF policy carried by the descriptor. The default is to raise. A workload can instead choose logging or ignoring errors where the provider and expression expose that option. Remote clients may also apply their own retry rules, so the provider path can consume retries before Daft sees a failed call. Google rate-limit and temporary-service responses can carry a Retry-After delay into Daft's retry handling when the response provides a valid delay.
References: daft/…/typing.py• L16-37 daft/…/typing.py• L40-107 daft/…/typing.py• L177-187 daft/…/utils.py• L19-61 daft/…/utils.py• L79-120 daft/…/__init__.py• L132-149 daft/…/text_embedder.py• L179-330 daft/…/prompter.py• L191-216
Local and distributed inference
Standard provider expressions run through Daft's UDF execution model. The vLLM prefix-caching provider is different: the prompt expression sends its request to a vLLM execution path that can keep repeated prompt prefixes near the same model actor.
The vLLM path can use several local or Ray-backed model actors. A router spreads work by load and can prefer actors that recently saw the same prefix. This is useful when many rows share a system prompt or another long prefix, but the provider is marked experimental and supports a narrower prompt contract than the standard provider path.
sequenceDiagram
participant DF as DataFrame execution
participant R as Prefix router
participant A as Model actors
participant M as vLLM model
DF->>R: Submit prompt batch
R->>A: Choose actor by load and prefix
A->>M: Run local generation
M-->>A: Generated output
A-->>DF: Poll output with source rows
References: daft/…/__init__.py• L587-617 daft/…/prompter.py• L27-72 daft/…/vllm.py• L235-263 daft/…/vllm.py• L266-333
AI metrics
Token usage
AI metrics record model request counts and token use as execution metrics. Each metric is tagged with the protocol, provider, and model, so an operator can compare cost and load across model choices instead of seeing only one aggregate count. See Observability and dashboard for the wider runtime metrics view.
The AI layer records these counters when the provider response includes usage data:
| Counter | Meaning |
|---|---|
requests | A completed AI request path reported by the provider implementation |
input tokens | Tokens consumed by the input |
output tokens | Tokens produced by the model |
total tokens | Total tokens reported by the provider |
Metrics are emitted inside UDF execution through the current operator-metrics context. A direct call to the metric helper outside a UDF does not attach the counter to a workload and produces a warning instead.
OpenAI prompt calls support both Chat Completions and Responses usage shapes. OpenAI embedding calls record input and total tokens. Google prompt calls record the token counts supplied in its response metadata. For thinking models, reasoning tokens are added to output tokens because the total token count includes them. Providers that do not return usage data do not add token counters through this layer.
References: daft/…/metrics.py• L6-26 daft/…/prompter.py• L304-317 daft/…/prompter.py• L319-390 daft/…/text_embedder.py• L269-326 daft/…/prompter.py• L176-216 daft/…/metrics.py• L19-65
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the provider capability matrix aligned with both the provider registry and each provider's
get_*methods. - Keep protocol result contracts aligned with descriptor result types, especially embedding dimensions and structured prompt outputs.
- Keep multimodal input rules aligned across the public
promptfunction and each provider's message conversion path. - Keep provider options, request options, and UDF options separated when adding a new option. A key in the wrong group can reach the wrong client or worker.
- Keep provider client retry behavior distinct from Daft UDF retry behavior so one request is not retried more times than intended.
- Keep embedding token limits and dimension discovery aligned with custom OpenAI-compatible and LM Studio servers.
- Keep
Retry-Afterparsing aligned with provider error handling so a valid service delay reaches Daft's retry policy. - Keep token metric names and attribute keys stable for dashboard and downstream metric consumers.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A requested provider is not in the provider registry | Expression construction fails before inference starts | Use one of the registered provider names or attach a provider instance |
| A provider's optional package is missing | Provider loading raises an install hint for the provider extra | Install the extra that matches the selected provider |
| A prompt sends media to the Transformers provider | The prompt path rejects the non-text input | Use OpenAI-compatible or Google prompting for media inputs |
| A vLLM prompt uses a message list, system message, or structured return format | The specialized vLLM path rejects the unsupported option | Send one text expression and use the standard provider path for those features |
| A provider does not implement the requested protocol | The provider raises a not-implemented error before model execution | Select a provider with the required capability |
| A custom OpenAI-compatible embedding service cannot answer a dimension probe and no dimensions are supplied | The descriptor cannot define a stable embedding result type | Specify the embedding dimensions or use a server that supports the probe |
| A provider option is passed as a request-only option or a UDF option is sent to the provider client | The client can reject the request or apply a setting at the wrong layer | Keep provider, API, and UDF options in their defined groups |
A Google rate-limit or temporary-service response has no valid Retry-After value | Daft receives the original provider error without a service delay | Configure provider retries or handle the original error when no delay is supplied |
| Metrics are emitted outside UDF execution | Counters are not attached to an operator | Inspect AI metrics during DataFrame execution |
Key files
| File | Why to read it |
|---|---|
| daft/…/__init__.py | Public AI expressions, provider resolution, result typing, and the standard versus vLLM execution split |
| daft/…/provider.py | Provider registry, optional provider loading, and the common capability boundary |
| daft/…/protocols.py | Shared contracts for prompting, embeddings, and classification |
| daft/…/typing.py | Descriptor, embedding shape, request option, and UDF execution concepts |
| daft/…/utils.py | Separation of provider and request options, resource selection, and retry-delay handling |
| daft/…/prompter.py | OpenAI-compatible text and multimodal prompt behavior, protocol selection, and usage metrics |
| daft/…/prompter.py | Google prompt input conversion, retry-delay handling, and usage reporting |
| daft/…/text_embedder.py | OpenAI-compatible embedding dimensions, token-aware batching, long-input handling, and usage metrics |
| daft/…/text_embedder.py | LM Studio embedding dimension discovery and its reuse of the OpenAI-compatible embedding path |
| daft/…/provider.py | Local model capability coverage for embeddings and classification |
| daft/…/prompter.py | Specialized prefix-caching prompt contract and its limits |
| daft/…/vllm.py | Local and Ray-backed vLLM actor execution and prefix-aware routing |
| daft/…/metrics.py | AI token and request metric names and dimensions |