I’m seeing an intermittent bug whereby the input to my Structured Output Parser has the “output” JSON key inside another “output” JSON key, which of course fails to parse. I can’t find a pattern. I can run two exact same requests after one another, and one response will be correct, and another will be double wrapped.
Hey @graham
This is one of the most frustrating things I’ve run into when building AI dependent workflows. Proper formatting of json.
I have few approaches I take to reduce the chance of this happening but so far I haven’t found a 100% proof way to overcome it completely.
If you have json formatting instructions in your prompt you might want to try and remove them. The structured output parser appends json format instructions to your prompt based on the example/schema in the background so instructions added to the prompt might actually confuse the LLM.
You might want to add an auto-fixing parser (I wouldn’t put too much hope on it fixing issues though… i’ve had mixed results with these things)
You can add error handling flow in the AI node settings and try to fix the json with addtional AI prompt.
If nothing else helps make sure you add error handling and send yourself a message or an email. this way you can quickly detect failed workflows and try to rerun them manually to fix the issue.
I have no idea what’s happening under the hood, but the problem seems to be that the response from the model can be in different places. Even though the model returns JSON, sometimes it’s interpreted as free text and re-wrapped.
This thread has some good advice:
Namely, instead of attaching the Structured Output Parser to the Agent node, pipe the Agent’s output to a Basic LLM Chain node and connect the Structured Output Parser there instead. This seems to be more reliable (so far!).
I have also done that in some workflows and its probably the most reliable way to generate text and output a structured object for now. Sadly it consumes much more credits because you process everything twice, but AI agents are much more reliable if you give them just one straightforward task. I.e. generate text, format text etc.
After wrestling with the same issue, I finally figured out what was happening.
Because I was using Postgres as the memory for the AI Agent node, the agent was saving each message to the database already containing an output field. The next time the agent ran, it would fetch that message—with its existing output—from memory and then append yet another output generated by the AI model. The cycle kept repeating.
To fix it, I forced the AI Agent to produce its output as plain text instead of JSON. That way, no JSON gets stored in Postgres. Then, only after I receive the plain‑text output do I run it through another LLM and convert it to JSON at that stage. The end goal is to avoid storing any JSON in the AI Agent node’s memory.
Not sure if this will work for everyone but my colleague just removed the Structured Output Parser and added a Code block behind the AI Agent with the following code
// Extract and parse only the required keys from the JSON string in 'output'
return items.map(item => {
const parsed = JSON.parse(item.json.output);
return {
json: {
example_param_1: parsed.example_param_1,
example_param_2: parsed.example_param_2
}
};
});
I’ve found making any output that matters a tool call the most reliable (response/feedback to human, agent to agent, saving state/thread before ending/sleeping, looping back to continue on, etc). Main line output is often the end of the line for most workflows, content wise … just an empty output or just a \n or two for the most reliable workflows.
I’ll often toss stuff there for observability or debugging but not for additional processing.
I had the issue surface out of nowhere, my workflow worked just fine for a while and it stopped working. The workflow was per diagram but output parser’s expectation or memory of AI agent node kept changing the output and gemini 1.5 flash would return the output wrapped and not clean json, thought the system prompt clearly stated clean/raw json only and nothing else. when I disconnected the output parser, gemini output was better. it did not have those ‘’'json or action/parser. so finally got rid of output parser and added a code node after ai agent node and then put the java script to conditionally check if non json or wrapped output come back and had java script generate clean json output.