Skip to main content
Get structured JSON, types, or formatted data from agents instead of plain text.

Quick Start

1

Get JSON Output via Instructions

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

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<T>Lists
HashMapKey-value data

Best Practices

Well-defined types help the LLM produce correct output.
Derive Serialize and Deserialize for your types.