* feat(core): add Z.ai AI provider Z.ai can be added as an instance or workspace AI provider with an API key. It uses pydantic-ai's ZaiModel with a profile limited to the request fields Z.ai documents: automatic tool choice, max_tokens, no strict tool definitions and no file parts. AI fields send JPG/PNG images and small text files; the temperature is capped at 1. Part of #5753. * fix(core): limit Z.ai thinking on short capped requests Z.ai reasons at maximum effort by default, so a 250-token Kuma title or a 256-token model test could spend its whole budget reasoning and return nothing. Requests capped at 1024 tokens or less now disable thinking, or ask always-thinking models such as glm-5.3 for low effort, unless the caller chose a level.
6.1 KiB
AI Field Architecture
Overview
The AI field lets users generate cell values by sending prompts to LLM providers. The architecture splits into two layers:
- Core layer (
baserow.core.generative_ai) — provider abstraction, prompt execution, file handling contract. Provider-agnostic. - Premium layer (
baserow_premium.fields) — AI field type, handler (orchestration), output types, job scheduling.
Core: GenerativeAIModelType
Each LLM provider (OpenAI, Anthropic, Amazon Bedrock, Google Gemini, Groq, Mistral,
Ollama, OpenRouter, xAI, Z.ai) is a GenerativeAIModelType subclass registered in
generative_ai_model_type_registry. A model type is responsible for:
- Configuration — reading API keys and enabled models from workspace
settings or instance-level env vars (
get_workspace_setting,get_enabled_models). - Prompting —
prompt()is the single entry point for all AI calls. It accepts a text prompt, optional multi-modal content, an output type (plain text, choice list, or Pydantic model for structured output), and returns the result. Internally it builds a pydantic-ai Agent and runs it. - File handling — providers that support file input
(
supports_files = True) overrideprepare_files()anddelete_file(). See File handling below.
OpenAI-compatible providers (OpenAI, Ollama, OpenRouter) share a common base
(BaseOpenAIGenerativeAIModelType) for credential handling and model
instantiation.
Adding a new provider
Subclass GenerativeAIModelType, implement get_ai_model() (returns a
pydantic-ai Model), get_enabled_models(), get_api_key() or is_enabled(),
and get_settings_serializer(). Add the provider's database configuration
metadata to AI_PROVIDER_TYPES and register the model type in the core app's
ready() hook. New providers are configured through the instance or workspace
AI provider UI/API; PROVIDER_ENVIRONMENT_SETTINGS only supports importing the
existing legacy environment settings and must not be extended. Override
_prepare_model_settings() if the provider has quirks (e.g. Anthropic caps
temperature at 1.0). If the provider supports file input, provide a
FileHandler implementation.
Register a matching frontend BuiltInGenerativeAIModelType using the same stable
type identifier and register any database-provider UI metadata and English
strings. Extending that base is what reports the provider through
isBuiltInProviderType(), which lets an integration override that omits models
inherit the workspace allowlist; plugin providers extend GenerativeAIModelType
directly and keep their own authoritative model list. The database-provider API
supplies the available provider configuration, while the frontend registry
supplies presentation.
Premium: AIFieldHandler
AIFieldHandler is the single entry point for AI field operations:
generate_formula_with_ai()— generates a Baserow formula from a natural-language description (used by the formula AI modal).generate_value_with_ai(ai_field, row)— generates a single cell value. This is the core method that orchestrates the full flow:- Validates the model configuration.
- Resolves the prompt formula against the row context.
- Collects files from the row's file field (if configured).
- Calls
model_type.prepare_files()thenmodel_type.prompt(). - Cleans up uploaded files via
model_type.cleanup_files(). - Resolves the output (choice matching if applicable).
Output types
The AIFieldOutputType registry defines how AI responses map to cell
values. Current types:
TextAIFieldOutputType— free-form text stored as long text.ChoiceAIFieldOutputType— the prompt is constrained to a set of select options; the model's response is fuzzy-matched to the closest option.
File handling
File support follows a prepare/cleanup lifecycle using the AIFile
dataclass:
AIFieldHandler._collect_ai_files()buildsAIFileinstances from the row's file field. EachAIFilehas metadata (name,size,mime_type) and a lazyread_content()method.model_type.prepare_files(ai_files, workspace)decides which files to accept. It reads content only for accepted files, setsai_file.content(the pydantic-ai content part), and optionallyai_file.provider_file_idif the file was uploaded to the provider. Returns only processed files.- After the prompt call,
model_type.cleanup_files(prepared, workspace)deletes any provider-uploaded files.
OpenAI embeds images and uploads supported documents to the OpenAI Files API.
Anthropic embeds images and uploads PDFs. Google Gemini, Mistral, Ollama, and
OpenRouter embed supported binary content and inline small text files without a
provider upload. Google uses a conservative 14 MiB cumulative raw-file budget so
base64 encoding, prompts, and system instructions stay below Gemini's 20 MB inline
image request limit. Amazon Bedrock sends images and documents as native Converse
blocks, within Bedrock's per-kind limits (20 images of 3.75 MB, 5 documents of
4.5 MB) set through EmbedKindLimit, and a 14 MiB total budget. Groq does not
advertise provider-level file support because its capabilities vary by model.
xAI embeds JPEG and PNG images within a 20 MiB cumulative raw-file budget and
inlines small text files; its Chat Completions API has no document input, so
other files are skipped. Z.ai also embeds JPEG and PNG images, up to 3.75 MB each
and 50 per request (EmbedKindLimit) within a 14 MiB budget, and skips documents
because a request cannot mix file parts with images; only its vision models read
the images.
Job scheduling
Bulk generation (entire table, filtered view, or specific rows) runs as an
async job (GenerateAIValuesJobType). The job uses AIValueGenerator
which processes rows in a thread pool, calling
AIFieldHandler.generate_value_with_ai() per row.
When auto-update is enabled, changes to fields referenced in the prompt
create AIFieldScheduledUpdate records and schedule a per-field singleton
Celery task. The task runs synchronously (holding a lock), then checks if
new updates arrived while it was running — if so, it reschedules itself.