StructuredOutput

Trait StructuredOutput 

Source
pub trait StructuredOutput: Serialize + DeserializeOwned {
    // Required methods
    fn tool_name() -> &'static str;
    fn tool_description() -> &'static str;
    fn json_schema() -> Value;

    // Provided method
    fn tool_definition() -> ToolDefinition { ... }
}
Expand description

Core trait for types that can be used as structured LLM outputs.

This trait enables a type to be used as a structured output from an LLM by:

  1. Generating a JSON Schema describing the type’s structure
  2. Creating a tool definition that the LLM can call
  3. Validating and deserializing tool call arguments

The derive macro provides automatic implementation for most use cases:

#[derive(Serialize, Deserialize, StructuredOutput)]
#[structured_output(
    name = "final_answer",
    description = "Final response with structured data"
)]
struct Answer {
    response: String,
    confidence: f32,
}

Required Methods§

Source

fn tool_name() -> &'static str

Tool name used in API requests (e.g., “final_answer”, “create_character”)

Source

fn tool_description() -> &'static str

Human-readable description of what this output represents

Source

fn json_schema() -> Value

JSON Schema describing this type’s structure

The schema should follow the JSON Schema specification and will be used to validate the LLM’s output before deserialization.

Provided Methods§

Source

fn tool_definition() -> ToolDefinition

Complete tool definition ready for API requests

This combines the tool name, description, and schema into the format expected by LLM APIs (OpenAI, Anthropic, etc.)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§