chat: the default choice for most LLM chat completion use casesjson: a specialized function type when your goal is generating structured outputs
Generate structured outputs with a static schema
Let’s create a JSON function for one of its typical use cases: data extraction.1
Configure your JSON function
Create a configuration file that defines your JSON function with the output schema and JSON mode.
If you don’t specify an The field
output_schema, the gateway will default to accepting any valid JSON output.tensorzero.toml
json_mode can be one of the following: off, on, strict, or implicit_tool.
The implicit_tool strategy is a custom TensorZero implementation that leverages tool use under the hood for generating JSON.
See Configuration Reference for details.2
Configure your output schema
If you choose to specify a schema, place it in the relevant file:
output_schema.json
3
Create your prompt template
Create a template that instructs the model to extract the information you need.
system_template.minijinja
4
Call the function
- Python
- Python (OpenAI SDK)
- Node (OpenAI SDK)
- HTTP
When using the TensorZero SDK, the response will include
raw and parsed values.
The parsed field contains the validated JSON object.
If the output doesn’t match the schema or isn’t valid JSON, parsed will be None and you can fall back to the raw string output.Sample Response
Sample Response
Generate structured outputs with a dynamic schema
While we recommend specifying a fixed schema whenever possible, you can provide the output schema dynamically at inference time if your use case demands it. Seeoutput_schema in the Inference API Reference or response_format in the Inference (OpenAI) API Reference.
You can also override json_mode at inference time if necessary.