Classify with a Saved Configuration
Create a reusable classify configuration of rules with the LlamaCloud SDKs or REST API, then reference it by configuration_id across multiple classify jobs instead of passing rules inline.
In this example, we’ll save a reusable classify configuration and use it across multiple jobs. Instead of passing inline rules every time, you create a configuration once and reference it by ID.
This is useful when you have a standard set of classification rules that you want to reuse across multiple files or integrate into an automated pipeline.
Install
Section titled “Install”pip install llama-cloud>=1.6npm install @llamaindex/llama-cloudgo get github.com/run-llama/llama-parse-goimplementation("ai.llamaindex:llama-cloud:1.3.0")go install github.com/run-llama/llama-parse-cli/cmd/llp@latestCreate a Saved Configuration
Section titled “Create a Saved Configuration”Create a classify configuration with your rules. This returns a configuration ID that you can reuse.
The SDKs resolve the project from your API key. The REST call needs an explicit project ID — find it in the URL when viewing a project in the UI.
import osfrom llama_cloud import LlamaCloud
client = LlamaCloud(api_key=os.environ["LLAMA_CLOUD_API_KEY"])
config = client.configurations.create( name="Invoice vs Receipt Classifier", parameters={ "product_type": "classify_v2", "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.", }, ], "mode": "FAST", },)
print(config.id)import LlamaCloud from "@llamaindex/llama-cloud";
const client = new LlamaCloud({ apiKey: process.env.LLAMA_CLOUD_API_KEY,});
const config = await client.configurations.create({ name: "Invoice vs Receipt Classifier", parameters: { product_type: "classify_v2", 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.", }, ], mode: "FAST", },});
console.log(config.id);package main
import ( "context" "fmt" "log"
llamacloud "github.com/run-llama/llama-parse-go")
func main() { ctx := context.Background() client := llamacloud.NewClient()
config, err := client.Configurations.New(ctx, llamacloud.ConfigurationNewParams{ ConfigurationCreate: llamacloud.ConfigurationCreateParam{ Name: "Invoice vs Receipt Classifier", Parameters: llamacloud.ConfigurationCreateParametersUnionParam{ OfClassifyV2: &llamacloud.ClassifyV2Parameters{ Rules: []llamacloud.ClassifyV2ParametersRule{ { 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.", }, }, Mode: llamacloud.ClassifyV2ParametersModeFast, }, }, }, }) if err != nil { log.Fatal(err) }
fmt.Println(config.ID)}import ai.llamaindex.llamacloud.client.LlamaCloudClient;import ai.llamaindex.llamacloud.client.okhttp.LlamaCloudOkHttpClient;import ai.llamaindex.llamacloud.models.configurations.ClassifyV2Parameters;import ai.llamaindex.llamacloud.models.configurations.ConfigurationCreate;import ai.llamaindex.llamacloud.models.configurations.ConfigurationResponse;
public class CreateClassifyConfiguration { public static void main(String[] args) throws Exception { LlamaCloudClient client = LlamaCloudOkHttpClient.fromEnv();
ConfigurationResponse config = client.configurations().create( ConfigurationCreate.builder() .name("Invoice vs Receipt Classifier") .parameters(ClassifyV2Parameters.builder() .addRule(ClassifyV2Parameters.Rule.builder() .type("invoice") .description("Documents that contain an invoice number, invoice date, bill-to section, and line items with totals.") .build()) .addRule(ClassifyV2Parameters.Rule.builder() .type("receipt") .description("Short purchase receipts, typically from POS systems, with merchant, items and total, often a single page.") .build()) .mode(ClassifyV2Parameters.Mode.FAST) .build()) .build());
System.out.println(config.id()); }}export LLAMA_CLOUD_API_KEY="llx-..."
CONFIG_ID=$(llp configurations create \ --name "Invoice vs Receipt Classifier" \ --parameters '{product_type: classify_v2, 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."}], mode: FAST}' \ | jq -r '.id')
echo "$CONFIG_ID"curl -X POST 'https://api.cloud.llamaindex.ai/api/v1/beta/configurations?project_id=YOUR_PROJECT_ID' \ -H 'accept: application/json' \ -H "Authorization: Bearer $LLAMA_CLOUD_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "name": "Invoice vs Receipt Classifier", "parameters": { "product_type": "classify_v2", "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." } ], "mode": "FAST" } }'The response includes an id field — this is your configuration_id:
{ "id": "cfg-11111111-2222-3333-4444-555555555555", "name": "Invoice vs Receipt Classifier", "product_type": "classify_v2", ...}Classify Using the Configuration ID
Section titled “Classify Using the Configuration ID”Now use the saved configuration to classify files — no need to pass rules inline.
import osimport timefrom llama_cloud import LlamaCloud
client = LlamaCloud(api_key=os.environ["LLAMA_CLOUD_API_KEY"])
# Upload a filefile_obj = client.files.create(file="/path/to/document.pdf", purpose="classify")
# Create a classify job using the saved configurationjob = client.classify.create( file_id=file_obj.id, configuration_id="cfg-11111111-2222-3333-4444-555555555555",)
# Poll until completestatus = client.classify.get(job.id)while status.status == "PENDING": time.sleep(2) status = client.classify.get(job.id)
# Print resultif status.result: print(f"Type: {status.result.type}") print(f"Confidence: {status.result.confidence}") print(f"Reasoning: {status.result.reasoning}")else: print(f"Classification failed: {status.error_message}")import LlamaCloud from "@llamaindex/llama-cloud";import fs from "fs";
const client = new LlamaCloud({ apiKey: process.env.LLAMA_CLOUD_API_KEY,});
// Upload a fileconst fileObj = await client.files.create({ file: fs.createReadStream("/path/to/document.pdf"), purpose: "classify",});
// Create a classify job using the saved configurationlet job = await client.classify.create({ file_id: fileObj.id, configuration_id: "cfg-11111111-2222-3333-4444-555555555555",});
// Poll until completewhile (job.status === "PENDING") { await new Promise((r) => setTimeout(r, 2000)); job = await client.classify.get(job.id);}
// Print resultif (job.result) { console.log(`Type: ${job.result.type}`); console.log(`Confidence: ${job.result.confidence}`); console.log(`Reasoning: ${job.result.reasoning}`);} else { console.log(`Classification failed: ${job.error_message}`);}package main
import ( "context" "fmt" "log" "os" "time"
llamacloud "github.com/run-llama/llama-parse-go")
func main() { ctx := context.Background() client := llamacloud.NewClient()
// Upload a file f, err := os.Open("/path/to/document.pdf") if err != nil { log.Fatal(err) } defer f.Close()
fileObj, err := client.Files.New(ctx, llamacloud.FileNewParams{ File: f, Purpose: "classify", }) if err != nil { log.Fatal(err) }
// Create a classify job using the saved configuration job, err := client.Classify.New(ctx, llamacloud.ClassifyNewParams{ ClassifyCreateRequest: llamacloud.ClassifyCreateRequestParam{ FileInput: llamacloud.String(fileObj.ID), ConfigurationID: llamacloud.String("cfg-11111111-2222-3333-4444-555555555555"), }, }) if err != nil { log.Fatal(err) }
// Poll until the job reaches a terminal state result, err := client.Classify.Get(ctx, job.ID, llamacloud.ClassifyGetParams{}) if err != nil { log.Fatal(err) } for result.Status == llamacloud.ClassifyGetResponseStatusPending || result.Status == llamacloud.ClassifyGetResponseStatusRunning { time.Sleep(2 * time.Second) result, err = client.Classify.Get(ctx, job.ID, llamacloud.ClassifyGetParams{}) if err != nil { log.Fatal(err) } }
if !result.JSON.Result.Valid() { fmt.Printf("Classification failed: %s\n", result.ErrorMessage) } else { fmt.Printf("Type: %s\n", result.Result.Type) fmt.Printf("Confidence: %v\n", result.Result.Confidence) fmt.Printf("Reasoning: %s\n", result.Result.Reasoning) }}import ai.llamaindex.llamacloud.client.LlamaCloudClient;import ai.llamaindex.llamacloud.client.okhttp.LlamaCloudOkHttpClient;import ai.llamaindex.llamacloud.models.classify.ClassifyCreateRequest;import ai.llamaindex.llamacloud.models.classify.ClassifyCreateResponse;import ai.llamaindex.llamacloud.models.classify.ClassifyGetResponse;import ai.llamaindex.llamacloud.models.files.FileCreateParams;import ai.llamaindex.llamacloud.models.files.FileCreateResponse;import java.nio.file.Paths;
public class ClassifyWithSavedConfig { public static void main(String[] args) throws Exception { LlamaCloudClient client = LlamaCloudOkHttpClient.fromEnv();
// Upload a file FileCreateResponse fileObj = client.files().create( FileCreateParams.builder() .file(Paths.get("/path/to/document.pdf")) .purpose("classify") .build());
// Create a classify job using the saved configuration ClassifyCreateResponse job = client.classify().create( ClassifyCreateRequest.builder() .fileInput(fileObj.id()) .configurationId("cfg-11111111-2222-3333-4444-555555555555") .build());
// Poll until the job reaches a terminal state ClassifyGetResponse result = client.classify().get(job.id()); while (result.status().equals(ClassifyGetResponse.Status.PENDING) || result.status().equals(ClassifyGetResponse.Status.RUNNING)) { Thread.sleep(2000); result = client.classify().get(job.id()); }
if (!result.result().isPresent()) { System.out.println("Classification failed: " + result.errorMessage().orElse("")); } else { System.out.println("Type: " + result.result().get().type().orElse("")); System.out.println("Confidence: " + result.result().get().confidence()); System.out.println("Reasoning: " + result.result().get().reasoning()); } }}export LLAMA_CLOUD_API_KEY="llx-..."
# Upload a fileFILE_ID=$(llp files create --file /path/to/document.pdf --purpose classify | jq -r '.id')
# Create a classify job using the saved configurationJOB_ID=$(llp classify create \ --file-input "$FILE_ID" \ --configuration-id "cfg-11111111-2222-3333-4444-555555555555" \ | jq -r '.id')
# Poll until the job reaches a terminal statewhile true; do RESULT=$(llp classify get "$JOB_ID") STATUS=$(echo "$RESULT" | jq -r '.status') [ "$STATUS" = "PENDING" ] || [ "$STATUS" = "RUNNING" ] || break sleep 2done
echo "$RESULT" | jq -r '.result.type, .result.confidence, .result.reasoning'Update a Configuration
Section titled “Update a Configuration”You can update the rules or mode of an existing configuration at any time:
import osfrom llama_cloud import LlamaCloud
client = LlamaCloud(api_key=os.environ["LLAMA_CLOUD_API_KEY"])
config = client.configurations.update( "cfg-11111111-2222-3333-4444-555555555555", parameters={ "product_type": "classify_v2", "rules": [ { "type": "invoice", "description": "Documents containing invoice numbers, dates, and itemized totals.", }, { "type": "receipt", "description": "POS receipts with merchant name, items, and total.", }, { "type": "purchase_order", "description": "Purchase orders with PO numbers, vendor details, and requested items.", }, ], "mode": "FAST", },)
print(config.version)import LlamaCloud from "@llamaindex/llama-cloud";
const client = new LlamaCloud({ apiKey: process.env.LLAMA_CLOUD_API_KEY,});
const config = await client.configurations.update( "cfg-11111111-2222-3333-4444-555555555555", { parameters: { product_type: "classify_v2", rules: [ { type: "invoice", description: "Documents containing invoice numbers, dates, and itemized totals.", }, { type: "receipt", description: "POS receipts with merchant name, items, and total.", }, { type: "purchase_order", description: "Purchase orders with PO numbers, vendor details, and requested items.", }, ], mode: "FAST", }, },);
console.log(config.version);package main
import ( "context" "fmt" "log"
llamacloud "github.com/run-llama/llama-parse-go")
func main() { ctx := context.Background() client := llamacloud.NewClient()
config, err := client.Configurations.Update(ctx, "cfg-11111111-2222-3333-4444-555555555555", llamacloud.ConfigurationUpdateParams{ Parameters: llamacloud.ConfigurationUpdateParamsParametersUnion{ OfClassifyV2: &llamacloud.ClassifyV2Parameters{ Rules: []llamacloud.ClassifyV2ParametersRule{ { Type: "invoice", Description: "Documents containing invoice numbers, dates, and itemized totals.", }, { Type: "receipt", Description: "POS receipts with merchant name, items, and total.", }, { Type: "purchase_order", Description: "Purchase orders with PO numbers, vendor details, and requested items.", }, }, Mode: llamacloud.ClassifyV2ParametersModeFast, }, }, }) if err != nil { log.Fatal(err) }
fmt.Println(config.Version)}import ai.llamaindex.llamacloud.client.LlamaCloudClient;import ai.llamaindex.llamacloud.client.okhttp.LlamaCloudOkHttpClient;import ai.llamaindex.llamacloud.models.configurations.ClassifyV2Parameters;import ai.llamaindex.llamacloud.models.configurations.ConfigurationResponse;import ai.llamaindex.llamacloud.models.configurations.ConfigurationUpdateParams;
public class UpdateClassifyConfiguration { public static void main(String[] args) throws Exception { LlamaCloudClient client = LlamaCloudOkHttpClient.fromEnv();
ConfigurationResponse config = client.configurations().update( ConfigurationUpdateParams.builder() .configId("cfg-11111111-2222-3333-4444-555555555555") .parameters(ClassifyV2Parameters.builder() .addRule(ClassifyV2Parameters.Rule.builder() .type("invoice") .description("Documents containing invoice numbers, dates, and itemized totals.") .build()) .addRule(ClassifyV2Parameters.Rule.builder() .type("receipt") .description("POS receipts with merchant name, items, and total.") .build()) .addRule(ClassifyV2Parameters.Rule.builder() .type("purchase_order") .description("Purchase orders with PO numbers, vendor details, and requested items.") .build()) .mode(ClassifyV2Parameters.Mode.FAST) .build()) .build());
System.out.println(config.version()); }}export LLAMA_CLOUD_API_KEY="llx-..."
llp configurations update \ --config-id "cfg-11111111-2222-3333-4444-555555555555" \ --parameters '{product_type: classify_v2, rules: [{type: invoice, description: "Documents containing invoice numbers, dates, and itemized totals."}, {type: receipt, description: "POS receipts with merchant name, items, and total."}, {type: purchase_order, description: "Purchase orders with PO numbers, vendor details, and requested items."}], mode: FAST}' \ | jq -r '.version'curl -X PUT 'https://api.cloud.llamaindex.ai/api/v1/beta/configurations/cfg-11111111-2222-3333-4444-555555555555?project_id=YOUR_PROJECT_ID' \ -H 'accept: application/json' \ -H "Authorization: Bearer $LLAMA_CLOUD_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "parameters": { "product_type": "classify_v2", "rules": [ { "type": "invoice", "description": "Documents containing invoice numbers, dates, and itemized totals." }, { "type": "receipt", "description": "POS receipts with merchant name, items, and total." }, { "type": "purchase_order", "description": "Purchase orders with PO numbers, vendor details, and requested items." } ], "mode": "FAST" } }'Future classify jobs using this configuration_id will automatically use the updated rules.
- Use
configuration_idinstead of inlineconfigurationwhen you want to reuse the same rules across multiple jobs. - Configurations are scoped to your project — they are not shared across projects.
- You can also create and manage configurations from the LlamaCloud UI.