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:
- Generating a JSON Schema describing the type’s structure
- Creating a tool definition that the LLM can call
- 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§
Sourcefn tool_name() -> &'static str
fn tool_name() -> &'static str
Tool name used in API requests (e.g., “final_answer”, “create_character”)
Sourcefn tool_description() -> &'static str
fn tool_description() -> &'static str
Human-readable description of what this output represents
Sourcefn json_schema() -> Value
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§
Sourcefn tool_definition() -> ToolDefinition
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.