Skip to content

Classify SDK

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-cloud-py or llama-cloud-ts.

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>=1.0

Using the classify API consists of a few main steps

  1. Uploading a file and getting a file_id
  2. Creating a classify job
  3. Waiting for the job to finish
  4. Fetching the final result

The SDK will contain both a helper method to automate these steps:

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")
file_id = file_obj.id
# Upload and wait for completion
result = client.classifier.classify(
file_ids=[file_id],
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], # optional, parse only specific pages (1-indexed), can't be used with max_pages
},
mode="FAST", # or "MULTIMODAL"
)
# Print the classification results
for item in result.items:
assert item.result is not None
print(f"Classified type: {item.result.type}")
print(f"Confidence: {item.result.confidence}")
print(f"Reasoning: {item.result.reasoning}")

Or you can run each step individually:

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")
file_id = file_obj.id
# Upload to create a job
job = client.classifier.jobs.create(
file_ids=[file_id],
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], # optional, parse only specific pages (1-indexed), can't be used with max_pages
},
mode="FAST", # or "MULTIMODAL"
)
# Poll for job completion
status = client.classifier.jobs.get(classify_job_id=job.id)
while status.status == "PENDING":
time.sleep(2)
status = client.classifier.jobs.get(classify_job_id=job.id)
result = client.classifier.jobs.get_results(classify_job_id=job.id)
# Print the classification results
for item in result.items:
assert item.result is not None
print(f"Classified type: {item.result.type}")
print(f"Confidence: {item.result.confidence}")
print(f"Reasoning: {item.result.reasoning}")

Notes:

  • ClassifierRule requires a type and a descriptive description that the model can follow.
  • ClassifyParsingConfiguration is optional; set lang, max_pages, or target_pages to control parsing.
  • In cases of partial failure, some of the items may not have a result (i.e. results.items[*].result could be None).

LlamaClassify supports two modes:

ModeCredits per PageDescription
FAST1Text-based classification (default)
MULTIMODAL2Vision-based classification for visual documents

Use Multimodal mode when your documents contain images, charts, or complex layouts that are important for classification:

results = client.classifier.classify(
rules=rules,
files=["/path/to/visual-doc.pdf"],
mode="MULTIMODAL", # use vision model for classification
)
  • 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.