Skip to main content
Version: 1.0.5

Structured Data with ASI:One

ASI:One can return responses that conform to a JSON schema you define. This example shows how to extract structured information—an order_summary—directly from natural language using the response_format parameter with a JSON schema.

import os
import json
import requests
from typing import List
from openai import OpenAI


# ---- Config from environment ----
LLM_API_ENDPOINT = os.getenv("LLM_API_ENDPOINT", "https://api.asi1.ai/v1")
LLM_API_KEY = os.getenv("LLM_API_KEY", "your-api-key")
LLM_MODEL = os.getenv("LLM_MODEL", "asi1")

# Initialize OpenAI client with ASI1 API
client = OpenAI(
api_key=LLM_API_KEY,
base_url=LLM_API_ENDPOINT
)

headers = {
"Authorization": f"Bearer {LLM_API_KEY}",
"Content-Type": "application/json",
}

# ---- Target JSON schema (order summary) ----
response_format = {
"type": "json_schema",
"json_schema": {
"name": "order_summary",
"strict": True,
"schema": {
"type": "object",
"additionalProperties": False,
"properties": {
"order_id": {"type": "string"},
"total": {"type": "number"},
"currency": {"type": "string"},
"items": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"additionalProperties": False,
"properties": {
"sku": {"type": "string"},
"name": {"type": "string"},
"qty": {"type": "integer", "minimum": 1},
"unit_price": {"type": "number"}
},
"required": ["sku", "name", "qty", "unit_price"]
}
}
},
"required": ["order_id", "total", "currency", "items"]
}
}
}

prompt = (
"Generate a realistic example order summary with 2–3 items. "
"Use USD as ISO currency code. Fill every required field. "
"The order id is 1234567890. "
"The total is 100. "
"The currency is USD. "
"The items are 100. "
"The unit price is 1."
)

# ---- Chat Completions payload ----
payload = {
"model": LLM_MODEL,
"messages": [
{"role": "system", "content": "Return ONLY valid JSON matching the provided schema."},
{"role": "user", "content": prompt},
],
"response_format": response_format,
}

resp = requests.post(
f"{LLM_API_ENDPOINT}/chat/completions",
headers=headers,
json=payload,
timeout=60,
)

# Raw response
if not resp.ok:
print(resp.status_code, resp.text)
resp.raise_for_status()

data = resp.json()

# Simple parse of final JSON content
try:
content = (
(data.get("choices") or [{}])[0]
.get("message", {})
.get("content", "")
)
parsed = json.loads(content) if content else None
except Exception:
parsed = None

print("\n=== Output ===")
print(json.dumps(parsed, indent=2) if parsed is not None else "None")

Expected Output

The model returns a response that conforms to the JSON schema you defined. The response is automatically structured as an order_summary object:

=== Output ===
{
"currency": "USD",
"items": [
{
"name": "Wireless Bluetooth Earbuds",
"qty": 40,
"sku": "WBE-001",
"unit_price": 1.0
},
{
"name": "USB-C Charging Cable (6ft)",
"qty": 35,
"sku": "UCC-002",
"unit_price": 1.0
},
{
"name": "Phone Stand - Adjustable",
"qty": 25,
"sku": "PSA-003",
"unit_price": 1.0
}
],
"order_id": "1234567890",
"total": 100.0
}

You now have structured order data that conforms to your schema, ready for further processing such as saving to a database, generating invoices, triggering notifications, or integrating with payment systems.


Next Steps