Skip to content

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 ​

  1. Download structured-output-flow.json, or take it from the samples folder on GitHub.
  2. In the studio's workflow editor, import the file (see import and export).
  3. Open the LLM Call node and point it at a model you have: pull llama3.1 into Ollama, or switch the provider and store its key as a secret. In Docker, localhost is the container: use http://host.docker.internal:11434/v1 for Ollama on the host. On Linux that name resolves only if the api service has extra_hosts: ["host.docker.internal:host-gateway"], which the repo's docker-compose.yml does not set. Save.
  4. Run it, then open the run and click Output Parser to see parsed, retries and parserTrace.
  5. Change the text on Text Input and run it again.

How it works ​

Diagram of the Structured Output Parser Flow workflowRuns Text Input, then System Prompt, then LLM Call, then Output Parser, then Output.Text Input (text_input)Text Inputtext_inputSystem Prompt (system_prompt)System Promptsystem_promptLLM Call (llm_call)LLM Callllm_callOutput Parser (output_parser)Output Parseroutput_parserOutput (output)Outputoutput
Solid arrows are the order the steps run in. Dashed lines attach a chat model, memory, tools or workers to an agent; the agent uses them while it runs.
NodeTypeRole
Text Inputtext_inputStep in the run
System Promptsystem_promptStep in the run
LLM Callllm_callStep in the run
Output Parseroutput_parserStep in the run
OutputoutputStep 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 answeredResult
{"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 retrySuccess, retries: 1
"sentiment": "very happy" every timeRun fails: Output Parser failed after 2 retries: Field 'sentiment' value 'very happy' not in enum: positive, negative, neutral
"The sentiment is clearly positive." every timeRun 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 simple key: value lines. When parsing or the schema check still fails after the retries, the run does not fail: the parser returns parsed: null and a warning. We saw this with the prose answer above, so check for null before 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 as items and count.
  • 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.

Further reading ​

This website (not the L2M product) uses Google Analytics, only after you accept cookies. Cookie settings