Skip to main content
Generate embeddings and receive the complete raw API response including all metadata. Use this when you need access to token usage, model information, or other response details not included in the standard openai_embed() function.

Samples

Get raw embedding response

Receive the full API response:
SELECT ai.openai_embed_with_raw_response(
    'text-embedding-ada-002',
    'PostgreSQL is powerful'
);

Extract token usage

Check how many tokens were used:
SELECT
    (ai.openai_embed_with_raw_response(
        'text-embedding-ada-002',
        'sample text'
    )->>'usage')::jsonb;

Arguments

NameTypeDefaultRequiredDescription
modelTEXT-The OpenAI embedding model to use (e.g., text-embedding-ada-002, text-embedding-3-small)
input_textTEXT-Single text input to embed (use this OR input_texts OR input_tokens)
input_textsTEXT[]-Array of text inputs to embed in a batch
input_tokensINT[]-Pre-tokenized input as an array of token IDs
api_keyTEXTNULLOpenAI API key. If not provided, uses ai.openai_api_key setting
api_key_nameTEXTNULLName of the secret containing the API key
dimensionsINTNULLNumber of dimensions for the output embedding (only supported by some models)
openai_userTEXTNULLUnique identifier for the end-user for abuse monitoring
encoding_formatTEXTNULLFormat for the embeddings (float or base64)
extra_headersJSONBNULLAdditional HTTP headers to include in the API request
extra_queryJSONBNULLAdditional query parameters for the API request
verboseBOOLEANFALSEEnable verbose logging for debugging
client_configJSONBNULLAdvanced client configuration options

Returns

JSONB: The complete API response including:
  • object: Response type
  • data: Array of embedding objects
  • model: Model used
  • usage: Token usage information
  • openai_embed(): standard embedding function returning just the vector