Parse Documents with Upstage Layout Analysis
Parse documents into text, tables, and figures via the Upstage Layout Analysis API.
Why it matters
Leverage the Upstage Layout Analysis API to extract structured data, including text, tables, and figures, from various document formats. This asset enables efficient data ingestion for downstream AI applications.
Outcomes
What it gets done
Load and parse document files (PDF, images, etc.).
Detect and extract elements like text, tables, and figures.
Optionally perform OCR and control output formats (text, HTML, Markdown).
Retrieve bounding box coordinates for extracted elements.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-reader-readers-upstage | bash Overview
UpstageDocumentParseReader
A LlamaIndex reader that uses Upstage's Layout Analysis API to extract text, tables, and figures with configurable OCR and output format. Use for structured document parsing needing per-element format or coordinate control; use lazy_load_data for large files.
What it does
The UpstageDocumentParseReader loads document files and detects elements such as text, tables, and figures using the Upstage Layout Analysis API, requiring an API key from the Upstage console. It supersedes the package's older UpstageLayoutAnalysisReader, which the source explicitly marks as deprecated.
The reader takes five optional construction parameters (the source's own header calls them "three", but five are actually documented). api_key accepts the API access token directly, or it can be left unset if UPSTAGE_API_KEY is already in the environment. ocr controls whether OCR runs before layout detection, with values auto (the default - OCR only for image input, while PDFs or other non-image documents have text and coordinates extracted directly without conversion to images) or force (always convert to images and run OCR first). output_format sets how each layout element is formatted in the output - text, html, or markdown, defaulting to html. Two more parameters shape the response: coordinates, a boolean (default true) for whether bounding-box coordinates are returned per layout element, and base64_encoding, a list naming which layout categories (from paragraph, table, figure, header, footer, caption, equation, heading1, list, index, footnote, chart) should be returned as base64-encoded strings - useful for cropping and reusing a specific element, such as extracting base64 images of every table in a document by passing ["table"].
load_data, extended from BaseReader, is the main entry point and requires a file_path - a single string or pathlib.Path, or a list of them. A lazy_load_data variant mirrors load_data but is built for efficiency with large files, loading page by page rather than all at once.
When to use - and when NOT to
Use it when you need structured extraction of text, tables, and figures from documents - especially when you need per-element output format control, bounding-box coordinates, or base64-encoded crops of specific elements like tables or figures. Use lazy_load_data specifically for large files where loading everything into memory at once would be wasteful. Use ocr=force when a PDF's embedded text layer is unreliable and you need OCR run regardless of document type. Do not use the deprecated UpstageLayoutAnalysisReader in new code - UpstageDocumentParseReader is the current, supported reader for this integration.
Capabilities
load_data/lazy_load_data parse documents into layout-detected elements (text, tables, figures) with configurable OCR mode, output format (text/html/markdown), coordinate return, and base64 encoding of specific layout categories.
How to install
pip install llama-index-readers-upstage
Requires an Upstage API key from the Upstage console, either passed directly or set as UPSTAGE_API_KEY.
Who it's for
Developers who need structured, layout-aware document parsing - text, tables, and figures with precise formatting and coordinate control - for LlamaIndex ingestion, including at scale via lazy loading for large files.
Source README
UpstageDocumentParseReader
UpstageDocumentParseReader
pip install llama-index-readers-upstage
This reader loads document files and detects elements such as text, tables, and figures using the Upstage Layout Analysis API. Users wishing to utilize this reader must obtain an API key from the Upstage console.
Construction
The UpstageDocumentParseReader is equipped with the following three optional parameters during instantiation:
api_key: This parameter is designed to accept a string that serves as an API access token. If the API key has already been registered in the environment variableUPSTAGE_API_KEY, there is no necessity to input it again during the reader's configuration.ocr: A string value indicating whether to perform OCR inference on the document before layout detection. The possible value is one ofautoandforce. The default isautowhich means that OCR is performed for image input only. When this option is set toautofor PDF or non-image documents, the engine directly extracts text and coordinates from the document without converting it to images. Otherwise, the engine converts the input file to images and performs OCR inference before layout detection.output_format: A list of string value indicating in which each layout element output is formatted. Possible values aretext,html, andmarkdown. The default value is"html"coordinates: A boolean value indicating whether to return coordinates of bounding boxes of each layout element. The default istruebase64_encoding: A list of string value indicating which layout category should be provided as base64 encoded string. Categories are includeparagraph,table,figure,header,footer,caption,equation,heading1,list,index,footnote,chart. This feature is useful when user wants to crop the layout element from the original document image and store and use it for their own purpose. For example, users can extract image base64 encoding of all tables of the input document with["table"]. All layout categories can be specified.
load_data
The load_data function, encompassed within the UpstageDocumentParseReader, extends from the BaseReader class. The lazy_load_data function mirrors the functionalities of the load_data function but with an enhanced focus on efficiency and lazy loading, making it particularly suitable for handling large files. Utilizing this function effectively necessitates a thorough understanding of its parameters and their respective expected inputs to harness its full potential:
file_path(required): This critical parameter accepts either a single string orpathlib.Pathobject, or a list comprising multiple of these elements, representing the path(s) to the file(s) intended for loading. Proper accessibility and precise specification of these path(s) are essential to ensure smooth operation.
Usage
Here's an example usage of the UpstageDocumentParseReader.
import os
os.environ["UPSTAGE_API_KEY"] = "YOUR_API_KEY"
from llama_index.readers.upstage import UpstageDocumentParseReader
file_path = "/PATH/TO/YOUR/FILE.pdf"
reader = UpstageDocumentParseReader()
### For improved memory efficiency, consider using the lazy_load_data method to load documents page by page.
docs = reader.load_data(file_path=file_path)
for doc in docs[:3]:
print(doc)
UpstageLayoutAnalysisReader (deprecated)
pip install llama-index-readers-upstage
This reader loads document files and detects elements such as text, tables, and figures using the Upstage Layout Analysis API. Users wishing to utilize this reader must obtain an API key from the Upstage console.
Construction
The UpstageLayoutAnalysisReader is equipped with the following three optional parameters during instantiation:
api_key: This parameter is designed to accept a string that serves a
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.