Skip to content

About

Structured outputs for LLMs

Resources

Contributing

Stars

58 stars

Watchers

2 watching

Forks

Repository files navigation

Instructor for Rust

Turn an LLM response into a Rust value. Annotate your type with #[model] and call client.create(prompt). Instructor derives the schema and can ask the model to correct an invalid reply.

Route your first ticket

This CLI extracts an order ID and a queue from a customer message. With recent stable Rust, install the GitHub source release. The crates.io release predates these APIs.

cargo new ticket-router
cd ticket-router
cargo add instructor-ai --git https://github.com/567-labs/instructor-rs \
  --tag rust-2026-12-01

Replace src/main.rs with this program. The Ticket annotation sets the response type, and the match selects the action for each queue. Try it without a key

use instructor_ai::{model, Error};

#[model]
#[serde(rename_all = "snake_case")]
enum Queue {
    Billing,
    Shipping,
    Other,
}

#[model]
struct Ticket {
    queue: Queue,
    order_id: String,
}

fn main() -> Result<(), Error> {
    let client = instructor_ai::BlockingClient::from_provider("openai/gpt-6-luna")?;
    let ticket: Ticket = client.create("Route this ticket: I was charged twice for order A17.")?;

    let action = match ticket.queue {
        Queue::Billing => "Review charges",
        Queue::Shipping => "Check tracking",
        Queue::Other => "Read the ticket",
    };
    println!("{action} for order {}", ticket.order_id);
    Ok(())
}

Set OPENAI_API_KEY and run the CLI. Expect Review charges for order A17.

cargo run

Try a saved reply without a key

Replace the client and ticket bindings in main with these lines.

let saved_reply = r#"{"queue":"billing","order_id":"A17"}"#;
let ticket: Ticket = instructor_ai::parse_response(saved_reply, &Default::default())?;

Run cargo run. Try shipping to select another action, or sales to see the parser reject an unknown queue. For a full response saved with create_with_completion, use parse_completion. Parse a saved completion

Choose your provider

Pass a model your account can access to BlockingClient::from_provider and set its key.

Provider Model argument Environment variable
OpenAI "openai/gpt-6-luna" OPENAI_API_KEY
Anthropic "anthropic/claude-haiku-5-5" ANTHROPIC_API_KEY
OpenRouter "openrouter/openai/gpt-6.2-sol" OPENROUTER_API_KEY

You can also use Google Gemini, Cohere or Mistral. Provider setup and supported modes

Build on the example

Porting a Python application? Supported workflows · Benchmarks

About

Structured outputs for LLMs

Resources

Contributing

Stars

58 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages