How to Fix AI JSON Hallucinations ("Additional text encountered" error)

Hey Zenphi Community! ๐Ÿš€

If you are using Gemini, ChatGPT, or Claude in your Zenphi flows, you have likely toggled the "Output as JSON" option to extract clean data. The plan is always simple: take that JSON output, pass it into the "Convert JSON to Object" action against your specific schema, and use those neat tokens later in your flow.

But what happens when your prompt gets too heavy, or the AI has a glitch due to high traffic?

The AI fails to follow formatting rules perfectly. Instead of giving you raw JSON, it gets "creative" and adds markdown backticks, random quotes, or gets stuck in a loop and spits out trailing garbage at the end of your data.

The Problem & The Error

When the AI experiences these bugs, it outputs something messy like this:

JSON


```json
{
  "client_name": "John Doe",
  "status": "Approved"
}": null
}
}
null
}

Because Zenphi's Convert JSON to Object action expects an exact schema and perfect JSON format, hitting those extra markdown backticks or the trailing }": null garbage causes your flow to fault with this frustrating error:

Error for Action: Convert JSON to Object. Additional text encountered after finished reading JSON content.

Fortunately, we have two excellent ways to solve this and make your flows completely bulletproof!

Solution 1: Adjust Your AI Temperature

Inside your AI action settings (like Gemini or OpenAI), look under the Advanced Settings section. By default, the Temperature is usually set to 1.

Temperature controls the AI's randomness and creativity. While creativity is great for writing emails, it is terrible for structured data extraction!

  • The Fix: Lower the temperature to 0 or 0.2. This forces the AI to be highly analytical and robotic, drastically reducing its tendency to hallucinate extra characters.


Solution 2: Drop in our "Bulletproof JSON Cleanup" Mini-Flow

Sometimes, even with a low temperature, an AI engine can still have a random bug or traffic spike that messes up the format.

To protect your flow from crashing, we built a dedicated Cleanup Mini-Flow. It is just a simple "Execute Script" actionโ€”all you need to do is pass your AI result into the script as the input, and then pass its clean output directly into your Convert step!

Here is exactly what the script does behind the scenes to save your flow:

  • Strips Markdown: Automatically removes any ```json or ``` wrappers the AI might have added.

  • Tracks the Object: Scans the text to find the exact start and end of your JSON object.

  • Trashes the Garbage: Slices off any hallucinated trailing text, extra null values, or stray brackets the moment the JSON object safely closes.

๐Ÿ‘‰ [Click here to download the JSON Cleanup Mini-Flow and see how to set it up!]

Implementing these two steps will keep your automation running smoothly, even when the AI models are having a rough day.

Let us know if this helps save your flows from crashing, or if you've found other creative ways to handle AI formatting issues!

Happy automating! ๐Ÿฆพ