Structured extraction
Turning unstructured text into JSON your code can rely on.
Asking for JSON
response_format is a supported request field. Setting it to
{"type": "json_object"} asks the model to emit valid JSON.
Python
import json, os
from openai import OpenAI
client = OpenAI(base_url="https://api.astrodyne.ai/v1",
api_key=os.environ["ASTRODYNE_API_KEY"])
resp = client.chat.completions.create(
model="YOUR_MODEL_ID",
response_format={"type": "json_object"},
messages=[
{"role": "system",
"content": "Extract fields as JSON with keys: name, email, company."},
{"role": "user", "content": raw_text},
],
)
data = json.loads(resp.choices[0].message.content)
Validate what comes back
Asking for JSON is not a guarantee of your schema. Parse it, validate it against
a schema you control, and decide what to do when a field is missing. Treat model
output as untrusted input, exactly like a form submission.
Making extraction reliable
- Name every field you want, in the system message, with its type.
- Say explicitly what to emit when a value is absent —
nullbeats invention. - Set a low
temperature. Extraction is not a creative task. - Use
seedwhen you need repeatable output for a test.
Cost shape
Extraction is input-heavy and output-light, so the input price per million tokens dominates. An efficient-tier model is often the right choice; compare in Models.