VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page work on VillageSQL. Install Now →
vsql_ai extension adds ai_prompt() — a function that calls an AI model directly from a query, so enrichment, classification, and generation can happen where the data already lives.
The Problem: Context Switching Between SQL and AI
A typical pattern without VillageSQL:With VillageSQL: ai_prompt() in SQL
ai_prompt(provider, model, api_key, prompt) makes an API call and returns the response as a string. Use it anywhere a scalar function works.
Updating rows in bulk
The most common use: backfill an enrichment column across existing rows.Using prompt results in WHERE
You can filter on AI output, though this calls the model for every row in the scan:ai_prompt() in a WHERE clause.
Building prompts from multiple columns
Concatenate columns into the prompt to give the model full context:Performance and Rate Limits
Eachai_prompt() call is an independent HTTPS request. For bulk operations:
- Use LIMIT in UPDATE — AI calls take 5–30 seconds each, so a batch
UPDATEover 100 rows takes minutes; process in batches of 50–100 rows to stay within provider rate limits.max_execution_timeonly boundsSELECTand does nothing here — to cap a runningUPDATE, useKILL QUERY <connection_id>from another session. - Store results — Never call
ai_prompt()in a SELECT without storing the result. Repeated SELECTs re-run the API call each time.
Choosing a Model
For classification and labeling tasks, use the fastest/cheapest model that gives acceptable accuracy. Reserve more capable models for tasks where output quality matters more than throughput.
For setup and provider configuration, see Connecting MySQL to AI APIs.
Frequently Asked Questions
Can I use ai_prompt() in a DEFAULT expression?
No — non-deterministic functions (whichai_prompt() is) can’t be used in DEFAULT expressions or generated columns in MySQL.
Can I use ai_prompt() in a stored procedure?
Yes. Stored procedures, triggers, and functions can all callai_prompt(), subject to the same timeout constraints. Be cautious with triggers — an AI call on every INSERT will make every write slow.
How do I control the response format?
Through the prompt. “Reply with one word: positive, negative, or neutral” gets predictable output for classification. “Return valid JSON with keys: category, confidence” gets structured output. The model follows instructions reliably when they’re specific.Does ai_prompt() retry on failure?
No. If the API call fails, it returns NULL. RunSHOW WARNINGS to see why — vsql_ai raises a Warning 3200 with the real failure reason — before implementing retry logic in your application’s batch loop.
Troubleshooting
See also
- Connecting MySQL to AI APIs — installing and configuring the vsql_ai extension first
- AI Summarization in MySQL — summarizing long text with ai_prompt()
- Classifying Text in MySQL with AI — labeling rows by category using ai_prompt()
- Sentiment Analysis on MySQL Data — scoring text for tone and sentiment

