使用 Upstage 信息抽取API,通过自定义 JSON schema(同步/异步)或预置模型(如收据)从文档中提取指定的命名字段
Extract structured data from documents using custom JSON schemas. Also supports prebuilt models for receipts, invoices, and trade documents.
python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["UPSTAGE_API_KEY"],
base_url="https://api.upstage.ai/v1/information-extraction"
)
response = client.chat.completions.create(
model="information-extract",
messages=[{
"role": "user",
"content": [{"type": "image_url", "image_url": {"url": "https://example.com/invoice.pdf"}}]
}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "invoice_schema",
"schema": {
"type": "object",
"properties": {
"invoice_number": {"type": "string", "description": "Invoice ID"},
"total_amount": {"type": "string", "description": "Total amount with currency"},
"date": {"type": "string", "description": "Invoice date in YYYY-MM-DD"}
}
}
}
}
)
print(response.choices[0].message.content)
API Key: Always use os.environ["UPSTAGE_API_KEY"]. Get your key at console.upstage.ai.
ModeEndpointSyncPOST https://api.upstage.ai/v1/information-extractionAsyncPOST https://api.upstage.ai/v1/information-extraction/asyncStatusGET https://api.upstage.ai/v1/information-extraction/jobs/{job_id}
base_url to https://api.upstage.ai/v1/information-extractionParameterTypeRequiredDescriptionmodelstringYesinformation-extract or information-extract-nightly``messagesarrayYesSingle user message with image_url``response_formatobjectYesExtraction schema (JSON Schema format)modestringNostandard (default) or enhanced``locationbooleanNoReturn coordinates (default: false)confidencebooleanNoReturn confidence scores (default: false)splitbooleanNoSplit multi-document files (default: false)
ItemSyncAsyncMax pages1001,000Max properties1005,000Max schema chars15,000120,000
string, integer, number, array allowed (no objects)upstage-schema-generation skilljson
{
"choices": [
{
"message": {
"content": "{\"invoice_number\": \"INV-001\", \"total_amount\": \"$1,234.56\", \"date\": \"2026-01-15\"}"
}
}
],
"usage": {"prompt_tokens": 500, "completion_tokens": 50}
}
content is a JSON string. Parse with json.loads().
Ready-to-use models that require no schema definition.
ModelDocument Typereceipt-extractionReceiptsair-waybill-extractionAir waybillsbill-of-lading-and-shipping-request-extractionBills of lading / shipping requestscommercial-invoice-and-packing-list-extractionCommercial invoices / packing listskr-export-declaration-certificate-extractionKorean export declaration certificates
python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["UPSTAGE_API_KEY"],
base_url="https://api.upstage.ai/v1/information-extraction"
)
response = client.chat.completions.create(
model="receipt-extraction",
messages=[{
"role": "user",
"content": [{"type": "image_url", "image_url": {"url": "https://example.com/receipt.jpg"}}]
}]
)
print(response.choices[0].message.content)
Prebuilt models are called without response_format.
python
import os
import time
import requests
api_key = os.environ["UPSTAGE_API_KEY"]
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
# 1. Submit async job
response = requests.post(
"https://api.upstage.ai/v1/information-extraction/async",
headers=headers,
json={
"model": "information-extract",
"messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "FILE_URL"}}]}],
"response_format": {"type": "json_schema", "json_schema": {"name": "schema", "schema": {...}}}
}
)
job_id = response.json()["id"]
# 2. Poll for results
while True:
status = requests.get(
f"https://api.upstage.ai/v1/information-extraction/jobs/{job_id}",
headers=headers
).json()
if status["status"] == "completed":
print(status["choices"][0]["message"]["content"])
break
time.sleep(5)
<system-temp>/<input-stem>.extracted.json (e.g., /tmp/invoice.extracted.json). Use tempfile.gettempdir() for cross-platform code.enhanced mode improves accuracy on complex tables/images but is slower.confidence: true to get per-field confidence scores for quality filtering.upstage-schema-generation skill for automatic generation.split: true is useful when a single file contains multiple documents.