Skip to main content

validate_structured

Function validate_structured 

Source
pub fn validate_structured(schema: &Value, answer: &str) -> Result<(), String>
Expand description

Structured output validation: the answer must parse as JSON and conform to the schema.

Used by self-implemented Agents wiring up typed output (TypedAgent): on failure, record the feedback text from structured_retry_message as a User message so the model can correct and retry (the budget is defined by the assembler; use StructuredValidator when you want a state machine with the budget built in). Failures return model-facing error text (English — fed back to the model, which corrects based on it).

The schema is compiled on every call (jsonschema caches compiled results internally; regular schemas compile in under a millisecond, negligible relative to model latency).

§Examples

use molo::agent::validate_structured;

let schema = serde_json::json!({
    "type": "object",
    "properties": { "city": { "type": "string" } },
    "required": ["city"],
});
assert!(validate_structured(&schema, r#"{"city":"Beijing"}"#).is_ok());
assert!(validate_structured(&schema, "not JSON").is_err());