Structured Output
What You’ll Build
Section titled “What You’ll Build”A prompt that returns structured JSON matching a schema you define — no manual parsing, no malformed output. The LLM is constrained to return exactly the fields you specify, and the Prompty processor auto-parses the result.
Step 1: Define the Output Schema
Section titled “Step 1: Define the Output Schema”Add an outputSchema block to your .prompty frontmatter. Each property
needs a name, kind (type), and optional description:
---name: weather-reportdescription: Returns structured weather data for a citymodel: id: gpt-4o-mini provider: openai apiType: chat connection: kind: key apiKey: ${env:OPENAI_API_KEY} options: temperature: 0.3inputSchema: properties: - name: city kind: string default: SeattleoutputSchema: properties: - name: city kind: string description: The city name - name: temperature kind: integer description: Temperature in degrees Fahrenheit - name: conditions kind: string description: Current weather conditions (e.g. sunny, cloudy, rain)template: format: kind: jinja2 parser: kind: prompty---system:You are a weather data API. Return the current weather for the requested city.
user:What's the weather in {{city}}?Prompty converts outputSchema into OpenAI’s response_format with
type: "json_schema" and strict mode enabled — the model must return
valid JSON with exactly those fields.
Step 2: Execute and Use the Result
Section titled “Step 2: Execute and Use the Result”from prompty import execute
result = execute("weather.prompty", inputs={"city": "Seattle"})
# result is already a dict — no JSON.parse neededprint(result["city"]) # "Seattle"print(result["temperature"]) # 62print(result["conditions"]) # "Partly cloudy"print(type(result)) # <class 'dict'>The async variant works identically:
from prompty import execute_async
result = await execute_async("weather.prompty", inputs={"city": "Seattle"})print(result["temperature"]) # 62import { execute } from "prompty";
const result = await execute("weather.prompty", { city: "Seattle" });
// result is already a parsed objectconsole.log(result.city); // "Seattle"console.log(result.temperature); // 62console.log(result.conditions); // "Partly cloudy"Nested Objects
Section titled “Nested Objects”For complex responses, use kind: object with nested properties:
---name: detailed-weathermodel: id: gpt-4o-mini provider: openai apiType: chat connection: kind: key apiKey: ${env:OPENAI_API_KEY}outputSchema: properties: - name: city kind: string - name: current kind: object properties: - name: temperature kind: integer description: Temperature in °F - name: humidity kind: integer description: Humidity percentage - name: conditions kind: string - name: forecast kind: array description: Next 3 days forecasttemplate: format: kind: jinja2 parser: kind: prompty---system:Return current weather and a 3-day forecast for the requested city.
user:Weather for {{city}}?from prompty import execute
result = execute("detailed-weather.prompty", inputs={"city": "Portland"})
print(result["city"]) # "Portland"print(result["current"]["temperature"]) # 58print(result["current"]["humidity"]) # 72print(result["forecast"]) # [{"day": "Mon", ...}, ...]import { execute } from "prompty";
const result = await execute("detailed-weather.prompty", { city: "Portland" });
console.log(result.city); // "Portland"console.log(result.current.temperature); // 58console.log(result.current.humidity); // 72console.log(result.forecast); // [{day: "Mon", ...}, ...]Provider Support
Section titled “Provider Support”The runtime generates this wire format automatically:
{ "type": "json_schema", "json_schema": { "name": "output_schema", "strict": true, "schema": { "type": "object", "properties": { "city": { "type": "string" }, "temperature": { "type": "integer" }, "conditions": { "type": "string" } }, "required": ["city", "temperature", "conditions"], "additionalProperties": false } }}All properties are marked required and additionalProperties is set to
false — the model returns exactly the fields you specified, nothing more.
Further Reading
Section titled “Further Reading”- Structured Output concept — how the conversion pipeline works under the hood
- Output Schema reference — full
outputSchemasyntax