Llama Cloud Python SDK

2.3.0 · active · verified Thu Apr 09

The `llama-cloud` Python SDK is the official library for interacting with the Llama Cloud API, providing convenient access to services like LlamaParse for agentic OCR and document processing, structured data extraction, document classification, and RAG ingestion. The library offers both synchronous and asynchronous clients and is actively maintained with frequent releases, currently at version 2.3.0.

Warnings

Install

Imports

Quickstart

Initializes the Llama Cloud client using an API key from environment variables and demonstrates how to create a document parsing job. For actual usage, replace `dummy_file_id` with an ID obtained from uploading a file via `client.files.create()`.

import os
from llama_cloud import LlamaCloud
from pathlib import Path

# Ensure LLAMA_CLOUD_API_KEY is set in your environment variables
# e.g., os.environ['LLAMA_CLOUD_API_KEY'] = 'llx-...' or using a .env file
client = LlamaCloud(
    api_key=os.environ.get('LLAMA_CLOUD_API_KEY', '')
)

# Example: Upload a dummy file and create a parsing job
# Replace with a real file path for actual usage
# file_path = Path("path/to/your/document.pdf")
# with open(file_path, "rb") as f:
#     file_obj = client.files.create(
#         file=(file_path.name, f.read(), "application/pdf"),
#         purpose="parse",
#     )

# For demonstration, simulate a file_id to create a parsing job
# In a real scenario, file_id would come from client.files.create() or an existing file.
dummy_file_id = "your-example-file-id" # Replace with actual file_id if uploading

if not dummy_file_id:
    print("Please provide a valid file_id or upload a file first.")
else:
    try:
        job = client.parsing.create(
            tier="agentic", # or "fast", "cost_effective"
            version="latest",
            file_id=dummy_file_id,
            # For example, to get markdown output:
            output_options={
                "markdown": {
                    "tables": {
                        "output_tables_as_markdown": True
                    }
                }
            },
            expand=["markdown"]
        )
        print(f"Created parsing job with ID: {job.id}")
        # You would typically poll the job status and retrieve results here
        # For example, using client.parsing.get_job_result(job.id)
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →