use claude_sdk::{CacheTtl, EffortLevel, Message, MessagesRequest, Metadata, ServiceTier};
use serde_json::json;
fn main() {
println!("Claude SDK - Structured Output Example");
println!("==========================================\n");
let schema = json!({
"type": "object",
"properties": {
"summary": {
"type": "string",
"description": "Brief summary of the analysis"
},
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral", "mixed"]
},
"confidence": {
"type": "number",
"minimum": 0.0,
"maximum": 1.0
},
"key_topics": {
"type": "array",
"items": {"type": "string"},
"maxItems": 5
},
"recommendation": {
"type": "string"
}
},
"required": ["summary", "sentiment", "confidence", "key_topics"]
});
println!("JSON Schema:");
println!("{}\n", serde_json::to_string_pretty(&schema).unwrap());
let request = MessagesRequest::new(
"claude-sonnet-4-5-20250929",
4096,
vec![Message::user(
"Analyze this product review: 'The new laptop is incredibly fast \
and the battery lasts all day. However, the keyboard feels cheap \
and the trackpad is too small. Overall decent value for the price.'",
)],
)
.with_system("You are a product review analyst. Always provide structured analysis.")
.with_json_schema(schema)
.with_adaptive_thinking()
.with_effort(EffortLevel::High)
.with_service_tier(ServiceTier::Auto)
.with_metadata(Metadata {
user_id: Some("analyst-42".into()),
})
.with_container("analysis-session-001");
let _ = CacheTtl::OneHour;
println!("Full Request JSON:");
let json = serde_json::to_string_pretty(&request).unwrap();
println!("{}\n", json);
let val: serde_json::Value = serde_json::to_value(&request).unwrap();
println!("Key Features Used:");
if val.get("output_config").is_some() {
println!(" output_config.format -- JSON schema for structured output");
println!(" output_config.effort -- High effort for maximum quality");
}
if val.get("thinking").is_some() {
println!(" thinking -- Adaptive (Claude decides reasoning depth)");
}
if val.get("service_tier").is_some() {
println!(" service_tier -- Auto (priority if available)");
}
if val.get("metadata").is_some() {
println!(" metadata -- User tracking for abuse detection");
}
if val.get("container").is_some() {
println!(" container -- Persistent execution context");
}
println!("\nTo send this request, set ANTHROPIC_API_KEY and use:");
println!(" let response = client.send_message(request).await?;");
println!("\nThe response will be guaranteed JSON matching the schema above.");
}