- Categories:
String & binary functions (AI Functions)
AI_ EXTRACT¶
Extracts information from an input string or file.
Syntax¶
Extract information from an input string:
Extract information from a file:
Arguments¶
textAn input string for extraction.
fileA FILE for extraction.
Supported file formats:
- PNG
- PPTX, PPT
- EML
- DOC, DOCX
- JPEG, JPG
- HTM, HTML
- TEXT, TXT
- TIF, TIFF
- BMP, GIF, WEBP
- MD
The files must be less than 100 MB in size.
To extract information from selected pages of a paginated document, pass
FL_FILTER_PAGESas thefileargument.AI_EXTRACTprocesses only the selected pages. For page-range syntax and restrictions, see FL_FILTER_PAGES.responseFormatInformation to be extracted. The format depends on the type of extraction.
Entity extraction formats
Extract single values by providing one of the following formats:
-
Simple object schema that maps the label and information to be extracted:
-
An array of strings that contain the information to be extracted:
-
An array of arrays that contain two strings (label and the information to be extracted):
-
A JSON schema with
'type': 'string'on the sub-object:
List extraction format
Extract arrays of values using a JSON schema with
'type': 'array'on the sub-object:Table extraction format
Extract tabular data using a JSON schema with
'type': 'object'andcolumn_ordering. Each column is defined as a nested property with'type': 'array'and adescriptionthat matches the column name in the file:Note
-
You can’t combine the JSON schema format with other response formats. If
responseFormatcontains theschemakey, you must define all questions within the JSON schema. Additional keys are not supported. -
The model only accepts certain shapes of JSON schema. Top level type must always be an object, which contains independently extracted sub-objects. Sub-objects may be a table (object of lists of strings representing columns), a list of strings, or a string.
String is currently the only supported scalar type.
-
Use the
descriptionfield to provide context to the model; for example, to help the model localize the right table in a document. You can enter the column header name, or describe the column in another way. -
Use the
column_orderingfield to specify the order of all columns in the extracted table. Thecolumn_orderingfield is case-sensitive and must match the column names defined in thepropertiesfield. The order should reflect the order of the columns in the document.
-
scores => { boolean }Optional. Supported only in the named-argument syntax shown above. A BOOLEAN that controls whether the function returns scores for extracted values. The default is
FALSE. WhenTRUE, the JSON result includes ascoringobject in addition toresponse. For the output format, SQL examples, and limitations, see Extraction scores.
Returns¶
A JSON object containing the extracted information. The structure of the response depends on the type of extraction.
When cross-region inference is enabled and AI_EXTRACT can’t process a file, the error field contains the error for that document. The statement continues for
any other documents. This includes a document that exceeds the 1,000-page limit, a corrupted file, and a file that isn’t in a
supported format. Without cross-region inference, a file that AI_EXTRACT can’t process fails the SQL statement. For the page limit in that case, see Usage notes.
Entity extraction¶
Returns a JSON object with key-value pairs for each extracted entity:
List extraction¶
Returns a JSON object with arrays of extracted values:
Table extraction¶
Returns a JSON object with column arrays representing the extracted table:
Combined extraction¶
When extracting entities, lists, and tables in a single call, the response contains all extraction types:
Extraction scores¶
When you use AI_EXTRACT, you can request scores that indicate the model’s certainty about each extracted value. You can use these scores to set thresholds for business logic, such as flagging low-scoring extractions for human review.
A higher score indicates a higher likelihood that the extracted value is correct. You can compare scores for extracting a given entity across different documents to identify which values are more or less reliable, and use them to build deterministic processing logic such as thresholds, fallback mechanisms, and human-in-the-loop workflows.
How scores work¶
When you set the scores parameter to TRUE, AI_EXTRACT returns a scoring object alongside the standard
response object. The scoring object contains a score for each extracted field.
The scores parameter is optional, and it is set to FALSE by default. Use the optional scores argument in the
named-argument syntax shown in Arguments.
Scoring output format¶
When scores => TRUE, the returned JSON includes a scoring object:
Each field in scoring.scores corresponds to a field in response and contains a score value between 0 and 1.
For list extraction, the scoring object returns an aggregate score for the entire list:
For table extraction, the scoring object returns an aggregate score for the entire table:
Scoring usage notes¶
- Requesting scores does not incur additional cost. For general information on AI_EXTRACT costs, see Cost considerations.
- Per-element scores for individual list items and table cells are not available.
- Scores are supported for fine-tuned models.
Examples with extraction scores¶
The following example extracts information from a file and returns scores for each extracted field:
Result:
The following example extracts a list of buyer names and returns an aggregate score:
Result:
The following example extracts a table and returns an aggregate score:
Result:
Access control requirements¶
Users must use a role that has been granted the SNOWFLAKE.CORTEX_USER database role. For information about granting this privilege, see Cortex LLM privileges.
Usage notes¶
-
AI_EXTRACT is optimized for both digital-born and scanned documents.
-
You can’t use both
textandfileparameters simultaneously in the same function call. -
Page selection with
FL_FILTER_PAGESapplies to thefileargument, not thetextargument. Files without pages, such as plain-text files, cannot be filtered by page. -
Use
FL_FILTER_PAGESdirectly inside theAI_EXTRACTcall. The selection applies only to that call; it is not stored on theFILEvalue and does not create a new document. -
Multiple ranges select pages from the original document. Overlapping ranges do not cause the same page to be processed more than once, and selected pages are processed in document order.
-
A valid selection that resolves to no pages returns an empty result. For invalid selections and other page-selection errors, see Error conditions.
-
With cross-region inference enabled:
- File extraction supports documents of up to 1,000 pages. A document that exceeds the limit returns an error for that document.
- A request that extracts many fields from a document returns one combined result.
- If AI_EXTRACT can’t process a file, the
errorfield contains the error for that document and the statement continues. This includes a document that exceeds the page limit, a corrupted file, and a file that isn’t in a supported format. scale_factoris no longer needed. AI_EXTRACT now delivers improved extraction quality without manual scaling adjustments. Existing calls continue to work unchanged; anyscale_factorvalue is ignored.
-
Without cross-region inference:
- File extraction supports documents of up to 125 pages.
- A file that AI_EXTRACT can’t process fails the SQL statement. This includes a corrupted file and a file that isn’t in a supported format.
- The
scale_factorparameter still applies. It scales pages of an input file before extraction, which can improve results for pages larger than A4, small text, or dense layouts. Set it to a numeric value from 1.0 through 4.0. The default is 1.0.
-
You can either ask questions in natural language or describe information to be extracted (such as city, street, ZIP code); for example:
-
The following languages are supported:
- Arabic
- Bengali
- Burmese
- Cebuano
- Chinese
- Czech
- Dutch
- English
- French
- German
- Hebrew
- Hindi
- Indonesian
- Italian
- Japanese
- Khmer
- Korean
- Lao
- Malay
- Persian
- Polish
- Portuguese
- Russian
- Spanish
- Tagalog
- Thai
- Turkish
- Urdu
- Vietnamese
-
In a single AI_EXTRACT call, you can ask a maximum of 100 questions for entity extraction, and a maximum of 10 questions for table extraction.
A table extraction question is equal to 10 entity extraction questions. For example, you can ask 4 table extraction questions and 60 entity extraction questions in a single AI_EXTRACT call.
-
The maximum output length for entity extraction is 512 tokens per question. For table extraction, the model returns answers that are a maximum of 4096 tokens.
-
You can request optional extraction scores by using named arguments and passing
scores => TRUE. For details, see Extraction scores.
AI_EXTRACT supports documents on stages that use client-side or server-side encryption, including in accounts that use PrivateLink or other network policies that restrict public network access to stages. Files on external stages that use client-side encryption configured in Snowflake (for example, by using a COPY INTO statement) are supported.
Cost considerations¶
-
The Cortex AI_EXTRACT function incurs compute cost based on the number of pages per document, input prompt tokens, and output tokens processed.
- For paged file formats (PDF, DOCX, TIF, TIFF), each page is counted as 970 tokens.
- For image file formats (JPEG, JPG, PNG), each individual image file is billed as a page and counted as 970 tokens.
-
Snowflake recommends executing queries that call the Cortex AI_EXTRACT function in a smaller warehouse (no larger than MEDIUM). Larger warehouses don’t increase performance.
Regional availability¶
AI_EXTRACT is available to accounts in the following regions:
| Cloud platform | Region name |
|---|---|
| Amazon Web Services (AWS) |
|
| Microsoft Azure |
|
| Google Cloud Platform |
|
AI_EXTRACT has cross-region support. For information on enabling Cortex AI cross-region support, see Cross-region inference.
Error conditions¶
AI_EXTRACT can produce the following error messages:
| Message | Explanation |
|---|---|
Internal error. | A system error occurred. Wait and try again. If the error persists, contact Snowflake support. |
Not found. | The file was not found. |
Provided file cannot be found. | The file was not found. |
Provided file cannot be accessed. | The current user does not have sufficient privileges to access the file. |
The provided file format {file_extension} isn't supported. | The document isn’t in a supported format. With cross-region inference enabled, AI_EXTRACT returns this error for that document. Otherwise, the statement fails. |
The provided file isn't in the expected format or is corrupted. | The document is corrupted or isn’t in a supported format. With cross-region inference enabled, AI_EXTRACT returns this error for that document. Otherwise, the statement fails. |
Empty request. | No parameters were provided. |
Missing or empty response format. | No response format was provided. |
Invalid response format. | The response format is not valid JSON. |
Duplicate feature name found: {feature_name}. | The response format contains one or more duplicate feature names. |
Too many questions: {number} complex and {number} simple = {number} total, complex question weight {number}. | The number of questions exceeds the allowed limit. |
Maximum number of 1000 pages exceeded. The document has {actual_pages} pages. | Cross-region inference is enabled, and the document exceeds the 1,000-page limit. AI_EXTRACT returns this error for that document. |
Maximum number of 125 pages exceeded. The document has {actual_pages} pages. | Cross-region inference isn’t enabled, and the document exceeds the 125-page limit. |
Page size in pixels exceeds 10000x10000. The page size is {actual_px} pixels. | Image input or a converted document page is larger than the supported dimensions. |
Page size in inches exceeds 50x50 (3600x3600 pt). The page size is {actual_in} inches ({actual_pt} pt). | Page is larger than the supported dimensions. |
Maximum file size of 104857600 bytes exceeded. The file size is {actual_size} bytes. | The document is larger than 100 MB. |
Examples¶
Entity extraction¶
-
The following example extracts entities from the input text using a simple object schema:
-
The following example extracts and parses entities from the input text:
-
The following example extracts entities from the
document.pdffile: -
The following example extracts entities from all files in a directory on a stage:
Note
Ensure that the directory table is enabled. For more information, see Manage directory tables.
-
The following example extracts the
titleentity from thereport.pdffile using a JSON schema:
List extraction¶
The following example extracts the employees list from the report.pdf file:
Table extraction¶
The following example extracts the income_table table from the report.pdf file:
Extract information from the first and last pages¶
The following example selects the first and last pages of a contract:
On a 10-page document, this selects pages with indexes 0 and 9. It does not process the intervening pages.
Combined extraction¶
The following example extracts a table (income_table), entity (title), and list (employees) from the report.pdf
file in a single call:
Extraction using a fine-tuned arctic-extract model¶
To use the fine-tuned arctic-extract model for inference with the AI_EXTRACT function,
specify the model using the model parameter as shown in the following example:
You can overwrite questions used for fine-tuning by using the responseFormat parameter as shown in the following example:
For more information, see Fine-tuning arctic-extract models.
Note
AI_EXTRACT is the updated version of EXTRACT_ANSWER. For the latest functionality, use AI_EXTRACT.
Legal notices¶
Refer to Snowflake AI and ML for legal notices.