Structured Data

Generate structured JSON-style outputs by prompting models to return specific fields.

This page demonstrates a lightweight technique to request structured outputs without relying on external SDK helpers. You can instruct the model to return a strict JSON object and then validate it on your side.

Python

1import os
2import json
3import requests
4from typing import List
5
6# ---- Config from environment ----
7LLM_API_ENDPOINT = os.getenv("LLM_API_ENDPOINT", "https://api.asi1.ai/v1")
8LLM_API_KEY = os.getenv("LLM_API_KEY", "your-api-key")
9LLM_MODEL = os.getenv("LLM_MODEL", "asi1-extended")
10
11headers = {
12 "Authorization": f"Bearer {LLM_API_KEY}",
13 "Content-Type": "application/json",
14}
15
16# ---- Target JSON schema (order summary) ----
17response_format = {
18 "type": "json_schema",
19 "json_schema": {
20 "name": "order_summary",
21 "strict": True,
22 "schema": {
23 "type": "object",
24 "additionalProperties": False,
25 "properties": {
26 "order_id": {"type": "string"},
27 "total": {"type": "number"},
28 "currency": {"type": "string"},
29 "items": {
30 "type": "array",
31 "minItems": 1,
32 "items": {
33 "type": "object",
34 "additionalProperties": False,
35 "properties": {
36 "sku": {"type": "string"},
37 "name": {"type": "string"},
38 "qty": {"type": "integer", "minimum": 1},
39 "unit_price": {"type": "number"}
40 },
41 "required": ["sku", "name", "qty", "unit_price"]
42 }
43 }
44 },
45 "required": ["order_id", "total", "currency", "items"]
46 }
47 }
48}
49
50prompt = (
51 "Generate a realistic example order summary with 2–3 items. "
52 "Use USD as ISO currency code. Fill every required field. "
53 "The order id is 1234567890. "
54 "The total is 100. "
55 "The currency is USD. "
56 "The items are 100. "
57 "The unit price is 1."
58)
59
60# ---- Chat Completions payload ----
61payload = {
62 "model": LLM_MODEL,
63 "messages": [
64 {"role": "system", "content": "Return ONLY valid JSON matching the provided schema."},
65 {"role": "user", "content": prompt},
66 ],
67 "response_format": response_format,
68}
69
70resp = requests.post(
71 f"{LLM_API_ENDPOINT}/chat/completions",
72 headers=headers,
73 json=payload,
74 timeout=60,
75)
76
77# Raw response
78if not resp.ok:
79 print(resp.status_code, resp.text)
80 resp.raise_for_status()
81
82data = resp.json()
83
84# Simple parse of final JSON content
85try:
86 content = (
87 (data.get("choices") or [{}])[0]
88 .get("message", {})
89 .get("content", "")
90 )
91 parsed = json.loads(content) if content else None
92except Exception:
93 parsed = None
94
95print("\n=== Output ===")
96print(json.dumps(parsed, indent=2) if parsed is not None else "None")

Example Output

$=== Output ===
>{
> "order_id": "1234567890",
> "total": 100,
> "currency": "USD",
> "items": [
> {
> "sku": "WIRELESS-EARBUDS",
> "name": "Wireless Earbuds",
> "qty": 100,
> "unit_price": 1
> }
> ]
>}

Notes

  • The model is instructed to return JSON only. Still, validate and sanitize on your side.
  • For production, enforce schemas server-side and reject invalid JSON.