JSONValidator

org.llm4s.agent.guardrails.builtin.JSONValidator
See theJSONValidator companion object
class JSONValidator(schema: Option[Value]) extends OutputGuardrail

Validates that output is valid JSON matching an optional schema.

This guardrail ensures that LLM output is properly formatted JSON, which is useful when requesting structured data from the agent.

The schema is a deliberately small subset of JSON Schema:

  • required: the listed fields must be present on the top-level object.
  • properties.<field>.type: when a top-level field is present, its value must have that type. The types are string, number, integer (a number with no fractional part), boolean, object, array and null; a list such as ["string", "null"] accepts any of its types. A field the document does not contain is not checked (list it under required to demand it).

Everything else in a schema is ignored: nested properties, items, enum, format, and a type that is not one of the names above, so a schema this validator cannot judge never rejects output. Every field whose type does not match is reported, in the order the schema lists them.

Value parameters

schema

Optional JSON schema to validate against (minimal subset)

Attributes

Companion
object
Graph
Supertypes
trait Guardrail[String]
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

override def validate(value: String): Result[String]

Validate a value.

Validate a value.

This is a PURE FUNCTION - no side effects allowed. Same input always produces same output.

Value parameters

value

The value to validate

Attributes

Returns

Right(value) if valid, Left(error) if invalid

Definition Classes

Inherited methods

def andThen(other: Guardrail[String]): Guardrail[String]

Compose this guardrail with another sequentially.

Compose this guardrail with another sequentially.

The second guardrail runs only if this one passes.

Value parameters

other

The guardrail to run after this one

Attributes

Returns

A composite guardrail that runs both in sequence

Inherited from:
Guardrail
def transform(output: String): String

Optional: Transform the output after validation. Default is identity (no transformation).

Optional: Transform the output after validation. Default is identity (no transformation).

Value parameters

output

The validated output

Attributes

Returns

The transformed output

Inherited from:
OutputGuardrail
def validate(value: String, context: ModelContext): Result[String]

Validates value inside an agent's hook, as validate(value) does, with the hook's context. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form.

Validates value inside an agent's hook, as validate(value) does, with the hook's context. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form.

Attributes

Inherited from:
Guardrail

Concrete fields

override val description: Option[String]

Optional description of what this guardrail validates.

Optional description of what this guardrail validates.

Attributes

val name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes