agentic-doc

repository·main·Indexed 25 days ago

https://github.com/landing-ai/agentic-doc

A Python library (v0.3.3) that wraps the VisionAgent document extraction REST API to extract structured, hierarchical JSON data from visually complex documents such as PDFs, images, and charts. It supports batch processing, Pydantic-based structured field extraction, and connectors for Google Drive, Amazon S3, local directories, and URLs. Note: This library is now legacy; users are encouraged to migrate to the landingai-ade library.

Tokens
8.1K
Snippets
15
Records
50
Agent score
77%

What's inside agentic-doc

  1. Understand error handling and retry mechanisms

    main

    The library automatically handles intermittent HTTP errors and rate limits using an exponential backoff strategy with jitter.

    Retried HTTP Status Codes:

    • 408 (Request Timeout)
    • 429 (Too Many Requests)
    • 502 (Bad Gateway)
    • 503 (Service Unavailable)
    • 504 (Gateway Timeout)

    Retry Logic Details:

    • Initial Wait: 1 second, increasing exponentially.
    • Jitter: Includes a random jitter of up to 10 seconds to prevent thundering herd problems.
    • Limits: Retries stop after max_retries attempts or when the wait time reaches max_retry_wait_time. Exceeding these limits raises an exception.

    Parsing Errors: If an unrecoverable error occurs during parsing, the resulting object for the affected page(s) will include an errors field containing the error_message, error_code, and the page number.

  2. Configure the VISION_AGENT_API_KEY environment variable

    main

    The library requires a LandingAI agentic AI API key. Set this key as an environment variable or in a .env file using the name VISION_AGENT_API_KEY.

    export VISION_AGENT_API_KEY=<your-api-key>
  3. How to override global settings (Deprecated)

    main

    The library provides a global settings object in agentic_doc.config to support legacy code that modifies settings directly.

    Warning: This approach is deprecated. You should instead pass a ParseConfig instance to the parse function.

    When you modify agentic_doc.config.settings, it uses a SettingsOverrides object to capture changes, which are then merged into the actual Settings instance when get_settings() is called.

  4. How document connectors work

    main

    Document connectors provide a unified interface for interacting with different storage backends (Local, Google Drive, S3, and URLs). All connectors inherit from BaseConnector and implement three core methods:

    1. list_files(path, pattern): Returns a list of file identifiers (e.g., paths, IDs, or URLs).
    2. download_file(file_id, local_path): Downloads a specific file to a local path. If local_path is not provided, the connector typically saves the file to a temporary directory.
    3. get_file_info(file_id): Retrieves metadata (like size or modification time) for a specific file.

    To instantiate a connector, use the create_connector factory function with an appropriate ConnectorConfig object.

  5. Configure parsing behavior with ParseConfig

    main

    When calling _send_parsing_request, you can pass a ParseConfig object to customize how the document is processed. The following parameters can be controlled via the config object:

    • figure_captioning_type: Controls the detail of figure captioning. Defaults to FigureCaptioningType.verbose if not specified.
    • figure_captioning_prompt: A custom prompt used for figure captioning.
    • split: Determines how the document is split. Defaults to SplitType.full if not specified.
    • enable_rotation_detection: A boolean to enable or disable rotation detection.
    • api_key: An optional override for the API key. If not provided in the config, the library uses the vision_agent_api_key from your environment settings.
  6. Configure retry logging style

    main

    The RETRY_LOGGING_STYLE setting determines how the library reports retry attempts:

    • log_msg (Default): Logs each retry attempt as a separate log message.
    • inline_block: Prints a yellow progress block () on the same line for each attempt. Useful for tracking progress without verbose logs.
    • none: Disables retry logging entirely.
  7. Understand the Chunk and ChunkGrounding data structures

    main

    A Chunk represents a segment of extracted content. It includes the text, its type, a unique ID, and grounding information which maps the text to specific locations in the document.

    ChunkGrounding provides the page number and a ChunkGroundingBox (coordinates in [left, top, right, bottom] format). Note that image_path in ChunkGrounding is None by default as it is not provided by the server API.

  8. Use Connectors to parse documents from external sources

    main

    The parse() function supports BaseConnector instances or ConnectorConfig objects. This allows you to trigger parsing workflows directly from external storage systems (e.g., S3, Google Drive) managed by a connector.

    When using a connector:

    • You can specify connector_path to search within a specific directory in the connector.
    • You can specify connector_pattern to filter files (e.g., using glob patterns).
    • The library handles downloading the files to temporary local paths before parsing.
  9. Configure parallelism and throughput via environment variables

    main

    You can optimize processing speed and manage API rate limits by configuring the following settings via environment variables or a .env file:

    VariableDescriptionDefault
    BATCH_SIZENumber of files to process in parallel4
    MAX_WORKERSThreads used to process parts of each file in parallel5
    MAX_RETRIESMaximum retry attempts for failed intermittent requests100
    MAX_RETRY_WAIT_TIMEMaximum wait time in seconds for each retry60
    RETRY_LOGGING_STYLEControls how retry attempts are loggedlog_msg

    Parallelism Calculation: Total parallelism = BATCH_SIZE × MAX_WORKERS. Note: The maximum allowed parallelism is 100.

    • Increase MAX_WORKERS to speed up processing of large individual files.
    • Increase BATCH_SIZE to improve throughput when processing multiple files.
    • If you hit rate limits frequently, decrease these values to match your API limits.
    # Example .env file
    BATCH_SIZE=4
    MAX_WORKERS=2
    MAX_RETRIES=80
    MAX_RETRY_WAIT_TIME=30
    RETRY_LOGGING_STYLE=log_msg
  10. Configure structured field extraction

    main

    To extract specific fields from a document, you must provide either a Pydantic model or a JSON schema to the parsing functions. You cannot use both simultaneously.

    Options:

    1. extraction_model: A Pydantic BaseModel class. The extracted data will be validated against this model and returned as a typed object within the ParsedDocument.
    2. extraction_schema: A standard JSON schema dictionary. The extracted data will be validated against this schema.

    Constraint: If extraction_model is used, the library also attempts to validate extraction_metadata using a metadata model derived from your provided model.