Skip to main content
Bird & Bear
Menu
← All articles

BigQuery Working Examples: AI.CHUNK_DOCUMENT

bigquery ai analytics sql
BigQuery Working Examples: AI.CHUNK_DOCUMENT

The seventh in our series of BigQuery AI function examples. The first six worked on numbers (AI.KEY_DRIVERS, AI.FORECAST, AI.CAUSAL_EFFECT, ML.CORRELATION, ML.METRICS) or on short text (AI.AGG). This one works on whole documents, and unlike those others, it isn’t a function you call in a SELECT list, it’s a table-valued function you call in a FROM clause.

AI.CHUNK_DOCUMENT reached general availability in BigQuery on October 21, 2026. It splits document content into chunks sized for embedding generation, using the Document AI layout parser to split on actual document structure, headings, sections, paragraphs, lists, tables, rather than cutting at a fixed character count regardless of what that count lands in the middle of.

Why chunking is its own function

An embedding model can only usefully represent a few hundred to a couple of thousand words at a time. Feed it a forty-page PDF in one go and the resulting embedding is a blurry average of everything in the document, not something you can search against meaningfully. Feed it one paragraph at a time and the results are precise, but somebody has to do the splitting first, consistently, in a way that doesn’t cut a sentence in half or separate a heading from the paragraph it introduces.

AI.CHUNK_DOCUMENT does that splitting as a BigQuery-managed service. You don’t create or train a model, BigQuery automatically provisions a Document AI layout parser processor (named bq-created-layout-parser) in your project the first time you run it, and reuses the same one on every subsequent call rather than provisioning a new one per query.

Syntax

AI.CHUNK_DOCUMENT(
  { 'STORAGE_URI' | TABLE INPUT_TABLE | (QUERY_STATEMENT) }
  [, connection_id => 'CONNECTION_ID' ]
  [, chunk_size => CHUNK_SIZE ]
)

The first argument takes one of three shapes: a Cloud Storage URI string, with an optional trailing wildcard ('gs://my-bucket/docs/*.pdf'); an existing table or object table containing an ObjectRef column named ref; or a subquery that returns one. connection_id is required when the input is an object table (it must match that table’s own connection), optional otherwise, Google calls this authenticating with a Cloud resource connection, as opposed to the default of authenticating with your own end-user credentials (EUC). chunk_size is the target maximum chunk size in tokens, between 1 and 2000, default 1000.

Enable the Document AI API

AI.CHUNK_DOCUMENT needs the Document AI API (documentai.googleapis.com) enabled on your project, and whichever authentication route you use needs specific roles: Document AI Editor (or Viewer, if the processor already exists) to create or invoke bq-created-layout-parser, plus Storage Object Viewer or Storage Object User on whatever Cloud Storage bucket holds your documents. If you authenticate through a connection rather than EUC, those roles belong to the connection’s service account, and you additionally need BigQuery Connection User on the connection itself.

A worked example

The examples below use Google’s own public Cloud Storage sample buckets, the same ones in Google’s reference documentation. They’re public, so every query below is runnable as written.

The simplest possible call, straight against a single PDF:

SELECT
  chunk_id,
  start_page,
  end_page,
  content
FROM
  AI.CHUNK_DOCUMENT(
    'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/documents/crittercuisine_5000_user_manual.pdf'
  );

Each output row is one chunk: its 1-based index within the document, the page range it spans, and the extracted text. Headings come through as Markdown (# CritterCuisine Pro 5000 - Automatic Pet Feeder - User Manual), which is the layout parser doing its job, preserving structure rather than just extracting raw text.

Chunking multiple files with OBJ.LIST and a connection

For a whole folder of documents, or when access needs a specific connection’s credentials rather than your own, pair AI.CHUNK_DOCUMENT with OBJ.LIST, and use the same connection_id in both:

SELECT
  chunk_id,
  start_page,
  end_page,
  content,
  ref.uri
FROM
  AI.CHUNK_DOCUMENT(
    (SELECT ref
     FROM OBJ.LIST(
       'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/document_chunks/*.pdf',
       'us.example_connection')),
    connection_id => 'us.example_connection',
    chunk_size => 250
  );

Note chunk_size => 250 here, a tighter target than the 1000-token default, worth tuning down for shorter, more targeted chunks if your downstream embedding or retrieval step benefits from finer granularity.

Chunking from an ObjectRef column or an object table

If you already have an ObjectRef sitting in a column you can pass that straight in:

SELECT
  chunk_id,
  start_page,
  end_page,
  content
FROM
  AI.CHUNK_DOCUMENT(
    (SELECT OBJ.MAKE_REF(
       'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/documents/crittercuisine_5000_user_manual.pdf') AS ref)
  );

Or, for a whole object table of documents (the connection_id here is required and must match the one the table was created with):

CREATE OR REPLACE EXTERNAL TABLE cymbal_pets.product_manual
WITH CONNECTION us.example_connection
OPTIONS (
  object_metadata = 'SIMPLE',
  uris = ['gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/document_chunks/*.pdf']
);

SELECT *
FROM AI.CHUNK_DOCUMENT(TABLE cymbal_pets.product_manual, connection_id => 'us.example_connection');

What the output contains

Beyond chunk_id, start_page, end_page and content, each row also carries status (empty string on success, an error code and message such as INVALID_ARGUMENT or PERMISSION_DENIED if that particular file or chunk failed), plus the source object’s own metadata: uri, generation, content_type, size, md5_hash, updated, a metadata array of further Cloud Storage key/value pairs, and the ref ObjectRef itself. Checking status across a batch is the direct way to find out which files in a folder failed to process, rather than just noticing a gap in the row count.

Pricing and where it runs

Chunking is billed through the Document AI Layout Parser API, per page processed, standard Document AI pricing applies. There’s no idle holding fee for the auto-provisioned processor, delete it from the console and BigQuery just recreates it next time you call the function. Ordinary BigQuery slot usage is billed on top, as with any query. AI.CHUNK_DOCUMENT is supported in the US and EU multi-regions, and in all single regions within the US and EU except europe-west2 and europe-west6.

Where this fits in the bigger picture

This is the running pattern we’ve been tracking in BigQuery’s release notes since August: Google steadily turning “thing you’d build yourself, outside the warehouse” into “thing that’s already there, inside a query.” Chunking is a particularly clean example, one of the most common pieces of plumbing in any RAG project, and until now it’s never been something you could do without leaving BigQuery.

For anyone sitting on genuinely unstructured content inside a data warehouse, product manuals, contracts, support tickets, spec sheets, this closes a real gap between “we have the documents” and “an AI tool can actually answer questions about what’s in them”.

What is this useful for?

The old version of this step was always a separate script, a LangChain text splitter or similar, bolted onto the side of wherever the actual document data lived. AI.CHUNK_DOCUMENT turns it into a table-valued function, governed by the same IAM and billing as everything else in the warehouse. It doesn’t do the embedding itself, that’s a separate step, but it does the part that used to require the most fiddly, document-specific logic: deciding where one chunk ends and the next begins without cutting through the middle of a sentence or a table row.

All articles in this series

Newsletter

Occasional notes on data, tracking & AI-readiness

No spam, no growth-hacking. Just an email when there's something worth reading.