Skip to content

REST API

First, upload a PDF using the Files API.

Install the Python SDK if you haven’t already:

Terminal window
pip install llama-cloud>=1.0

Then upload your file:

from llama_cloud import LlamaCloud
client = LlamaCloud(api_key="LLAMA_CLOUD_API_KEY")
file_obj = client.files.create(file="path/to/your/file.pdf", purpose="split")
print(file_obj.id)

Save the returned id as your FILE_ID.

Create a split job with your file ID and category definitions:

job = await client.beta.split.create(
categories=[
{
"name": "invoice",
"description": "A commercial document requesting payment for goods or services, typically containing line items, totals, and payment terms"
},
{
"name": "contract",
"description": "A legal agreement between parties outlining terms, conditions, obligations, and signatures"
}
],
document_input={"type": "file_id", "value": file_id},
)

The response includes the job ID and initial status:

{
"id": "spl-abc123...",
"status": "pending"
}

Jobs are processed asynchronously. Poll the status until it reaches completed or failed:

completed_job = await client.beta.split.wait_for_completion(
job.id,
polling_interval=1.0,
verbose=True,
)

When the job completes successfully, the response includes the segmentation results:

{
"id": "spl-abc123...",
"status": "completed",
"result": {
"segments": [
{
"category": "invoice",
"pages": [1, 2, 3],
"confidence_category": "high"
},
{
"category": "contract",
"pages": [4, 5, 6, 7, 8],
"confidence_category": "high"
}
]
}
}

Each segment contains:

  • category: The assigned category name
  • pages: Array of page numbers (1-indexed) belonging to this segment
  • confidence_category: Confidence level (high, medium, or low)

By default, all pages must be assigned to one of your defined categories. To allow pages that don’t match any category to be grouped as uncategorized, use the splitting_strategy option:

job = await client.beta.split.create(
categories=[
{
"name": "invoice",
"description": "A commercial document requesting payment for goods or services"
}
],
document_input={"type": "file_id", "value": file_id},
splitting_strategy={"allow_uncategorized": True}
)

With this option, pages that don’t match invoice will be grouped into segments with category: "uncategorized".

If you’re working within a specific project, include the project_id query parameter:

job = await client.beta.split.create(
...,
project_id="YOUR_PROJECT_ID"
)

This is a subset of the available endpoints to help you get started.

You can see all available endpoints in our full API documentation.