githubEdit

box-openStructured Output

Structured output allows you to request JSON responses that follow a specific schema from language models. This ensures that the model's response conforms to your expected data structure, making it easier to parse and use in your applications.

Orpheus provides a powerful and ergonomic API for structured output through the Format type. You can define JSON schemas that specify exactly what structure you want the model to return, including property types, descriptions, and requirements.

Basic Usage

To use structured output, create a Format using the builder pattern and attach it to your chat request:

set_response_format.rs
use orpheus::prelude::*;

fn main() -> anyhow::Result<()> {
    let client = Orpheus::from_env()?;

    // Define your response format
    let person_format = Format::json("person")
        .with_schema(|schema| {
            schema
                .property("name", Param::string())
                .property("age", Param::number())
                .required(["name", "age"])
        })
        .build();

    let response = client
        .chat("Jessica is a 20 year old college student.")
        .model("openai/gpt-4o")
        .response_format(person_format)
        .send()?;

    println!("{}", response.content()?);
    // Output: {"name": "Jessica", "age": 20}

    Ok(())
}

Schema Definition

Format Builder

The Format::json() method starts a JSON schema format builder:

Parameter Types

Orpheus supports all standard JSON schema types through the Param enum:

String Parameters

Number Parameters

Boolean Parameters

Object Parameters

Array Parameters

Null Parameters

Advanced Features

Union Types (anyOf)

You can specify that a field can be one of several types using the anyof! macro:

Last updated

Was this helpful?