> For the complete documentation index, see [llms.txt](https://docs.layerlens.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.layerlens.ai/more-in-this-section-6/json-schema-validate.md).

# Recipe: validate JSON output against a schema

**When to use:** the model must return valid schema-conformant JSON every time.

The Stratix SDK doesn't ship a dedicated "JSON-schema scorer" type. Two viable patterns:

## Pattern A — pre-evaluation validation (cheapest)

Validate JSON shape **client-side** before posting traces. Catches schema errors before consuming evaluation budget.

```python
import json, jsonschema
from layerlens import Stratix

schema = {
 "type": "object",
 "properties": {
 "answer": {"type": "string"},
 "confidence": {"type": "number", "minimum": 0, "maximum": 1},
 },
 "required": ["answer", "confidence"],
 "additionalProperties": False,
}

client = Stratix()

def trace_with_schema_check(model_output: dict, **trace_kwargs):
 try:
 jsonschema.validate(instance=model_output, schema=schema)
 except jsonschema.ValidationError as e:
 # Tag failed traces explicitly so you can filter them downstream
 trace_kwargs.setdefault("tags", {})["schema_valid"] = False
 trace_kwargs["tags"]["schema_error"] = str(e)
 else:
 trace_kwargs.setdefault("tags", {})["schema_valid"] = True
 return client.traces.upload(...) # your trace upload path
```

## Pattern B — custom judge that scores schema conformance

Build a scorer/judge whose prompt encodes the schema rule:

```python
scorer = client.scorers.create(
 name="response-schema-conformance",
 description="Score whether output is valid JSON matching the response schema",
 model_id=model.id,
 prompt="""You are validating model output against a JSON schema.

Schema:
{
 "type": "object",
 "required": ["answer", "confidence"],
 "properties": {
 "answer": {"type": "string"},
 "confidence": {"type": "number", "minimum": 0, "maximum": 1}
 }
}

Score 0 if the output is not valid JSON or does not conform.
Score 1 if it conforms exactly.
""",
)
```

Pattern A is faster and free. Pattern B is what to use when the deviation must be quantified across many traces.

## See also

* [Concept: Scorers](/8.-evaluate-score-the-outputs/scorers-1.md)
* [Stratix Premium — Scorers](/8.-evaluate-score-the-outputs/scorers.md)
* [SDK reference: scorers](/more-in-this-section-9/scorers-2.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.layerlens.ai/more-in-this-section-6/json-schema-validate.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
