Call LLM APIs (OpenAI, Gemini, Cloudflare) directly from SQL with structured output support.
Maintainer(s):
onnimonni
Installing and Loading
INSTALL llm FROM community;
LOAD llm;
About llm
Call LLM APIs directly from SQL with support for OpenAI, Gemini, Cloudflare Workers AI, and local OpenAI-compatible servers.
Features
llm()table function with lateral join and named parameter supportprompt()scalar function with auto-detection of API keysllm_and_cache()for immediate responses with cachingllm_batch_and_cache()for 50% cheaper batch API processingllm_hash()for LEFT JOIN support with cached results- CREATE SECRET support for secure API key storage
- Structured output with
return_typeandjson_schema
Structured Output Examples
Integer output:
SELECT response FROM llm('What is 15 + 27?',
provider := 'gemini', model := 'gemini-2.5-flash',
return_type := 'INTEGER');
-- Returns: {"value":42}
Array output:
SELECT response FROM llm('List the first 5 prime numbers.',
provider := 'gemini', model := 'gemini-2.5-flash',
return_type := 'INTEGER[]');
-- Returns: {"value": [2, 3, 5, 7, 11]}
Struct output:
SELECT response FROM llm('Info about Paris.',
provider := 'gemini', model := 'gemini-2.5-flash',
json_schema := '{
"type": "OBJECT",
"properties": {
"city": {"type": "STRING"},
"country": {"type": "STRING"},
"population": {"type": "INTEGER"}
},
"required": ["city", "country", "population"]
}');
-- Returns: {"city": "Paris", "country": "France", "population": 2140000}
Array of structs:
SELECT response FROM llm('List 3 European capitals.',
provider := 'gemini', model := 'gemini-2.5-flash',
json_schema := '{
"type": "OBJECT",
"properties": {
"cities": {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"name": {"type": "STRING"},
"country": {"type": "STRING"}
}
}
}
}
}');
-- Returns: {"cities": [{"name": "Paris", "country": "France"}, ...]}
For more information, see the documentation.