[ Web Proxy ]
URL:
Viewing: https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-ai-embed [Back]  [Original]

The AI.EMBED function  |  BigQuery  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback Stay organized with collections Save and categorize content based on your preferences.

The AI.EMBED function

This document describes the AI.EMBED function, which lets you create embeddings from text or image data in BigQuery. For example, the following query creates an embedding for a piece of text:

The function works by sending a request to a stable Gemini Enterprise Agent Platform embedding model or a built-in embedding model in BigQuery, and then returning that model's response.

SELECT AI.EMBED("Some text to embed!", endpoint => 'text-embedding-005');

Embeddings

Embeddings are high-dimensional numerical vectors that represent a given entity. Machine learning (ML) models use embeddings to encode semantics about entities to make it easier to reason about and compare them. If two entities are semantically similar, then their respective embeddings are located near each other in the embedding vector space.

Embeddings help you perform the following tasks:

Input

Using the AI.EMBED function, you can use the following types of input:

When you analyze image data, the content must be in one of the supported image formats that are described in the Gemini API model mimeType parameter.

Syntax

Text embedding

AI.EMBED(
  [ content => ] 'content',
  { endpoint => 'endpoint' | model => 'model' }
  [, task_type => 'task_type']
  [, title => 'title']
  [, model_params => model_params]
  [, connection_id => 'connection']
)

Arguments

AI.EMBED takes the following arguments:

Multimodal embedding

AI.EMBED(
  [ content => ] 'content',
  endpoint => 'endpoint'
  [, connection_id => 'connection']
  [, model_params => model_params]
)

Arguments

AI.EMBED takes the following arguments:

Output

AI.EMBED returns a STRUCT value for each row in the table. The struct contains the following fields:

Examples

text embedding

The following example shows how to embed a string literal using an Agent Platform endpoint:

SELECT
  AI.EMBED(
    'A piece of text to embed',
    endpoint => 'text-embedding-005') AS embedding;

The result is similar to the following:

+-----------------------+------------------+
| embedding.result      | embedding.status |
+-----------------------+------------------+
| 0.11416172981262207   |                  |
| 0.0041960999369621277 |                  |
| ...                   |                  |
+-----------------------+------------------+

The embedding.result column contains a single row with an array of length 768. The embedding.status column contains an empty string, which indicates that the embedding was successful.

Alternatively, you can perform the embedding by using a built-in embedding model in BigQuery:

SELECT
  AI.EMBED(
    'A piece of text to embed',
    model => 'embeddinggemma-300m') AS embedding;

If you need to reuse embeddings of the same data across many queries, you should save the results to table. The following example generates 768-dimensional embeddings for publicly available BBC news articles and writes the results to a table:

CREATE OR REPLACE TABLE mydataset.bbc_news_embeddings AS
SELECT
  title,
  body,
  AI.EMBED(
    body,
    endpoint => 'text-embedding-005',
    model_params => JSON '{"outputDimensionality": 768}'
  ).result AS embedding
FROM
  `bigquery-public-data.bbc_news.fulltext`;

The following example queries the table that you just created for the five articles that are most related to the topic "latest news in tech". It calls the VECTOR_SEARCH function and uses AI.EMBED to create an embedding to pass to the function as the search query.

SELECT base.title, base.body
FROM
  VECTOR_SEARCH(
    TABLE mydataset.bbc_news_embeddings,
    # The name of the column that contains the embedding
    'embedding',
    # The embedding to search
    (SELECT AI.EMBED('latest news in tech', endpoint => 'text-embedding-005').result),
    top_k => 5);

multimodal embedding

The following query creates an external table from images of pet products stored in a publicly available Cloud Storage bucket. Then, it generates embeddings for two of the images:

# Create a dataset
CREATE SCHEMA IF NOT EXISTS cymbal_pets;

# Create an object table
CREATE OR REPLACE EXTERNAL TABLE cymbal_pets.product_images
WITH CONNECTION DEFAULT
OPTIONS (
  object_metadata = 'SIMPLE',
  uris = ['gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/images/*.png']
);

SELECT
  ref.uri,
  OBJ.GET_READ_URL(ref).url AS signed_url,
  AI.EMBED(
    ref,
    endpoint => 'multimodalembedding@001') AS embedding
FROM
  `cymbal_pets.product_images`
LIMIT 2;

The following query uses the gemini-embedding-2-preview (Preview) model to embed the combination of a text description and an image:

SELECT
  AI.EMBED(
    ('Made of tempered glass',
    OBJ.MAKE_REF('gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/images/aquaclear-20-gallon-aquarium.png')),
    endpoint => 'gemini-embedding-2-preview');

Choose a model

Use the following table to help you choose an embedding model for your data:
Model specification Output dimension Max sequence length Supported text languages Description Embedding location Billing and permissions
model => 'embeddinggemma-300m' 768 2048 tokens Supported text languages Best for embedding short strings (<= 128 tokens). Specialized for multilingual tasks. BigQuery query engine Uses and scales with BigQuery slots. No Gemini Enterprise Agent Platform charges or setup.
endpoint => 'gemini-embedding-001' up to 3072 2048 tokens Supported text languages Best performance for multilingual and coding tasks. Agent Platform endpoint Incurs Agent Platform charges. Might require Agent Platform permission setup depending on your project settings.
endpoint => 'text-embedding-005' up to 768 2048 tokens English Best for embedding long English strings. Specialized in English and coding tasks. Agent Platform endpoint Incurs Agent Platform charges. Might require Agent Platform permission setup depending on your project settings.
endpoint => 'text-multilingual-embedding-002' up to 768 2048 tokens Supported text languages Best for embedding long strings. Specialized in multilingual tasks. Agent Platform endpoint Incurs Agent Platform charges. Might require Agent Platform permission setup depending on your project settings.
endpoint => 'gemini-embedding-2-preview' up to 3072 8192 tokens Supported text languages Best for embedding long strings, including multilingual and unstructured data. Supports a mix of text, images, audio, video, and PDF files. Agent Platform endpoint Incurs Agent Platform charges. Might require Agent Platform permission setup depending on your project settings.

Billing and permissions

The project that is billed for Agent Platform usage and the permissions required for the connection depend on how you specify the endpoint value and the context in which the AI.EMBED function is called.

Billing model

The following table describes which project is billed for Agent Platform usage based on the endpoint value and the context:

Endpoint format Context Billed project
Full path URL.
For example, projects/PROJECT_ID/locations/us-central1/publishers/google/models/text-embedding-005
Standard query or search query The project ID specified in the full path URL.
Full path URL.
For example, projects/PROJECT_ID/locations/us-central1/publishers/google/models/text-embedding-005
Autonomous embedding background job The project ID specified in the full path URL.
Model name only.
For example, text-embedding-005
Standard query or search query The project ID in which the query is run.
Model name only.
For example, text-embedding-005
Autonomous embedding background job The project ID that contains the table.

Connection permissions

The connection used by the AI.EMBED function must have the Agent Platform User (roles/aiplatform.user) role in the project that is billed for Agent Platform usage.

Locations

You can run AI.EMBED in all of the locations that support Agent Platform embedding models, and also in the US and EU multi-regions.

The gemini-embedding-2-preview model is only supported in the US and us-central1 regions.

Quotas

See Agent Platform and Cloud AI service functions quotas and limits.

What's next

Send feedback

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-08-11 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-08-11 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page