EN·ES·DE·PT·FR
⌘K

Typed output

Return a model call, or an agent's final answer, as your own type. Derive Deserialize and JsonSchema and ask for it.

The request carries the schema of your type (derived with schemars), and the instructions repeat it for providers that do not enforce schemas. A reply that does not parse gets one repair attempt: the parse error goes back to the model, which answers again. A second failure is StructuredError::Invalid.

rust
use aidaemon_sdk::structured::{complete_typed, AgentStructuredExt};
use schemars::JsonSchema;
use serde::Deserialize;

/// How a support ticket should be handled.
#[derive(Deserialize, JsonSchema)]
struct Triage {
    /// 1 is the most urgent.
    priority: u8,
    refund: bool,
}

// One model call.
let triage = complete_typed::<Triage>(&provider, request).await?;
println!("{} {}", triage.value.priority, triage.value.refund);

// An agent's final answer. Tools still run as usual;
// only the final answer must match the type.
let typed = agent.run_typed::<Triage>("support:ticket-1", "Where is my order?").await?;
println!("{}", typed.value.priority);

What comes back

  • complete_typed::<T>(&provider, request) returns Typed<T>: value, the response it was read from, the usage of every call made (the repair included) and repaired.
  • agent.run_typed::<T>(session_id, input) returns TypedOutcome<T>: value, the full outcome (receipts, usage, state) and repaired.
  • run_typed_from and resume_typed do the same for a saved state or an interrupted run. A run that pauses again for approval ends as StructuredError::NotFinished, so you can resume it.

Both live in aidaemon_sdk::structured and need no extra feature. The structured_output test in the SDK crate runs them against the scripted provider.