Expand description
§serdes-ai-output
Output schema validation and structured output support for serdes-ai.
This crate provides the infrastructure for parsing, validating, and handling structured output from language models.
§Core Concepts
OutputMode: How the model generates structured output (text, native, prompted, tool)OutputSchema: Trait for parsing and validating model responsesTextOutputSchema: For plain text output with optional constraintsStructuredOutputSchema: For typed structured output using serdeOutputValidator: Additional validation logic after parsingOutputToolset: Internal toolset for capturing output via tool calls
§Output Modes
Different models support different ways of generating structured output:
- Text: Free-form text, parsed by the application
- Native: Model’s built-in JSON mode (e.g., OpenAI’s response_format)
- Prompted: JSON output requested via system prompt
- Tool: Output captured via a “result” tool call (most reliable)
§Example
use serdes_ai_output::{StructuredOutputSchema, OutputSchema, OutputMode};
use serdes_ai_tools::{ObjectJsonSchema, PropertySchema};
use serde::Deserialize;
#[derive(Deserialize)]
struct Person {
name: String,
age: u32,
}
// Define the JSON schema
let schema = ObjectJsonSchema::new()
.with_property("name", PropertySchema::string("Person's name").build(), true)
.with_property("age", PropertySchema::integer("Person's age").build(), true);
// Create the output schema
let output_schema: StructuredOutputSchema<Person> = StructuredOutputSchema::new(schema)
.with_tool_name("submit_person")
.with_description("Submit the person's information");
// Parse output from a tool call
let args = serde_json::json!({"name": "Alice", "age": 30});
let person: Person = output_schema.parse_tool_call("submit_person", &args).unwrap();
assert_eq!(person.name, "Alice");§Text Output with Validation
use serdes_ai_output::{TextOutputSchema, OutputSchema};
let schema = TextOutputSchema::new()
.with_min_length(10)
.with_max_length(1000)
.with_pattern(r"^[A-Z]").unwrap() // Must start with uppercase
.trim();
let result = schema.parse_text(" Hello, World! ").unwrap();
assert_eq!(result, "Hello, World!");Re-exports§
pub use error::OutputParseError;pub use error::OutputValidationError;pub use error::ParseResult;pub use error::ValidationResult;pub use mode::OutputMode;pub use parser::extract_json_from_text;pub use parser::looks_like_json;pub use parser::parse_json_from_text;pub use parser::parse_json_value;pub use schema::BoxedOutputSchema;pub use schema::OutputSchema;pub use schema::OutputSchemaWrapper;pub use spec::IntoOutputSpec;pub use spec::OutputSpec;pub use spec::OutputSpecBuilder;pub use structured::extract_json;pub use structured::AnyJsonSchema;pub use structured::StructuredOutputSchema;pub use structured::DEFAULT_OUTPUT_TOOL_DESCRIPTION;pub use structured::DEFAULT_OUTPUT_TOOL_NAME;pub use text::TextOutputSchema;pub use text::TextOutputSchemaBuilder;pub use toolset::OutputCaptured;pub use toolset::OutputToolset;pub use types::NativeOutput;pub use types::PromptedOutput;pub use types::StructuredDict;pub use types::TextOutput;pub use types::ToolOutput;pub use validator::async_validator;pub use validator::sync_validator;pub use validator::BoxedValidator;pub use validator::NoOpValidator;pub use validator::OutputValidator;pub use validator::RejectValidator;pub use validator::RetryValidator;pub use validator::SyncValidator;pub use validator::ValidatorChain;
Modules§
- error
- Error types for output parsing and validation.
- mode
- Output mode definitions.
- parser
- Output parsing utilities.
- prelude
- Prelude for common imports.
- schema
- Output schema trait and core types.
- spec
- Output specification types.
- structured
- Structured output schema implementation.
- text
- Text output schema implementation.
- toolset
- Output toolset implementation.
- types
- Output type wrappers and markers.
- validator
- Output validators.