pub struct StructuredOutput {
pub raw: String,
pub parsed: Option<Value>,
pub validation_result: ValidationResult,
}Expand description
Structured output result with validation.
Wraps the raw response from the AI model with parsed, validated, and formatted data.
Fields§
§raw: StringRaw response content as string
parsed: Option<Value>Parsed JSON data (None if parsing failed)
validation_result: ValidationResultValidation result (always populated)
Implementations§
Source§impl StructuredOutput
impl StructuredOutput
Sourcepub fn from_response_unvalidated(content: impl Into<String>) -> Self
pub fn from_response_unvalidated(content: impl Into<String>) -> Self
Create a structured output from raw content without validation.
§Arguments
content- Raw response content
Returns: A StructuredOutput instance with parsed data but without validation.
Use .validate() method to add validation after creation.
Example:
use ai_lib_rust::structured::{StructuredOutput, OutputValidator};
use serde_json::json;
let schema = json!({"type": "object", "properties": {"name": {"type": "string"}}});
let validator = OutputValidator::lenient(schema);
let mut output = StructuredOutput::from_response_unvalidated(
r#"{"name": "Alice"}"#
);
output.validate(&validator);
assert!(output.is_valid());Sourcepub fn validate(&mut self, validator: &OutputValidator)
pub fn validate(&mut self, validator: &OutputValidator)
Validate this output against a schema.
§Arguments
validator- The validator to use
Updates the validation_result with the schema check result.
Example:
use ai_lib_rust::structured::{StructuredOutput, OutputValidator};
use serde_json::json;
let schema = json!({"type": "object", "properties": {"name": {"type": "string"}}});
let validator = OutputValidator::lenient(schema);
let mut output = StructuredOutput::from_response_unvalidated(r#"{"name": "Alice"}"#);
output.validate(&validator);Sourcepub fn from_response(
content: impl Into<String>,
validator: &OutputValidator,
) -> Self
pub fn from_response( content: impl Into<String>, validator: &OutputValidator, ) -> Self
Create a structured output from raw content with validation.
§Arguments
content- Raw response contentvalidator- Validator to check the content
Returns: A StructuredOutput instance with parsed and validated data.
Example:
use ai_lib_rust::structured::{StructuredOutput, OutputValidator};
use serde_json::json;
let schema = json!({"type": "object", "properties": {"name": {"type": "string"}}});
let validator = OutputValidator::lenient(schema);
let output = StructuredOutput::from_response(
r#"{"name": "Alice"}"#,
&validator
);
assert!(output.is_valid());Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Check if the output is valid.
Returns true if:
- JSON parsing succeeded
- Validation passed (if validation was performed)
Sourcepub fn data(&self) -> Value
pub fn data(&self) -> Value
Get the best available data representation.
Priority:
- Validated data (if validation passed)
- Parsed data (if available)
- Raw content as string
Sourcepub fn validation_result(&self) -> &ValidationResult
pub fn validation_result(&self) -> &ValidationResult
Get the validation result.
Sourcepub fn errors(&self) -> Vec<ValidationError>
pub fn errors(&self) -> Vec<ValidationError>
Get validation errors if validation failed.
Sourcepub fn error_messages(&self) -> Vec<String>
pub fn error_messages(&self) -> Vec<String>
Get error messages as strings.
Trait Implementations§
Source§impl Clone for StructuredOutput
impl Clone for StructuredOutput
Source§fn clone(&self) -> StructuredOutput
fn clone(&self) -> StructuredOutput
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more