Skip to main content
Bird & Bear
Menu
← All articles

BigQuery Working Examples: AI.PARSE_DOCUMENT

bigquery ai sql
BigQuery Working Examples: AI.PARSE_DOCUMENT

AI.PARSE_DOCUMENT reached general availability in BigQuery on November 2, 2026. It uses the Document AI layout parser to analyse a document’s structure and extract its layout elements, titles, headings, paragraphs, lists, tables, and images, converting an unstructured document into a structured JSON representation in a single step.

How this differs from AI.CHUNK_DOCUMENT

We covered AI.CHUNK_DOCUMENT in a previous entry in this series, and the two share so much underneath that it is easy to assume they do the same job. They don’t.

AI.CHUNK_DOCUMENT is a table-valued function. You call it in a FROM clause and it hands back multiple rows, one per chunk, each sized for embedding generation. It is built specifically for the RAG pipeline step of turning a document into embedding-ready pieces.

AI.PARSE_DOCUMENT is a scalar function. You call it in a SELECT list against one document and it hands back a single JSON value containing the document’s full layout, every block it found, in reading order. It is built for getting a document’s actual structure out as data, whether or not you ever turn that into embeddings.

The two are designed to work together, not as alternatives. Parse a document once with AI.PARSE_DOCUMENT using its raw-docai output schema, then feed that result straight into AI.CHUNK_DOCUMENT, and the document only gets sent to Document AI once instead of twice. See below for more information on how that works.

Both functions share the same underlying mechanism worth knowing about either way: BigQuery automatically provisions a Document AI layout parser processor, named bq-created-layout-parser, in your project the first time you call either function, and reuses that same processor on every call after that. Neither function asks you to create or train a model yourself.

Syntax

AI.PARSE_DOCUMENT(
  [ document => ] DOCUMENT
  [, connection_id => 'CONNECTION_ID' ]
  [, options => OPTIONS ]
)

DOCUMENT is the document to parse, either a Cloud Storage URI string ('gs://my-bucket/docs/doc.pdf') or an ObjectRef value, from OBJ.MAKE_REF, OBJ.LIST, or an object table. A NULL document returns NULL. Files in Google Drive must be passed as an ObjectRef value, not a Drive URI string directly.

connection_id is optional: A Cloud resource connection in the form PROJECT_ID.LOCATION.CONNECTION_ID. Leave it out and your own Google Account user credentials are used instead.

options is an optional JSON object with a single field, schema, set to either 'layout-v1' (the default) or 'raw-docai'.

The two output schemas, and when to use each

This is the one genuinely important choice this function asks you to make.

layout-v1, the default, returns a flat list of blocks in reading order. Each block carries an id, a type (title, heading, text, table, image, list_item, page_header, or page_footer), its content (tables come back as HTML), a page_number, and a normalised bounding box. It is compact, and it is the shape to reach for when you want to query the structure directly in SQL or pass it to an agentic workflow.

raw-docai returns the full nested Document AI layout tree instead, sections and their child elements kept together rather than flattened. It is more verbose, and its main purpose is as input to AI.CHUNK_DOCUMENT, which only accepts this schema, not layout-v1.

A worked example, against Google’s own public sample documents

Our shared dataset, bq_ai_demo.ecommerce_orders, has nothing to parse, the same gap we flagged when covering AI.CHUNK_DOCUMENT. As with that article, the queries below use Google’s own public Cloud Storage sample bucket, the one in Google’s reference documentation, so every query here is genuinely runnable as written.

The simplest possible call, parsing a single PDF with the default schema:

SELECT
  AI.PARSE_DOCUMENT(
    'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/documents/crittercuisine_5000_user_manual.pdf'
  ) AS parsed_doc;

The result is one row, one JSON value, containing metadata.schema and a blocks array. The first few blocks of that array look like this:

{
  "metadata": { "schema": "layout-v1" },
  "blocks": [
    {
      "id": 0,
      "type": "title",
      "content": "CritterCuisine Pro 5000 - Automatic Pet Feeder - User Manual",
      "page_number": 1,
      "bbox": [0.05, 0.10, 0.12, 0.40]
    },
    {
      "id": 1,
      "type": "text",
      "content": "Version 1.2 (Revised: October 26, 2023)",
      "page_number": 1,
      "bbox": [0.15, 0.10, 0.18, 0.60]
    }
  ]
}

Flattening the JSON into proper rows

A single JSON cell is awkward to actually work with. JSON_QUERY_ARRAY and UNNEST turn it into one row per block, which is the shape you actually want for filtering, grouping, or just reading:

SELECT
  INT64(block.id) AS block_id,
  JSON_VALUE(block.type) AS block_type,
  INT64(block.page_number) AS page_number,
  JSON_VALUE(block.content) AS content
