Structured Output

Get structured data from your agents

Get structured JSON, types, or formatted data from agents instead of plain text.

Diagram

Structured Output

🤖 Agent

📝 Prompt

📋 Schema

📦 JSON

Quick Start

  1. Get JSON Output via Instructions
    rust
    use praisonai::Agent;
    use serde::{Deserialize, Serialize};
    use serde_json;
    
    #[derive(Serialize, Deserialize)]
    struct Person {
        name: String,
        age: u32,
    }
    
    // Create agent with JSON output instruction
    let agent = Agent::new()
        .name("Extractor")
        .instructions("Extract information and respond ONLY with valid JSON, no other text.")
        .build()?;
    
    let response = agent.chat("Extract: John is 30 years old. Format: {\"name\": \"...\", \"age\": ...}").await?;
    
    // Parse the JSON response
    let person: Person = serde_json::from_str(&response)?;
    println!("{} is {} years old", person.name, person.age);
  2. Structured Extraction Tool
    rust
    use praisonai::{Agent, tool};
    use serde::{Deserialize, Serialize};
    
    #[derive(Serialize, Deserialize)]
    struct Person { name: String, age: u32 }
    
    #[tool(description = "Extract person info and return as JSON")]
    fn extract_person(name: String, age: u32) -> String {
        let person = Person { name, age };
        serde_json::to_string(&person).unwrap()
    }
    
    let agent = Agent::new()
        .name("Extractor")
        .instructions("Use extract_person to return structured data")
        .tool(extract_person)
        .build()?;

Output Formats

FormatUse Case
StringPlain text (default)
T: DeserializeAny struct
`Vec

| Lists | | HashMap` | Key-value data |


Best Practices

Define clear schemas

Well-defined types help the LLM produce correct output.

Use serde for serialization

Derive Serialize and Deserialize for your types.


Related