AI_PARSE_DOCUMENT
functiondeferredAI & MLReturns document content from a Snowflake stage as a JSON-formatted string. Supports text extraction (OCR), text and layout extraction (LAYOUT), and text, layout, and visual content extraction (AGENTIC). For more information, see Parsing documents with AI_PARSE_DOCUMENT. Available natively to accounts in select regions. Available through cross-region inference to accounts in all regions.
Syntax
supportedpartialnot supportednot scored
AI_PARSE_DOCUMENT( <file_object> [, <options> ] [, <return_error_details> ] )
Parameters
| Name | Required | Description |
|---|---|---|
file_object | required | A FILE object that specifies the document to parse, stored in a Snowflake stage. For information about creating file objects, see TO_FILE. To parse selected pages of a paginated document, pass FL_FILTER_PAGES as the file_object argument. AI_PARSE_DOCUMENT processes only the selected pages. For page-range syntax and restrictions, see FL_FILTER_PAGES. |
options | optional | An OBJECT value that contains options for parsing documents. The supported keys are shown below. All are optional. 'extract_images': If set to TRUE, the function extracts images embedded in the document. Requires LAYOUT mode. 'mode': Specifies the parsing mode. The supported modes are: 'OCR': The function extracts text only. This is the default mode. 'LAYOUT': The function extracts layout as well as text, including structural content such as tables. 'AGENTIC': The function goes beyond text and layout extraction to interpret visual elements, generate semantic summaries, and extract structured data from charts, diagrams, tables, and other visuals. For more information, see Agentic mode. 'page_split': If set to TRUE, the function splits the document into pages and processes each page separately. This feature supports only PDF, PowerPoint (.pptx), and Word (.docx) documents. Documents in other formats return an error. The default is FALSE. Tip To process long documents that exceed the token limit of AI_PARSE_DOCUMENT, set this option to TRUE. 'page_filter': An array that specifies ranges of pages of a multi-page document to process. Each range is an object with start and end fields that specify the first (inclusive) and last (exclusive) page in the range. Page indexes start at 0. For example, {'start': 0, 'end': 1} specifies the first page of the document. Alternatively, specify the page selection by passing FL_FILTER_PAGES as the file_object argument. Do not combine FL_FILTER_PAGES and page_filter in the same call; the selections are not merged and neither takes precedence. Note Specifying page_filter implies page_split. If you specify page ranges, it is not necessary to also set page_split. |
return_error_details | optional | A BOOLEAN flag that indicates whether to return error details in case of error. When set to TRUE, the function returns an OBJECT that contains value, error, and metadata fields. The value field contains the parsed document data, the error field contains the error message (or NULL on success), and metadata is returned alongside value and error. value uses the shape in Returns. See Error behavior for details. |
Returns
By default the function returns the JSON as a string, so use PARSE_JSON to read the fields in SQL. If parsing fails, the function returns NULL. Every successful result includes metadata.pageCount, which is the total number of pages in the source document. To explicitly request page-level output, set page_split to TRUE. The page_filter option also enables page splitting automatically. Page splitting off. The document is one content string: plain text in OCR mode, or Markdown in LAYOUT mode. Page splitting on. When page splitting is enabled, pages contains one object for each processed page, including for a one-page file. If you select pages with FL_FILTER_PAGES or page_filter, only the selected pages are included. Each page’s index is its zero-based position in the original document, not its position in the filtered result. For example, selecting indexes 7 and 9 returns indexes 7 and 9, not 0 and 1. Printed page numbers are ignored. dimensions has height and width. When you select pages with FL_FILTER_PAGES or page_filter, metadata.pageCount still reports the source document’s total page count. The number of pages processed and billed is in metadata.processedPageCount. Neither field identifies the selected pages. Use the page index values in page-level output to identify the selected pages. Images. images is included when 'mode' is 'LAYOUT' and 'extract_images' is TRUE, or when 'mode' is 'AGENTIC'. It is a top-level array when page splitting is off, and an array on each page when page splitting is on. The array is [] when there are no images. Markdown in content refers to an image by id, for example . In AGENTIC mode, each image entry contains a results object with the semantic summary and structured details for that visual. For field definitions, see Expected image result schemas for AGENTIC mode. Preview Feature — Open Available to all accounts. Important AGENTIC mode generates image results on a best-effort basis. The schemas in this section describe the expected structure of the model-generated results object, but schema conformance isn’t enforced or guaranteed. Returned fields, types, and values can vary. Validate the fields your application requires, and handle missing, null, or unexpected values. These schemas describe the results for images that are processed successfully. If processing fails for an image, its results object contains the error marker described in Partial results in AGENTIC mode, which also includes an example response. The expected results object contains these fields: All field paths in the following sections are relative to details. The notation [] refers to an element of an array. The following fields are expected when image_type is chart: When printed values with unit or scale suffixes are returned as bare numbers, the expected output uses one consistent unit for the entire series and records it in series[].unit. For example, values printed as $97B and $1.08T can be represented as: Alternatively, values can retain their original strings, such as "$97B" and "$1.08T". In that representation, each value carries its own unit and scale, and series[].unit is expected to be null. Category-level series depend on a readable mapping between segments and category names, such as a clear legend or directly labeled segments. When that mapping is ambiguous, the expected output is a single series representing total stacked heights, with an explanation in notes, rather than an inferred category breakdown. The following fields are expected when image_type is table: The following fields are expected when image_type is diagram: The following fields are expected when image_type is screenshot: The following fields are expected when image_type is document_page: The following fields are expected when image_type is infographic: The following fields are expected when image_type is photo_with_text: The following fields are expected when image_type is other:
Usage notes
- Use FL_FILTER_PAGES directly as the file_object argument. The page selection applies only to the enclosing AI_PARSE_DOCUMENT call. It does not modify the stored document or persist on the FILE value.
- Page filtering requires a paginated document. Non-paginated inputs, such as plain-text files, return an error when filtered by page.
- AGENTIC mode supports up to 2,000 images per document. This limit applies to the document, not to each page.
- AGENTIC mode doesn’t support the 'extract_images' option. If you set 'extract_images' to TRUE in AGENTIC mode, the function returns an error. To extract embedded images as base64-encoded data, use LAYOUT mode with 'extract_images' set to TRUE.
Error behavior
By default, if AI_PARSE_DOCUMENT can’t process the input, the function returns NULL. If the query processes multiple rows, rows with errors return NULL and don’t prevent the query from completing. An invalid FL_FILTER_PAGES selection, or a valid selection that resolves to no pages, produces a row-level error. By default, AI_PARSE_DOCUMENT returns NULL for the affected row. Set return_error_details to TRUE to return the error information. For range validation rules, see Error conditions. The following table shows the return value based on the return_error_details argument: return_error_details | Return value | Description FALSE Not passed | NULL | TRUE | OBJECT with value, error, and metadata fields | value: An OBJECT containing the parsed document data, or NULL if an error occurred. error: A VARCHAR value that contains the error message if an error occurred, or NULL if the function succeeded. The error field inside value (renamed from errorInformation) contains per-document error details when present. metadata: An OBJECT containing document metadata such as page count. This field is returned alongside value and error. For more information about error handling for AI functions, see Snowflake Cortex AI Function: Multirow error handling improvements. If processing fails for an image, AI_PARSE_DOCUMENT still returns the parsed document content and the available results for other images. The affected image entry contains the following error marker: This marker indicates a processing failure for that image, not a failure to parse the entire document. Check image-level results before using them in downstream processing. The following illustrative response contains successfully extracted details for one chart and one image that couldn’t be processed. Image coordinates are omitted for brevity.
Legal notices
Refer to Snowflake AI and ML for legal notices.
Known gaps
- The emulator does not run language, embedding, document or speech models, so Cortex AI functions have no model to answer with.