FROM
  UNNEST(
    JSON_QUERY_ARRAY(
      AI.PARSE_DOCUMENT(
        'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/documents/crittercuisine_5000_user_manual.pdf'
      ).blocks
    )
  ) AS block;

This is the version worth keeping as a template. Swap the Cloud Storage URI for your own document and you have a table of every heading, paragraph, table, and image in it, each tagged with the page it came from.

Parsing a whole folder at once

OBJ.LIST pairs with AI.PARSE_DOCUMENT the same way it pairs with AI.CHUNK_DOCUMENT, for a whole Cloud Storage prefix rather than one file:

SELECT
  uri,
  AI.PARSE_DOCUMENT(ref) AS parsed_doc
FROM
  OBJ.LIST(
    'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/document_chunks/*.pdf'
  );

Choosing the raw-docai schema, and chaining into AI.CHUNK_DOCUMENT

Set options => JSON '{"schema": "raw-docai"}' to get the full nested tree instead of the flat list:

SELECT
  AI.PARSE_DOCUMENT(
    'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/documents/crittercuisine_5000_user_manual.pdf',
    options => JSON '{"schema": "raw-docai"}'
  ) AS parsed_doc;

That schema exists mainly to feed the next step. Parse once, in raw-docai form, then hand the result straight to AI.CHUNK_DOCUMENT for genuinely embedding-ready chunks, without sending the document to Document AI twice:

SELECT
  uri,
  chunk_id,
  start_page,
  end_page,
  content
FROM
  AI.CHUNK_DOCUMENT(
    (
      SELECT
        uri,
        AI.PARSE_DOCUMENT(
          ref,
          options => JSON '{"schema": "raw-docai"}'
        ) AS parsed_document
      FROM
        OBJ.LIST(
          'gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/document_chunks/*.pdf'
        )
    )
  );

Handling a failed parse

A failed document, an unsupported file type, for example, still returns a JSON value rather than throwing an error partway through a batch. The blocks array comes back empty, and an errors array carries the detail:

{
  "metadata": { "schema": "layout-v1" },
  "errors": [
    { "code": "INVALID_ARGUMENT", "message": "Unsupported mime type for document parsing: text/markdown" }
  ],
  "blocks": []
}

Checking JSON_QUERY_ARRAY(parsed_doc.errors) for an empty array is the direct way to find which documents in a batch failed, rather than inferring it from a suspiciously short blocks array.

What to do before enabling

AI.PARSE_DOCUMENT needs the Document AI API (documentai.googleapis.com) enabled on your project. Beyond that, the roles needed depend on how you authenticate and whether the shared bq-created-layout-parser processor already exists.

Using your own Google Account credentials: Document AI Editor to create the processor the first time, or just Document AI Viewer once it already exists; Storage Object Viewer or Storage Object User on the Cloud Storage bucket holding your documents; and ordinary view access to any Drive files you pass in.

Using a Cloud resource connection instead: the connection’s service account needs the same Document AI Editor or Viewer role, plus Storage Object Viewer on the bucket, and you additionally need BigQuery Connection User on the connection itself.

Limits worth knowing before you build a pipeline

A document can run to at most 130 pages, and there is no option to select a specific page range, it is the whole document or nothing. Drive files only work when passed as an ObjectRef, never as a bare Drive URI string. Chaining into AI.CHUNK_DOCUMENT only works from the raw-docai schema, layout-v1 output cannot be chunked this way.

Pricing and where it runs

Parsing is billed through the Document AI Layout Parser API, per page processed, at standard Document AI pricing. There is no idle holding fee for the shared processor: delete bq-created-layout-parser from the console and BigQuery simply recreates it on the next call. Ordinary BigQuery slot usage is billed on top, as with any query. AI.PARSE_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, the same regional footprint as AI.CHUNK_DOCUMENT.

Where this fits in the bigger picture

This is the same thread we have been pulling on since AI.CHUNK_DOCUMENT: documents that used to need a separate processing step outside the warehouse are now something BigQuery handles natively. What AI.PARSE_DOCUMENT adds specifically is a view of a document’s actual structure, not just embedding-sized pieces of its text. A heading is tagged as a heading. A table comes back as a table, in HTML, not as a wall of text with the column boundaries guessed at by whatever split a chunking step happened to make.

For anyone building toward genuinely AI-ready data, that distinction matters more than it sounds. An agent that gets handed “this paragraph is a heading, this block is a table” can reason about a document properly. An agent that only gets handed evenly sized chunks of plain text is working with less than it could have, for no good reason, when the structure was there in the original document all along.

Newsletter

Occasional notes on data, tracking & AI-readiness

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