Learn how to generate structured JSON output using ComputeJSON
Generating JSON with LLMs is a useful technique that is broadly applicable to many tasks – from data extraction, to data wrangling, to function calling. You'll find that it's also very useful in the context of chained workflows. We believe JSON generation is a core primitive for AI engineering, so we've optimized the heck out of it – learn more on our blog (opens in a new tab).
1. Define a schema
First, we define a schema (and convert it to JSON schema (opens in a new tab) format).
- In TypeScript, we recommend using zod (opens in a new tab) and the zod-to-json-schema (opens in a new tab) package.
- In Python, we recommend using Pydantic (opens in a new tab) and the
model_json_schema()
method.
2. Generate JSON
To generate JSON, pass a JSON schema to ComputeJSON
along with a prompt.
To access the output of ComputeJSON in subsequent nodes, use future.json_object
.
Use case: Data wrangling
JSON generation can be used to extract structured data from unstructured sources, or transform data from one shape to another. In the following example, we use ComputeJSON
to transform a JSON object to a semantically equivalent object in a different shape.