> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# openai_embed_with_raw_response()

> Generate embeddings and get the complete API response including metadata

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:

```sql theme={"dark"}
SELECT ai.openai_embed_with_raw_response(
    'text-embedding-ada-002',
    'PostgreSQL is powerful'
);
```

### Extract token usage

Check how many tokens were used:

```sql theme={"dark"}
SELECT
    (ai.openai_embed_with_raw_response(
        'text-embedding-ada-002',
        'sample text'
    )->>'usage')::jsonb;
```

## Arguments

| Name              | Type      | Default | Required | Description                                                                                  |
| ----------------- | --------- | ------- | -------- | -------------------------------------------------------------------------------------------- |
| `model`           | `TEXT`    | -       | ✔        | The OpenAI embedding model to use (e.g., `text-embedding-ada-002`, `text-embedding-3-small`) |
| `input_text`      | `TEXT`    | -       | ✔        | Single text input to embed (use this OR `input_texts` OR `input_tokens`)                     |
| `input_texts`     | `TEXT[]`  | -       | ✔        | Array of text inputs to embed in a batch                                                     |
| `input_tokens`    | `INT[]`   | -       | ✔        | Pre-tokenized input as an array of token IDs                                                 |
| `api_key`         | `TEXT`    | `NULL`  | ✖        | OpenAI API key. If not provided, uses `ai.openai_api_key` setting                            |
| `api_key_name`    | `TEXT`    | `NULL`  | ✖        | Name of the secret containing the API key                                                    |
| `dimensions`      | `INT`     | `NULL`  | ✖        | Number of dimensions for the output embedding (only supported by some models)                |
| `openai_user`     | `TEXT`    | `NULL`  | ✖        | Unique identifier for the end-user for abuse monitoring                                      |
| `encoding_format` | `TEXT`    | `NULL`  | ✖        | Format for the embeddings (`float` or `base64`)                                              |
| `extra_headers`   | `JSONB`   | `NULL`  | ✖        | Additional HTTP headers to include in the API request                                        |
| `extra_query`     | `JSONB`   | `NULL`  | ✖        | Additional query parameters for the API request                                              |
| `verbose`         | `BOOLEAN` | `FALSE` | ✖        | Enable verbose logging for debugging                                                         |
| `client_config`   | `JSONB`   | `NULL`  | ✖        | Advanced 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

## Related functions

* [`openai_embed()`][openai_embed]: standard embedding function returning just the vector

[openai_embed]: /api-reference/pgai/model-calling/openai/openai_embed
