JSON Schema can be used to define the fields and value types expected in an LLM response. OpenAI Structured Outputs constrains a completed, non-refusal model output to the supplied JSON Schema. Required fields and enum values in a supported schema are enforced on the output structure.
According to the JSON Schema organization, the schema itself is also written as JSON. A schema is not a computer program but a declarative format that describes the structure of other data. A schema can express which fields are expected in a person record and how their values are represented.
Defining the output fields
In the JSON Schema organization's person record example, the outermost structure is an object whose type value is object. Under properties, the first_name and last_name fields are defined as string. In the same example, the birthday field is also of type string, and its format value is given as date.
The address field in the example contains another object. This inner object has its own properties section containing the street_address, city, state and country fields. Each of them is of type string.
Anthropic lists object, array, string, integer, number, boolean and null among the basic types supported for structured outputs. In addition, enum and const can be used. Anthropic limits the values in these two features to string, number, boolean or null.
In Anthropic's example below, properties defines the fields, and required lists the names of the required fields. The same example uses additionalProperties: false. Anthropic states that for structured outputs this value must be false on objects.
Field order and requirement
Anthropic's schema defines the notes, name, email and age fields. name and email appear in the required list. The other two fields are not in that list. The type is given as string for notes, name and email, and as integer for age:
{
"type": "object",
"properties": {
"notes": { "type": "string" },
"name": { "type": "string" },
"email": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name", "email"],
"additionalProperties": false
}
According to Anthropic, when structured outputs are used, object fields keep the order in which they are defined in the schema. There is one exception: required fields come first, followed by optional fields. Each group follows the schema order within itself.
In this example, the output order is name and email first, then notes and age. Anthropic advises marking all fields as required when field order matters to the application, or accounting for this reordering in the parsing logic.
According to Anthropic, because additionalProperties is false, Claude can return only the listed keys. The keys are listed in properties or as the class or schema fields passed to the SDK. If properties is empty or absent, the API accepts the object, but Claude can return only {} for that object.
When keys are not known in advance, Anthropic shows the use of an array in which each element carries a key and a value. In the example, items is defined as an object. The object's properties section contains the key and value fields, both of type string. Both fields are in the required list, and the object's additionalProperties is set to false.
According to Anthropic, map, dictionary or record types do not list their keys. The Python and TypeScript SDKs send them as an object with empty properties. The Java and Ruby SDKs, however, raise an error in this case.
Attaching the schema to a request
In Anthropic's JSON outputs flow, the desired structure is first defined as a JSON Schema or as a type in the language being used. The request then carries the schema in output_config.format with type: "json_schema". According to Anthropic, SDK helpers can configure this setting.
Claude returns JSON matching the schema in the text content block of the response. SDK helpers can parse the response into the defined type. Anthropic gives extracting data from text or images, generating structured reports and formatting API responses as example uses of this flow.
In Anthropic's Python SDK flow, a Pydantic model can be passed to a client.messages.parse() call as output_format. The SDK transforms the model's schema and sends it in output_config.format. It then validates the response and returns the parsed model in parsed_output.
Anthropic also uses the output_config.format field for a JSON Schema taken from a file or from an OpenAPI spec. The same field can be used to pass a schema built in code at runtime into the request. JSON outputs and strict tool use can be used together in the same request.