Extract structured JSON from text with an LLM
What you get: A workflow that asks a model for a sentiment label and returns it as checked JSON: {"name": ..., "sentiment": "positive"}.
Needs: A model: local Ollama as shipped, or a provider key · Time: 10 minutes · Nodes: 5
A System Prompt tells the model to answer only with JSON that has name and sentiment fields. The LLM Call answers the text from Text Input, and the Output Parser reads the answer as JSON and checks it against a schema: both fields required, sentiment one of positive, negative or neutral. If the check fails, the parser asks the same model to fix its answer, up to 2 times, and fails the run if it still does not match. As shipped, the model is llama3.1 on a local Ollama server (http://localhost:11434/v1).
Steps
- Download structured-output-flow.json, or take it from the samples folder on GitHub.
- In the studio's workflow editor, import the file (see import and export).
- Open the LLM Call node and point it at a model you have: pull
llama3.1into Ollama, or switch the provider and store its key as a secret. In Docker,localhostis the container: usehttp://host.docker.internal:11434/v1for Ollama on the host. On Linux that name resolves only if theapiservice hasextra_hosts: ["host.docker.internal:host-gateway"], which the repo'sdocker-compose.ymldoes not set. Save. - Run it, then open the run and click Output Parser to see
parsed,retriesandparserTrace. - Change the text on Text Input and run it again.
How it works
| Node | Type | Role |
|---|---|---|
| Text Input | text_input | Step in the run |
| System Prompt | system_prompt | Step in the run |
| LLM Call | llm_call | Step in the run |
| Output Parser | output_parser | Step in the run |
| Output | output | Step in the run |
5 node types. Each type links to its config fields in the node reference.
What a run does
We did not run the sample against Ollama. We ran it with the echo model and with a small local stand-in server that returns fixed answers, to see each parser path:
| The model answered | Result |
|---|---|
{"name": "product review", "sentiment": "positive"} | Success, retries: 0, strategy json_parse |
The same JSON inside a ```json block, after "Here is the result:" | Success, retries: 0 |
"sentiment": "very happy", then valid JSON on the retry | Success, retries: 1 |
"sentiment": "very happy" every time | Run fails: Output Parser failed after 2 retries: Field 'sentiment' value 'very happy' not in enum: positive, negative, neutral |
| "The sentiment is clearly positive." every time | Run fails: Output Parser failed after 2 retries: Invalid JSON (strict): ... |
Each retry is a separate call to the same model, with the system prompt "You are a JSON formatting assistant. Output ONLY valid JSON." and a message that names the error, the schema and the original answer. Retries cost tokens like any other call.
With echo, the run fails, and it is worth knowing why. Echo's answer is the prompt, not JSON, so the parser retries, and echo repeats the correction message back. That message contains the schema, which is a JSON object, so the parser finds it, and rejects it: Missing required field: name. The parser looks for JSON anywhere in the text, so prose that quotes JSON can parse to something you did not mean.
On success, the Output node's {{parsed}} renders the object as a JSON string: {"name":"product review","sentiment":"positive"}.
What the schema check covers
In json_schema mode the parser checks the top level of the object only: each field in required is present, a field typed string, number or boolean has that type, and a field with enum holds one of the listed values. Nested objects, arrays, integer and formats are not checked. For a full JSON Schema check, add an Output Guardrail node with the Matches a JSON Schema check and a schema, or an answer schema output check. Both are optional.
The LLM Call node does not ask the provider for a JSON mode; the JSON comes from the system prompt. That is why the parser and its retries are there.
When parsing fails
Set Parsing Strictness (parsingMode) on the parser:
strict(default): the answer, a fenced block in it, or the first complete object in it must be valid JSON.lenient: also repairs common slips such as single quotes, trailing commas, unquoted keys and Python literals.anything_goes: also reads simplekey: valuelines. When parsing or the schema check still fails after the retries, the run does not fail: the parser returnsparsed: nulland awarning. We saw this with the prose answer above, so check fornullbefore you use the result.
parserTrace in the node output lists each strategy tried and why it failed. See Output Parser fails with Invalid JSON.
Use the result downstream
Later nodes read the fields by path. With the Output template set to Sentiment: followed by {{parsed.sentiment}}, our run returned Sentiment: positive. To route on a field, put an If node after the parser with a condition such as {{ parsed.sentiment === "negative" }}: see branch a workflow on an LLM result.
Make it yours
- Edit the schema on the parser (a JSON string in JSON Schema (Required for strict mode)) and the system prompt together: name the same fields and values in both.
- Set Parser Mode to Item List (
item_list) for a plain list, one item per line, returned asitemsandcount. - Set LLM Max Auto-Retries (
maxRetries) to 1 to limit the extra calls. A value of 0 is read as the default, 2, so retries cannot be turned off this way.
Make it safer
These controls are optional; the recipe runs without them.
- Treat parsed fields as untrusted input, especially when the source text came from outside: validate them before they reach a database, a shell or a URL.
- Cap model spend, retries included, with a budget.