Example
Request

import base64
import json
from openai import OpenAI
client = OpenAI(
api_key="UPSTAGE_API_KEY",
base_url="https://api.upstage.ai/v1/information-extraction/schema-generation"
)
def encode_img_to_base64(img_path):
with open(img_path, 'rb') as img_file:
img_bytes = img_file.read()
base64_data = base64.b64encode(img_bytes).decode('utf-8')
return base64_data
# Read the image file and encode it to base64
img_path = "./bank_statement.png"
base64_data = encode_img_to_base64(img_path)
# Schema generation request
schema_response = client.chat.completions.create(
model="information-extract",
messages=[
{
"role": "system",
"content": "Generate schema about bank_name."
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{base64_data}"}
}
]
}
],
)
schema = json.loads(schema_response.choices[0].message.content)Response
{
"id": "iex-91aDXzeddudon1fH",
"choices": [
{
"finish_reason": "stop",
"index": null,
"logprobs": null,
"message": {
"content": "{\"type\": \"json_schema\", \"json_schema\": {\"name\": \"document_schema\", \"schema\": {\"type\": \"object\", \"properties\": {\"bankName\": {\"type\": \"string\", \"description\": \"The name of the bank.\"}, \"accountNumber\": {\"type\": \"string\", \"description\": \"The account number associated with the statement.\"}, \"statementDate\": {\"type\": \"string\", \"description\": \"The date of the statement.\"}, \"periodCovered\": {\"type\": \"string\", \"description\": \"The period covered by the statement.\"}, \"accountHolder\": {\"type\": \"array\", \"items\": {\"type\": \"object\", \"properties\": {\"name\": {\"type\": \"string\", \"description\": \"The name of the account holder.\"}, \"address\": {\"type\": \"string\", \"description\": \"The address of the account holder.\"}}, \"required\": [\"name\", \"address\"]}}, \"openingBalance\": {\"type\": \"number\", \"description\": \"The opening balance of the account.\"}, \"totalCreditAmount\": {\"type\": \"number\", \"description\": \"The total amount credited to the account.\"}, \"totalDebitAmount\": {\"type\": \"number\", \"description\": \"The total amount debited from the account.\"}, \"closingBalance\": {\"type\": \"number\", \"description\": \"The closing balance of the account.\"}, \"transactions\": {\"type\": \"array\", \"items\": {\"type\": \"object\", \"properties\": {\"date\": {\"type\": \"string\", \"description\": \"The date of the transaction.\"}, \"description\": {\"type\": \"string\", \"description\": \"A brief description of the transaction.\"}, \"credit\": {\"type\": \"number\", \"description\": \"The amount credited in the transaction.\"}, \"debit\": {\"type\": \"number\", \"description\": \"The amount debited in the transaction.\"}, \"balance\": {\"type\": \"number\", \"description\": \"The balance after the transaction.\"}}, \"required\": [\"date\", \"description\", \"credit\", \"debit\", \"balance\"]}}}, \"required\": [\"bankName\", \"accountNumber\", \"statementDate\", \"periodCovered\", \"accountHolder\", \"openingBalance\", \"totalCreditAmount\", \"totalDebitAmount\", \"closingBalance\", \"transactions\"]}}}",
"role": "assistant",
"function_call": null,
"tool_calls": null
}
}
],
"created": 1742837609,
"model": "information-extract",
"object": null,
"system_fingerprint": null,
"usage": {
"completion_tokens": 591,
"prompt_tokens": 1230,
"total_tokens": 1821,
"completion_tokens_details": null,
"prompt_tokens_details": null
}
}Universal extraction
Extract structured fields from any document using your own JSON schema. Covers the request body (model, messages, response_format, chunking), the chat.completion response, and example requests.
Asynchronous API
Submit multiple documents to universal information extraction as an async job and retrieve the results by job ID.