Skip to main content
This article is about the legacy agentic-doc library. Use the landingai-ade library for all new projects.

1. Set the API Key as an Environment Variable

Get your API key and store it in a .env file (or set it as an environment variable). For more details, go to API Key.
export VISION_AGENT_API_KEY=<your-api-key>

2. Install the Library

pip install agentic-doc

3. Extract Data from a Local File and Return Results as Objects

Run this script to parse a file on a local directory and return the results as Markdown and JSON objects.
from agentic_doc.parse import parse

# Parse a local file
result = parse("path/to/file.pdf")

# Get the extracted data as markdown
print("Extracted Markdown:")
print(result[0].markdown)

# Get the extracted data as structured chunks of content in a JSON schema
print("Extracted Chunks:")
print(result[0].chunks)
parses the document and prints the Markdown and JSON outputs for the document in the console. Because the extracted data is returned as objects, you can write scripts that take that output and immediately process it. For example, you could create a web app that extracts structured data from a PDF and immediately renders it in the UI.

4. Extract Data from a Local File and Save Results

In the previous example, you parsed a file and immediately output the results in the console. Now, run a different script to parse the same file and save the results as a JSON file in a local directory. Run this script to parse a local file and save the results as a JSON file at the specified directory.
from agentic_doc.parse import parse

# Parse a local PDF and save results to directory
result = parse("path/to/file.pdf", result_save_dir="path/to/save/results")

# Print the file path to the JSON file
print(f"Final result: {result[0].result_path}")
The API parses the document and saves the results in the directory you specified. Because the extracted data is saved, you can later audit it or build an app that references it. For example, you could build out a document processing system that parses documents nightly and saves the output as JSON files for auditors to inspect the next day.

5. Next Steps

Now that you know how to parse documents, learn about the additional parameters in Parsing Basics so that you can build out custom scripts for your use case.

Set Your API Key When Using the Legacy Library

There are a few methods you can use to set the API key when using the agentic-doc library, including:

Method 1: Set the API Key as an Environment Variable

Store your API key as an environment variable.
  1. Set the API key as an environment variable: Linux/macOS: One way to do this is to add the API key to your .zshrc file. (If updating this file, run source ~/.zshrc afterward.)
    export VISION_AGENT_API_KEY=your_api_key_here
    
    Windows (Command Prompt):
    set VISION_AGENT_API_KEY=your_api_key_here
    
    Windows (PowerShell):
    $env:VISION_AGENT_API_KEY="your_api_key_here"
    
  2. You do not need to add any additional code to your script to use the API key.

Method 2: Store API Key in a .env File

Use a .env file to store your API key.
  1. Create a .env file in your project root and store your API key in it:
    VISION_AGENT_API_KEY=your_api_key_here
    
  2. You do not need to add any additional code to your script to use the API key.

Method 3: Set API Key Using the ParseConfig Class

This method is only available in agentic-doc v0.3.0 and later.
You can set the API key using the ParseConfig class. This approach is also useful in environments where setting environment variables is difficult or not persistent (notebooks, scripts run via cron, etc.). For more information, go to Pass Settings with ParseConfig. For example:
from agentic_doc.config import ParseConfig
config = ParseConfig(api_key="your-api-key-here") 

Method 4: Set API Key Using the Settings Class

This method is only available in agentic-doc v0.2.10 and earlier.
If you prefer not to use environment variables or want more control, you can set the API key using the Settings class. This approach is also useful in environments where setting environment variables is difficult or not persistent (notebooks, scripts run via cron, etc.). For example:
from agentic_doc.config import settings
settings.vision_agent_api_key = "your_api_key_here"

API Key Precedence

If the API key is set using multiple methods, the agentic-doc library uses the following order of precedence (highest to lowest):
  • Environment variable
  • .env file
  • Library configuration (in code)
The first available key found in this order will be used. All other values will be ignored.

Troubleshoot API Key Issues with Legacy Library

Error: Illegal header value b’Basic ’

The following error occurs when running agentic-doc v0.2.10 and earlier and the API key is missing, not set correctly, or being read too late in your code.
[error  ] Error parsing document 'File name: file_namePage: #:#]' due to: Illegal header value b'Basic ' [agentic_doc.parse] (parse.py:246)
Solutions We recommend upgrading to agentic-doc v0.3.0 or later for updated API key management options. If you choose not to upgrade, try the following tips to resolve the error:
  • Set the API key before importing the library. For example, in the snippet below, the API key is set in the VISION_AGENT_API_KEY environment variable before importing anything from the library.
    import os
    # Set environment variable first
    os.environ["VISION_AGENT_API_KEY"] = "your_api_key_here"
    
    # Now import the library
    from agentic_doc.parse import parse_documents
    
  • Check that the environment variable is correct:
    echo $VISION_AGENT_API_KEY  # Linux/macOS
    echo %VISION_AGENT_API_KEY%  # Windows CMD
    $Env:VISION_AGENT_API_KEY # Windows PowerShell
    
  • Set the API key using configuration settings: Set the API key using the ParseConfig class (in v0.3.0 and later) or the Settings class (in agentic-doc v0.2.10 and earlier).