Skip to contents

Checks strict JSON syntax and the requested schema locally, without changing responses or logging validation failures. Does not coerce values or repair JSON. Extra properties are allowed by the current Schema; optional fields may be absent but may not be null. An object field constrains only its outer type; an array field also constrains its elements, since items says what it holds.

Usage

validate_output(x, schema)

Arguments

x

Character, Message, or list: JSON response text(s), a single message, an LLM batch, or a list of agent conversations. A flat message list is an LLM batch, as in responses; wrap one agent conversation in list() to check only its final assistant answer. A result directly from generate(agent, ...) is recognized as a single conversation.

schema

Schema: Requested output schema created with schema.

Value

An S7 validation report with @output (original text), @status (valid, invalid, unavailable, or not_validated), @schema, and @issues (a data.frame with index, path, keyword, and message). Missing responses are unavailable; malformed JSON is invalid. Empty input gives an empty report.

Examples

sch <- schema("Count", field("n", type = "integer"))
report <- validate_output(c('{"n":10}', '{"n":"10"}', NA_character_), sch)
report@status
#> [1] "valid"       "invalid"     "unavailable"
report@issues
#>   index path keyword         message
#> 1     2   /n    type must be integer