Knowledge tree
Offensive Security
Shells
File Transfer
Structured Outputs
A valid JSON value can still be useless to an application. {"file":"src/app.ts","line":"12"} parses, but line is not an integer. There are several ways to request and check a response’s shape:
Four different mechanisms
| Mechanism | Valid JSON | Schema adherence | When errors surface |
|---|---|---|---|
| Ask for JSON in the prompt | Not guaranteed | Not guaranteed | During parsing or validation |
| JSON mode | For an accepted complete response | No | When validating the value |
| Schema-constrained generation | For an accepted complete response, within the supported subset | Yes, for that subset | Before or during generation if the schema is unsupported |
| Post-generation validation | Only if parsing succeeds | Only if validation succeeds | After generation |
The prompt is an instruction. JSON mode and constrained generation are API controls. A post-generation validator runs in the application and can also follow either API mechanism. None removes the need to handle incomplete responses, model refusals, or transport errors.
Generation and validation
Schema-constrained generation can limit the tokens available at each step so the response stays within an allowed grammar. Post-generation validation checks a response that has already been generated. A provider may support only a subset of JSON Schema during generation even when a general validator accepts more constraints.
When a value arrives, the application should parse and validate it at its own boundary. On failure it can reject the value or retry with a precise error. Treating a partial object as complete hides the failure.
Structure and meaning
Consider {"file":"src/app.ts","line":12}. It may match the schema while line 12 does not exist, contains unrelated code, or lies outside the task’s scope. Check structure against the schema and meaning against the file and task. These are practical distinctions, not names for universal guarantees.
For an action, there are further decisions:
- Can the value be parsed?
- Does it satisfy the schema?
- Does it refer to a real, relevant thing?
- Is the requested use allowed and authorized?
- Did execution actually succeed?
An enum or a pattern can narrow values, but cannot establish user intent or grant authority. Those decisions require the application’s own data and policies.
Tool arguments
Tool calls use the same idea for a different destination. A model may produce structured arguments for read_file(path="src/app.ts"). The application validates the arguments and decides whether to execute the tool. Some APIs support strict schema adherence for tool arguments separately from structured final answers.
The schema makes the call easier to parse. The application must still check the path, scope, and authorization before execution.