使用 Upstage Document Classification API 将文档归类至用户自定义类别,并支持多页 PDF 文档拆分。适用于...
Classify documents into user-defined categories with confidence scores. Also supports Document Split for separating multi-document PDFs into individual documents.
python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["UPSTAGE_API_KEY"],
base_url="https://api.upstage.ai/v1/document-classification"
)
response = client.chat.completions.create(
model="document-classify",
messages=[{
"role": "user",
"content": [{"type": "image_url", "image_url": {"url": "https://example.com/document.pdf"}}]
}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "document-classify",
"schema": {
"type": "string",
"oneOf": [
{"const": "invoice", "description": "Commercial invoice with itemized charges"},
{"const": "receipt", "description": "Payment receipt"},
{"const": "contract", "description": "Legal contract or agreement"},
{"const": "resume", "description": "Personal resume or CV"}
]
}
}
}
)
print(response.choices[0].message.content)
API Key: Always use os.environ["UPSTAGE_API_KEY"]. Get your key at console.upstage.ai.
text
POST https://api.upstage.ai/v1/document-classification
OpenAI SDK compatible — set base_url to https://api.upstage.ai/v1/document-classification.
ParameterTypeRequiredDescriptionmodelstringYesdocument-classify or document-classify-nightly``messagesarrayYesSingle user message with image_url``response_formatobjectYesJSON Schema defining classification categoriessplitbooleanNoEnable multi-document splitting (default: false)split_criteriaarrayNoAdditional splitting criteria (used with split=true)
Categories are defined using oneOf with const values. The root schema type must be "string", not "object".
json
{
"type": "json_schema",
"json_schema": {
"name": "document-classify",
"schema": {
"type": "string",
"oneOf": [
{"const": "invoice", "description": "Commercial invoice"},
{"const": "receipt", "description": "Payment receipt"},
{"const": "other", "description": "Other document type"}
]
}
}
}
Important: Using
enumorobject-based schemas will return a 400 error. The Classification API requiresoneOfwithconst/descriptionpairs.
json
{
"choices": [{
"message": {
"content": "invoice",
"tool_calls": [{
"function": {
"arguments": {
"document_type": {"_value": "invoice", "confidence_score": 0.99},
"pages": [1, 2]
}
}
}]
}
}]
}
content: classified category nametool_calls.function.arguments.document_type._value: classified valuetool_calls.function.arguments.document_type.confidence_score: 0.0–1.0tool_calls.function.arguments.pages: page range (most useful with split mode)For PDFs containing multiple document types, set extra_body={"split": True} to separate them into groups. Each group is returned as a separate choices entry. See references/document-split.md for the full split workflow with optional split_criteria.
<system-temp>/<input-stem>.classified.json (e.g., /tmp/contract.classified.json).<system-temp>/<input-stem>.split/ with one file per detected document (e.g., page-001.invoice.pdf)."other" in your categories to handle unclassified documents.split is useful as the first step in a document processing pipeline for scanned mixed-document files.upstage-information-extraction.confidence_score to flag low-confidence documents for manual review.FileContentreferences/document-split.mdDocument split (basic + with criteria), curl example