Skip to content

schemaMatch ​

schemaMatch(schema) validates the output against a Zod schema and reports a boolean judgment. Use it for a contract you already own; use jsonCorrectness when the question is whether the text is valid JSON at all.

ts
import { schemaMatch } from '@anvia/core/evals'
import { z } from 'zod'

const metric = schemaMatch(z.object({ status: z.string(), count: z.number() }), {
  parseJson: true,
})
OptionDefaultEffect
schemarequiredZod schema the value must satisfy.
parseJsonfalseParse string output as JSON before validating.
nameschema_matchMetric name in scores and metricSummaries.
requiredtrueWhether a mismatch gates the case.
actualthe case outputSelector for the value to validate.

With parseJson: false the output itself is validated, which is what you want when the target already returns an object. With parseJson: true a string is parsed first.

Both a JSON parse error and a schema violation are recorded as a failure, not an invalid judgment, with the prettified Zod error as the comment. That is deliberate: the output was judged and it did not meet the contract. Only an unusable input, such as non-serializable output, becomes invalid.

Relationship to structured output ​

schemaMatch evaluates a target; it does not enforce one. If you need the agent itself to produce conforming output in production, configure outputSchema on the agent, which validates and repairs during the run. Use schemaMatch to prove the production path still satisfies the same schema, and keep both schemas in one module so they cannot drift.

A schema that accepts anything is not a test. Require the fields the application actually reads, and check optionality deliberately: z.object({ status: z.string() }) passes { status: "unknown" }, so pair the schema with containsAny or a predicate when the values matter too.

Built for Anvia.