Search Shortcut cmd + k | ctrl + k

Call LLM APIs (OpenAI, Gemini, Cloudflare) directly from SQL with structured output support.

Maintainer(s): onnimonni

Compatibility Matrix

This table covers all DuckDB releases that have not reached end-of-life, including LTS releases, and shows the 2 most recent patch releases of each. Older patch releases are not listed, but the extension may be available for them as well.

Platform 1.4.4 1.4.5 1.5.5 1.5.6 2.0.0
Linux AMD64
Linux ARM64
macOS AMD64
macOS ARM64
Windows AMD64
Windows ARM64
WebAssembly

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 support
  • prompt() scalar function with auto-detection of API keys
  • llm_and_cache() for immediate responses with caching
  • llm_batch_and_cache() for 50% cheaper batch API processing
  • llm_hash() for LEFT JOIN support with cached results
  • CREATE SECRET support for secure API key storage
  • Structured output with return_type and json_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.

Added Functions

This extension does not add any functions.

Overloaded Functions

This extension does not add any function overloads.

Added Types

This extension does not add any types.

Added Settings

This extension does not add any settings.