Testing JSON and structured outputs with promptfoo: when a suite is worth building
If your application depends on a model returning parseable JSON or a valid function call, a failed parse or a malformed argument breaks a downstream step, and often breaks it silently. promptfoo checks structured output with objective assertions that run locally from its CLI, so you can catch these failures before a prompt or model change reaches users. The promptfoo JSON evaluation guide covers schema checks and field-level assertions, and this page walks through the is-json and contains-json checks, JavaScript assertions on individual fields, and validation of function calls against tool schemas.
A suite is worth building when the output feeds code rather than a person. Typical signals are an extraction pipeline that writes to a database, an agent that dispatches tool calls, or a service that a frontend parses directly. In those cases, a prompt edit or a model upgrade can keep the text looking fluent while dropping a required key, changing a field type, or returning prose wrapped around the JSON. Before building anything, list which fields your code reads, which of them must match an enumerated value or a number range, and which failures your downstream systems already handle. Those answers decide how many assertions you need and where a model grader would add cost without adding signal.
The effort is mostly in the test cases, not the configuration. You need representative inputs, including messy and edge-case ones, with expected field values where they are known. Confirm with your team which checks run on every pull request and which run on a schedule, because schema and field checks are cheap to repeat while large case sets multiply model calls. Also confirm which model provider and version your suite will call, since structured output behavior can differ between them. Keep the suite in version control next to the prompts it tests, so each change to a prompt shows its effect on the fields your code depends on.
Which assertion fits which output contract
Structured output tests fail in different places: the whole response may not parse, the JSON may be buried in prose, a field may hold a wrong value, or a tool call may use arguments the schema does not allow. Each promptfoo assertion proves one of these things and nothing more, so pick the check that matches the contract your application depends on.
Whole-output validity with is-json
Use is-json when the entire model response must be a parseable JSON document and nothing else. It proves the output parses and, when you supply a schema, that the parsed value matches it. It cannot prove that the values are correct, that required business rules hold, or that the model avoided wrapping the JSON in a code fence or sentence. Confirm the exact option names and schema behavior in the promptfoo JSON evaluation guide before you rely on them.
Embedded JSON with contains-json
Use contains-json when the model writes prose and includes a JSON object somewhere in the response, such as a chat reply with a machine-readable block. It proves that a JSON fragment exists and can be parsed. It cannot prove that the fragment is the one your parser will pick up, that it is the only JSON in the output, or that the surrounding text is safe to show users. Confirm how it selects the fragment in the guide.
Field rules with JavaScript assertions
Use a JavaScript assertion when a schema is not enough, for example when amount must be positive, currency must come from an approved list, or status must be consistent with reason. The function parses the output and returns a pass or fail with your own logic. It proves the rules you wrote hold for the test cases. It cannot prove that your rules cover the cases that matter, so keep the rules next to the business requirement they encode.
Tool arguments with function-call checks
When the model chooses a function to call, check the function name and its arguments against the tool schema your application really exposes. This catches calls to tools that do not exist, missing required arguments and values of the wrong type. It cannot prove that the model should have called that tool at all, that the arguments are authorized for the current user, or that the tool behaves correctly when executed. Those need separate tests of the tool and its permissions.
Choosing the check order
Run the cheapest and broadest check first, then add narrower ones. A typical order is parse validity, then schema conformance, then field rules, then tool-call checks. An output that fails parsing does not need field checks, and a clear failure at the first step makes CI logs easier to read. Ask your team to document which contract each test case targets so that a later reader can see why a check exists.
Configuration options and when each fits
This table compares the four ways to validate structured output in promptfoo: is-json, contains-json, JavaScript field assertions and tool schema checks. Read each column to see what the check validates, where it fits, what it costs to run and how much upkeep it needs. The main takeaway is that schema-level checks are cheap and stable, while field-level logic should cover the business rules a schema cannot express.
| Option | What it validates | Typical use | Cost per run | Maintenance burden |
|---|---|---|---|---|
| is-json with a schema | Whole output parses as JSON and matches a supplied JSON Schema | Confirming a response is well-formed before any field checks run | Low, deterministic with no grader call | Low to moderate, schema must track the application contract |
| contains-json | A JSON object appears somewhere inside a longer text response | Models that wrap JSON in prose or markdown fences | Low, deterministic with no grader call | Low, but loose matching can hide formatting drift |
| JavaScript field assertions | Specific fields, value ranges, enums and cross-field rules | Business rules such as totals adding up or allowed status values | Low, runs local code against the parsed output | Moderate, assertion code grows with each new rule |
| Tool schema (function-call) checks | Tool name and arguments against the schema the application registers | Agents and assistants that call functions or tools | Low, deterministic when the tool definitions are loaded | Highest, must stay in sync with tool definitions in the application |
| Schema source (decision to confirm) | Inline JSON Schema, a schema file or a schema shared with application code | Inline suits small suites, shared files suit drift control | Same as the chosen check | Confirm with your engineers who owns the schema and how changes reach the tests |
Setting up a structured output eval step by step
This sequence takes a team from an empty project to a promptfoo eval that checks an LLM's JSON output at the schema level, the field level and the tool-call level. Each step ends with a check that proves the assertion behaves as intended before the next layer is added.
Install promptfoo and confirm the runtime
The engineer installing promptfoo checks that the machine runs Node.js 22.22.0 or newer, since the npm release requires that version or later, and then installs the promptfoo package in a dedicated project folder. Run the promptfoo CLI version command to confirm the install works. The step is done when the command prints a version number without errors and the project folder holds a config file location agreed with the team.
Configure the provider and structured prompt
Create a configuration file that names one model provider and a prompt that asks for a fixed JSON shape, such as an object with named keys and allowed values. Keep the prompt in its own file so it can be versioned and reviewed like code. Run a single test case through the CLI and inspect the raw output. The step is done when the model returns output you can read in the eval viewer, even if it is not yet valid JSON.
Add an is-json schema assertion
Add a test case with an is-json assertion and attach a JSON Schema that describes required keys, types and enumerations. This is the first gate: it confirms the output parses and matches the shape your downstream code expects. Consult the promptfoo JSON evaluation guide for the exact assertion syntax supported by your installed version. The step is done when the test passes on a valid output.
Verify the schema check rejects malformed output
Before trusting the assertion, feed it a deliberately broken output, such as a response with a missing required key, a wrong type or trailing prose outside the JSON. You can do this with a mock provider or a fixed test string. The assertion must fail on every broken case. The step is done when each malformed sample is marked as failed in the results, and the reason shown matches the defect you introduced.
Add field-level JavaScript assertions
Schema checks confirm shape but not meaning, so add javascript assertions that inspect individual fields. Examples include confirming that a numeric field falls within a range, that a date string is later than another field, or that an enumerated status matches the input record. Keep each function small and return a clear pass or fail value. The step is done when a test with a known-correct record passes all field checks.
Validate function calls against tool schemas
When the application expects the model to call a tool, write an assertion that parses the function call arguments and checks them against the same schema the application registers with the model. Confirm the function name matches an allowed tool and that required arguments are present. Test this with one correct call and one call that names an unknown tool or omits an argument. The step is done when the correct call passes and both faulty calls fail.
Run the full suite and review failures
Run the complete suite and open the results in the promptfoo web viewer. Group failures by assertion type to separate schema errors, field logic errors and tool-call errors. Check whether a failure comes from the prompt, the model or the assertion itself, and fix the assertion first if it was too strict. The step is done when a repeat run produces the same pass and fail pattern and each remaining failure has a recorded cause.
Confirm the suite catches a regression
Make a small deliberate change to the prompt, such as removing a required field instruction, and rerun the suite. The eval should flag the regression in the affected assertions. Ask the team to confirm which assertion type caught it, since that tells them which layer protects which behavior. The step is done when the change produces a visible failure and the change is reverted so the baseline passes again.
How a structured output passes through the assertion chain
The test case and prompt go to the model provider, which returns the raw response. That response then moves through is-json or contains-json checks, field-level JavaScript assertions and tool schema validation, all of which run locally on the machine running promptfoo. Check that the provider call is the only step that leaves your environment, and that a failure at any stage stops the chain with a clear fail result.
Local checks run on the response only after the provider call returns it.
Where objective checks stop and model graders start
Schema and field checks confirm that output is well formed, not that it is right. An is-json or contains-json assertion, a schema check or a JavaScript assertion on a field is deterministic: the same output always gets the same result, it runs in milliseconds, and it costs nothing per run. That makes these checks the right first layer of a structured output suite. They can tell you that priority is one of the allowed enum values or that amount is a number, but they cannot tell you that the enum value is the correct priority for the input, or that the amount matches the source document. A model can return perfectly valid JSON with the wrong answer in every field, and an objective check will pass it.
The boundary sits where the check needs to understand meaning. Once a field has to be judged against the input, such as whether a summary keeps the facts in the source or whether a classification matches the customer's stated intent, you move to a model-graded assertion such as llm-rubric, which is covered in the sibling topics on assertions and LLM-as-a-judge grading. Model graders cost tokens on every run, vary with the judge model and prompt, and need their own checking before you trust them. Decide field by field: keep every field you can verify with code as an objective assertion, and reserve a grader for the fields where a rule cannot express correctness. Reference-based checks for known answers, such as comparing a field to a labeled expected value, sit between the two and are often cheaper to maintain than a rubric.
Mixing both keeps the suite stable and its failures readable. Put the objective assertions first in each test so a malformed response fails fast without spending judge calls, and only run graders on outputs that already passed the structural checks. When a test fails, a schema failure points to a prompt or formatting regression, while a grader failure points to a quality regression, and the two need different fixes. Before you rely on any grader, confirm its judge model, its prompt and how its results are recorded in your environment, and review a sample of its verdicts by hand. Treat the grader as a measured component of the suite, not as ground truth.
Security and data handling for structured output evals
Structured output evals touch provider keys, sample records, tool schemas and stored results, and each one needs its own handling rule. The four cards below cover where that exposure sits when promptfoo runs against a model, and what to check before a suite leaves a developer laptop or a CI runner.
Provider credentials stay on the runner
promptfoo runs on your machine and talks directly with the model provider, so the API keys it uses live in your environment, not behind a proxy you do not control. Decide where keys are injected (a secrets manager, CI variables or a local .env file), limit each key to the model and project the suite needs, and rotate keys used by shared runners. Confirm that eval logs and exported reports do not echo request headers.
Fixtures can carry real records
Test cases for JSON extraction often start from production samples: invoices, patient intake forms or customer tickets. Those fixtures are personal data the moment they are copied into a config file or a dataset. Replace identifiers with synthetic values that keep the same structure, keep the field shapes and value ranges that matter for your assertions, and document who approved any real sample that must stay.
Schemas expose internal APIs
A JSON schema or tool definition used in a function-call assertion often describes internal endpoints, field names and permission-relevant parameters. Anyone who reads the eval config learns how your systems are wired. Keep production schemas in a private repository, review what a schema reveals before sharing a suite externally, and avoid naming internal services in descriptions that a model or a grader will see.
Results need a retention rule
Each run writes prompts, model outputs and grader reasoning, which can repeat sensitive fixture content. Set a retention period for result files and CI artifacts, decide who may open them, and delete reports that are no longer needed. Ask your team to confirm how long the CI system keeps artifacts and whether that matches your retention policy.
Self-hosted server is not a shared store
The self-hosted promptfoo app stores data in a local SQLite database that cannot be shared across replicas. It has no RBAC, horizontal scaling, built-in authentication or SSO, and the documentation does not recommend it for production use. If several people need shared results, plan access control in front of the server or evaluate the Enterprise edition, and confirm this with promptfoo before choosing a design.
Self-hosting docsQuestions buyers ask before testing structured outputs
What does structured output testing cost and what drives the effort?
The cost is the people time spent writing and maintaining assertions, plus any model calls the suite makes. Effort rises with the number of schemas, the number of tool functions the model can call, the number of providers you compare, and whether the suite must run in CI on every change. Deterministic checks such as is-json and schema validation are cheap to run, so most of the effort is in collecting representative outputs and agreeing what counts as a valid field. DeployOpen does not publish prices. A quote follows from a written scope that lists schemas, tool functions, providers and CI targets.
How long does a first structured output suite take to build?
The duration depends on how many schemas and tool definitions are in scope and how many sample outputs exist to build test cases from. A team with one schema, a handful of sample responses and one provider can usually reach a first working suite faster than a team covering many tools across several models. Ask any partner to state the duration in the written scope, with the schemas, tools, providers and CI integration it covers. DeployOpen agrees timelines in writing before work starts, so the figure is tied to that scope rather than to a generic estimate.
What must our team supply before testing starts?
Most of the input comes from your own team. Expect to provide:
- The JSON schemas or type definitions the application expects, including required fields and allowed values
- Sample prompts and representative model outputs, including edge cases and past failures
- Tool definitions for any function-call flows, with argument schemas and the expected tool selection
- Access to the model providers under test, with API keys or endpoints that the test environment can reach
- The CI system and branch rules where the suite should run
Missing schemas are the most common delay, because field-level assertions are only as good as the rules behind them.
Can an internal team build structured output tests without a partner?
Yes, a team that already maintains the application and its schemas can write is-json checks, schema assertions and JavaScript field checks with the promptfoo documentation as a guide. The work is mostly careful test design: deciding which fields are required, which values must be exact, and which failures should block a release. Teams with little experience with evaluation tooling often ask for help on the first suite, then maintain it internally. A partner is most useful when many schemas, tool functions and providers must be covered quickly, or when the suite must be reviewed by security or compliance teams.
Does the promptfoo Community edition cover structured output checks?
Yes. According to the promptfoo pricing page, the Community edition is free and includes all LLM evaluation features, all model providers and integrations, and red teaming with 10k probes per month, with community support. The is-json, schema and JavaScript assertions used for structured outputs are evaluation features, so they fall within that inclusion. Confirm the current assertion list against the promptfoo documentation for evaluating JSON outputs before you rely on a specific check in your environment.
What breaks most often in structured output tests?
The most common failures are not syntax errors but drift between the schema and the application. Typical problems include:
- A model returns valid JSON with a renamed or missing field, which a plain JSON check accepts
- Optional fields are treated as required, or the reverse, so tests pass or fail for the wrong reason
- Function-call arguments match the tool schema on one model but not on another
- Test outputs are copied from an old prompt version and no longer reflect production
Keep schemas under version control with the tests and refresh sample outputs when prompts change.
Do structured output tests send our data to outside services?
That depends on where the model under test runs, not on the test tool itself. promptfoo documentation states that the software runs completely locally, so evals run on your machine and talk directly with the LLM. Your sample prompts and outputs are sent to whichever provider or endpoint you configure. If those samples contain personal or regulated data, use a model endpoint you control or anonymized samples, and confirm the data handling terms with each provider.
How DeployOpen scopes structured output testing
DeployOpen scopes structured output testing in writing before any suite is built, because the scope decides which schemas, tool definitions, providers and CI steps the suite must cover. The scoping conversation starts with an inventory: every JSON shape the application expects, every function-call schema the model receives, and every provider or gateway the outputs pass through. For each one, the scope records the validation method (schema check, field-level JavaScript assertion or tool-schema validation), the owner of the schema, and how a schema change will reach the test suite. Your team should expect the written scope to name the promptfoo configuration files, test data sources and the CI system that runs the suite, along with the acceptance criteria for each deliverable.
The work that follows the scope is the build, documentation and handover. DeployOpen wires the suite into your CI pipeline, documents how to add a schema or tool definition, and records which assertions are deterministic and which need a model grader, so reviewers can see why each check exists. Deliverables are agreed in the same written scope, so the team receives a defined set of configs, test cases, run instructions and a short runbook rather than an open-ended engagement. Ask any provider for the exact deliverables, the acceptance steps and who signs off on them before work starts. Because model versions, provider APIs and application schemas change, ongoing retesting can be arranged as a retainer, and that agreement should also be written down before handover.

