Skip to content
Guide
Classify

Getting Started

Use the client SDK to classify documents with natural-language rules, including file uploads, job polling, and reading results.

This guide shows how to classify documents using the SDK. You will:

  • Create classification rules
  • Upload files
  • Submit a classify job
  • Read predictions (type, confidence, reasoning)

The SDK is available in llama-parse-py, llama-parse-ts, llama-parse-go, and llama-parse-java, plus the llama-parse-cli command-line tool.

First, get an API key and record it for safe keeping.

You can set this as an environment variable LLAMA_CLOUD_API_KEY or pass it directly to the SDK at runtime.

Then, install dependencies:

Terminal window
pip install llama-cloud>=2.8

Using the classify API consists of a few main steps:

  1. Upload a file and get its ID
  2. Create a classify job with your rules, passing that ID as file_input
  3. Wait for the job to finish
  4. Read the result

file_input also accepts a parse job ID, so a document you have already parsed can be classified without uploading or re-parsing it.

The SDK provides a convenience method that handles all of these steps in one call:

import os
from llama_cloud import LlamaCloud, AsyncLlamaCloud
# For async usage, use `AsyncLlamaCloud()`
client = LlamaCloud(api_key=os.environ["LLAMA_CLOUD_API_KEY"])
# Upload a file
file_obj = client.files.create(file="/path/to/doc1.pdf", purpose="classify")
# Classify and wait for completion
job = client.classify.run(
file_input=file_obj.id,
configuration={
"rules": [
{
"type": "invoice",
"description": "Documents that contain an invoice number, invoice date, bill-to section, and line items with totals.",
},
{
"type": "receipt",
"description": "Short purchase receipts, typically from POS systems, with merchant, items and total, often a single page.",
},
],
"parsing_configuration": {
"lang": "en",
"max_pages": 5, # optional, parse at most 5 pages
# "target_pages": "1,3", # optional, parse only specific pages (1-based)
},
},
)
# `run` raises PollingError if the job fails, so reaching here means it succeeded
print(f"Classified type: {job.result.type}")
print(f"Confidence: {job.result.confidence}")
print(f"Reasoning: {job.result.reasoning}")

You can also run each step individually if you need more control:

import os
import time
from llama_cloud import LlamaCloud
client = LlamaCloud(api_key=os.environ["LLAMA_CLOUD_API_KEY"])
# Upload a file
file_obj = client.files.create(file="/path/to/doc1.pdf", purpose="classify")
# Create a classify job
job = client.classify.create(
file_input=file_obj.id,
configuration={
"rules": [
{
"type": "invoice",
"description": "Documents that contain an invoice number, invoice date, bill-to section, and line items with totals.",
},
{
"type": "receipt",
"description": "Short purchase receipts, typically from POS systems, with merchant, items and total, often a single page.",
},
],
},
)
# Poll until the job reaches a terminal state
result = client.classify.get(job.id)
while result.status in ("PENDING", "RUNNING"):
time.sleep(2)
result = client.classify.get(job.id)
# `get` returns the result inline once the job is COMPLETED
if result.result is None:
print(f"Classification failed: {result.error_message}")
else:
print(f"Classified type: {result.result.type}")
print(f"Confidence: {result.result.confidence}")
print(f"Reasoning: {result.result.reasoning}")
  • Each rule requires a type (the label to assign) and a description (natural-language description of what content matches). Rule types must be unique within a configuration.
  • parsing_configuration is optional. Use lang, max_pages, or target_pages to control how documents are parsed before classification.
  • target_pages is a comma-separated string of 1-based page numbers or ranges (e.g., "1,3,5-7").
  • Each job classifies a single document. To classify several files, submit one job per file.
  • A failed or cancelled job has no result — check status before reading it, and use error_message for the reason.
  • Job statuses: PENDING, RUNNING, COMPLETED, FAILED, CANCELLED.
  • Instead of passing configuration inline, you can pass a configuration_id to reuse a saved configuration.
  • Be specific about content features that distinguish the type.
  • Include key fields the document usually contains (e.g., invoice number, total amount).
  • Add multiple rules when needed to cover distinct patterns.
  • Start simple, test on a small set, then refine.
Note for AI agents: this documentation is built for programmatic access. - Overview of all docs: https://developers.llamaindex.ai/llms.txt - Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md - Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters. - A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/for-agents/mcp/ - Other LlamaIndex tooling for agents — the LlamaParse Platform MCP server, agent skills and plugins, and the n8n node — is mapped at https://developers.llamaindex.ai/for-agents/