Structured Output
Define outputs in a prompt when the model should return data matching a known
shape.
outputs: - name: city kind: string - name: temp kind: integerStructuredResult
Section titled “StructuredResult”When outputs is defined, processors can return a StructuredResult: a
dictionary/object-shaped value that also keeps the raw JSON string for efficient
typed casting.
cast(result, target)
Section titled “cast(result, target)”Deserialize a processed structured result to an application type.
from dataclasses import dataclassfrom prompty import cast
@dataclassclass Weather: city: str temperature: int
weather = cast(result, Weather)import { cast } from "@prompty/core";import { z } from "zod";
const Weather = z.object({ city: z.string(), temperature: z.number() });const weather = cast<z.infer<typeof Weather>>(result, Weather.parse);using Prompty.Core;
record Weather(string City, int Temperature);
var weather = ((StructuredResult)result).Cast<Weather>();use serde::Deserialize;
#[derive(Deserialize)]struct Weather { city: String, temperature: i32,}
let weather: Weather = prompty::cast(&result)?;Typed invocation
Section titled “Typed invocation”Some runtimes provide generic invocation helpers that run the prompt and cast the final result in one step.
weather = invoke( "weather.prompty", inputs={"city": "Seattle"}, target_type=Weather,)const weather = await invoke( "weather.prompty", { city: "Seattle" }, { validator: Weather.parse },);var weather = await Pipeline.InvokeAsync<Weather>( "weather.prompty", new() { ["city"] = "Seattle" });let weather: Weather = prompty::invoke_from_path_typed( "weather.prompty", Some(&json!({ "city": "Seattle" })),).await?;