VillageSQL is a drop-in replacement for MySQL with extensions.
All examples in this guide 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:
- Set
max_execution_time— AI calls take 5–30 seconds each; a batch UPDATE over 100 rows takes minutes. - Use LIMIT in UPDATE — Process in batches of 50–100 rows to stay within provider rate limits.
- 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
| Model | Speed | Cost | Use when |
|---|---|---|---|
claude-haiku-4-5-20251001 | Fast | Low | Bulk classification, simple labeling |
claude-sonnet-4-5-20250929 | Medium | Medium | Complex reasoning, nuanced output |
gpt-4o-mini | Fast | Low | OpenAI ecosystem, simple tasks |
gemini-2.5-flash | Fast | Low | Google ecosystem, balanced tasks |
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. Implement retry logic in your application’s batch loop.Troubleshooting
| Problem | Solution |
|---|---|
FUNCTION ai_prompt does not exist | Run INSTALL EXTENSION vsql_ai |
| Returns NULL | Bad API key, rate limit hit, or timeout — check max_execution_time |
| UPDATE times out | Add SET SESSION max_execution_time = 300000 and use LIMIT batches |
| Model returns unexpected format | Tighten the prompt: specify exact output format and give examples |
| Rate limit errors | Reduce batch size; add SELECT SLEEP(1); between UPDATE batches, or add a sleep in your application loop |

