Abstract / Overview
LlamaIndex’s LlamaClassify enables developers to automatically categorize unstructured documents (PDFs, scans, contracts, invoices, receipts, etc.) into custom-defined types using natural-language rules.
This developer-focused tutorial explains how to classify contract types (e.g., affiliate vs. co-branding agreements) using the LlamaCloud Python SDK. It covers authentication, rule definition, asynchronous classification, and result interpretation.
You’ll learn how to:
Define flexible, natural-language classification rules
Upload and classify files through LlamaCloud
Retrieve model-generated predictions with confidence and reasoning
Integrate classification into document pipelines for parsing, extraction, or indexing
Conceptual Background

What Is LlamaClassify?
LlamaClassify is part of the LlamaCloud Services SDK, providing automatic document categorization with natural-language reasoning. You define types and textual rules that describe what distinguishes each type. The model then classifies each document accordingly.
Each classification job returns:
type: predicted document typeconfidence: numeric probability (0–1.0)reasoning: textual explanation of the model’s decision
When to Use LlamaClassify
Pre-Extraction Step: Route documents to schema-specific extraction agents (e.g., invoices vs. receipts).
Pre-Parsing Step: Apply tailored LlamaParse configurations based on type (e.g., parse settings differ for contracts vs. receipts).
Pre-Indexing Step: Send labeled documents to the right LlamaCloud indices for improved search and retrieval.
Back-office automation: Route incoming files (e.g., receipts, POs, bank statements) automatically to workflow queues.
Dataset curation: Generate labeled subsets for training specialized document understanding models.
Step-by-Step Developer Walkthrough
1. Environment Setup
Install dependencies and configure your API key.
pip install llama-cloud-services python-dotenvCreate a .env file to store your Llama Cloud API key securely:
LLAMA_CLOUD_API_KEY=llx-your-api-keyAlternatively, you can use environment variables or getpass() for interactive input.
2. Initialize the Client
import os
from dotenv import load_dotenv
from llama_cloud.client import AsyncLlamaCloud
from llama_cloud_services.beta.classifier.client import ClassifyClient
load_dotenv()
client = AsyncLlamaCloud(token=os.environ["LLAMA_CLOUD_API_KEY"])
project_id = "your-project-id"
organization_id = "your-organization-id"
classifier = ClassifyClient(client, project_id=project_id, organization_id=organization_id)This initializes an asynchronous LlamaCloud client and a ClassifyClient wrapper that handles uploads, job creation, polling, and retrieval.
3. Define Classification Rules
Classification in LlamaClassify relies on rules, each describing a type using natural language. The model uses these rules to evaluate which type best matches the document content.
Example rules for two contract categories:
from llama_cloud.types import ClassifierRule
rules = [
ClassifierRule(
type="affiliate_agreements",
description="Contracts that outline an affiliate partnership where one party markets or sells another’s products in exchange for commission."
),
ClassifierRule(
type="co_branding",
description="Contracts that define a co-branding arrangement between two organizations sharing brand assets or marketing campaigns under both logos."
),
]Best Practices for Rule Writing
Use descriptive phrases about distinctive content.
Mention unique structural elements (e.g., “Affiliate Partner,” “royalty,” “joint marketing”).
Avoid vague phrasing like “looks like a contract.”
Refine rules iteratively by testing them on sample files.
4. Optional Parsing Configuration
You can instruct the classifier to limit parsing for efficiency or accuracy.
from llama_cloud.types import ClassifyParsingConfiguration, ParserLanguages
parsing = ClassifyParsingConfiguration(
lang=ParserLanguages.EN,
max_pages=5 # Limit to first 5 pages for speed
)5. Upload and Classify Contracts
Asynchronously classify one or more PDF contracts:
result = await classifier.aclassify_file_path(
rules=rules,
file_input_path="CybergyHoldingsInc_Affiliate_Agreement.pdf",
)

Join the conversation! Your thoughts help the community grow.