snowflake.snowpark.DataFrameReader.documents¶
- DataFrameReader.documents(path: str) DataFrame[source]¶
Specify the path of the document(s) to read: the files are parsed to text, or a schema or a prompt turns the read into an extraction of structured values.
The reader chooses the model and how a long document is split across model calls; none of that is configurable. Options set with
option()oroptions()(names are case-insensitive, any other option is ignored):schema: the fields to extract. A JSON Schema dict (with apropertiesdict), a dict of{field: question}, or a list of field names or"field: question"strings. The result has one column per field, named in upper case.prompt: instructions for the model. With aschemait guides the extraction. Without one the model’s answer is returned as text.parse_mode: how files are parsed when there is noschemaorprompt:"layout"(the default),"ocr"or"text".row_boundary:"document"(the default) returns one row per file;"page"returns one row per page. Used withparse_modeonly.extract_images:TrueorFalse(the default). WhenTruethe images embedded in the documents are extracted too. It needsparse_mode"layout"and raises an error with any other mode.
parse_mode,row_boundaryandextract_imagesare ignored when aschemaorpromptis set: the file is then sent to the model directly, which keeps layout such as table columns that parsing would flatten.The returned DataFrame has these columns:
no
schemaorprompt:SOURCE_FILE,TOTAL_PAGESandCONTENT(the parsed text);PAGE_INDEXis added whenrow_boundaryis"page".IMAGESis added whenextract_imagesisTrue: an array of objects with anid, the bounding-box coordinates of the image and its data as base64 text, so rows of image-heavy documents can be large.schema:SOURCE_FILE, one column per schema field, andEXTRACTION_SCORES.EXTRACTION_SCORESis an object that maps each top-level schema field to a confidence score between 0 and 1 for its extracted value (one score for a whole array or object field). The model reports the scores itself, so they are not calibrated probabilities, and a field without a score is left out.promptonly:SOURCE_FILEandRESPONSE.
Every read also has a case-sensitive
"_document_error"column (quote the name when you select it). A document that fails does not fail the read: its row holds an array of{stage, message}entries describing what went wrong, andNULLwhen the document succeeded. A read stops early with an error when most of its documents fail, or when no supported model is available to the account.Note
The reader registers temporary procedures and functions in the current session. When
documents()is called from inside a stored procedure, that procedure must run with caller’s rights (execute_as="caller"): an owner’s-rights procedure cannot create temporary objects, and the read fails with “Unsupported statement type ‘temporary FUNCTION’”.Note
The read is lazy: the documents are read (and the model calls are made, and billed) when an action such as
collect()orshow()runs, and every action runs the whole read again. To use the result more than once, calldf.cache_result()or write the DataFrame to a table first.Note
With a
schema, the supported file types are.txt,.md,.pdf,.doc,.docx,.xls,.xlsx,.csvand.xhtml. Other files in the path are not sent to the model; each gets an error in its_document_error. Only PDFs are split into page ranges, so a large file of another type can exceed the model’s input size and fail as a whole. With apromptonly, every file in the path is sent to the model, and a file it cannot read gets an error in its_document_error.- Parameters:
path – The stage location of a document, or a stage location that has documents.
Example:
This function or method is in private preview since 1.56.0.