Why Use Structured Output?
Structured output lets you define the exact format you expect in the modelβs response, making it easier to parse, validate, and integrate downstream.Prerequisites
- An API key from Prem Studio
- Python β₯ 3.8 or Node.js β₯ 18
- premai or openai SDK installed
Supported Models
The list of supported models is constantly evolving. For the most up-to-date list of models that support structured JSON output, please visit the models list page. Models that support JSON output are clearly marked with a json tag on the models page.Generate Structured Output with Prem
This quick guide is gonna show you how to extract structured data from a raw CV using the Prem SDK. Youβll define a clear schema (e.g. name, email, education, work experience) and let the model return a clean JSON object that matches it. Why itβs useful: It turns messy, unstructured CV text into structured data automatically β no regex, no manual parsing. When to use it: Perfect for dev teams building HR tools, job platforms, or internal automation scripts that need to process and understand user-submitted resumes.1
Setup Environment
2
Define Output Structure and Example Text to Parse
3
Initialize Client
4
Format Messages and Send Request with Expected Output Schema
5
Parse the Output
The
json_repair.loads()
function automatically handles malformed JSON and extracts valid JSON from text that may contain extra content before or after the JSON block. This is much more robust than using json.loads()
and manual string extraction, which can fail with malformed JSON responses.Full Copy-Paste Example
Pro Tips
- We use the json-repair package to automatically handle malformed JSON and extract valid JSON from model responses, making the parsing much more robust than using
json.loads()
. - For even more advanced structured output handling, consider using the instructor package which provides additional validation and retry mechanisms.
- If the model doesnβt follow your schema correctly, simplify the schema by reducing nesting or optional fields.
- Be explicit in your instructions (e.g., βReturn only valid JSON without any explanation or extra text.β)
- Include examples in your prompt (few-shot prompting) to help the model understand the expected format more accurately.
Other Common Use-Cases
- Extracting metadata from documents
- Structured answers for UI rendering
- Converting text into application-specific formats