How to Turn an AI Agent's Python Output into Valid JSON
LangChain, CrewAI and n8n agents often print a Python dict instead of JSON: True, None, single quotes, trailing commas. How to convert it with the repair tool, step by step, what it changes, and its limits.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →Your agent finished its run and logged the result. You copy it into json.loads() and get:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 3 (char 4)
The output looks like JSON, but it is not. It is the repr() of a Python dictionary — what print(result) writes when result is a dict. This guide shows how to turn a long output like that into valid JSON in a few seconds with the JSON repair tool, what exactly gets changed, and where the tool stops.
Why agent output comes out as Python
Most agent frameworks keep their state as Python objects. When a step logs str(state) or print(response), Python writes its own literal syntax, not JSON:
| Python writes | JSON needs |
|---|---|
True / False | true / false |
None | null |
'single quotes' | "double quotes" |
{'a': 1,} (trailing comma) | {"a": 1} |
# a comment | no comments at all |
Any one of these makes JSON.parse or json.loads fail. A 150-line agent trace usually has all five, dozens of times.
Step by step
- Copy the whole output, including the parts you think are fine. A typical LangChain run log looks like this:
{
'run_id': 'run_8f3a21',
'agent': 'research_assistant',
'success': True,
'error': None,
'steps': [
{
'step': 1,
'tool': 'web_search',
'ok': True,
'cached': True, # from the tool cache
},
],
'tags': ['finance', 'q3', 'report',],
}
- Paste it into the repair tool and tap Fix My JSON. The repair runs in your browser; the text is not uploaded.
- Read the report. Each kind of change is listed: Python literals converted (
True→true,None→null), single-quoted strings converted to double quotes, trailing commas removed, comments stripped. Check that nothing in the list surprises you. - Open Diff View to see every change in place, line by line, against your original.
- Copy the fixed JSON, or download it as a
.jsonfile.
The result for the snippet above:
{
"run_id": "run_8f3a21",
"agent": "research_assistant",
"success": true,
"error": null,
"steps": [
{ "step": 1, "tool": "web_search", "ok": true, "cached": true }
],
"tags": ["finance", "q3", "report"]
}
Fix it at the source too
The tool is the fast way to rescue output you already have. In your own code, the clean fix is to serialise with the JSON library instead of printing the object:
import json
print(json.dumps(result, indent=2, default=str)) # JSON, not repr()
If you only have the printed text and you trust where it came from, Python can read it back with ast.literal_eval(text) and then json.dumps it. That fails on comments that are not Python comments and on anything that is not a literal.
Limitations — what the tool will not do
- It cannot know what you meant by an ambiguous value. A string such as
'it\'s'or a Windows path with backslashes is converted by fixed rules; check those values in Diff View. - Non-literal Python —
datetime(2026, 10, 21),Decimal('1.5'),<Object at 0x…>— has no JSON form. The tool cannot invent one; replace those values or serialise them withdefault=strat the source. - Very large logs. A megabyte repairs in a fraction of a second; for files in the tens of megabytes, use a script — the browser has to hold the whole text.
- Integers beyond 2^53 (64-bit IDs) are kept as written in the output text, but any JavaScript code that later calls
JSON.parseon them will round them. Store such IDs as strings. - It does not validate against your schema. Valid JSON is not the same as the shape your next step expects; validate with your schema after repairing — see the JSON Schema validation guide.
Related
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →Related Articles