How to Fix 'JSON parsing error in production' in AutoGen (Python)
What this error means
If you’re seeing JSON parsing error in production in AutoGen, it usually means one agent produced output that another part of your pipeline expected to be valid JSON, but it wasn’t. In practice, this shows up when you use structured outputs, tool calls, or custom message parsing and the model returns extra text, malformed JSON, or a schema mismatch.
The failure often appears only in production because your prompts, model settings, or message history are slightly different from local runs. The usual stack trace points at json.loads(...), pydantic.ValidationError, or an AutoGen response parser failing inside AssistantAgent or ConversableAgent.
The Most Common Cause
The #1 cause is asking the model for JSON but not enforcing JSON-only output. The model adds markdown fences, commentary, trailing commas, or plain-English text, and your parser blows up.
Here’s the broken pattern versus the fixed pattern.
| Broken | Fixed |
|---|---|
| Prompt says “return JSON” but no strict formatting | Prompt forces raw JSON only |
Parsing with json.loads(response.content) directly | Validate and extract clean content before parsing |
| No schema guardrail | Use structured output or tool/function calling |
# BROKEN
import json
from autogen import AssistantAgent
agent = AssistantAgent(
name="assistant",
llm_config={"config_list": [{"model": "gpt-4o-mini", "api_key": "..." }]}
)
result = agent.generate_reply(
messages=[{"role": "user", "content": "Return a JSON object with keys: status, amount"}]
)
# Common failure:
# json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
data = json.loads(result["content"])
# FIXED
import json
from autogen import AssistantAgent
agent = AssistantAgent(
name="assistant",
llm_config={
"config_list": [{"model": "gpt-4o-mini", "api_key": "..."}],
"temperature": 0,
}
)
messages = [
{
"role": "system",
"content": (
"Return ONLY valid JSON. "
"No markdown fences. No explanation. "
'Schema: {"status": string, "amount": number}'
),
},
{"role": "user", "content": "Create the payload."},
]
result = agent.generate_reply(messages=messages)
raw = result["content"].strip()
data = json.loads(raw)
If you’re on a newer AutoGen setup that supports structured outputs or tool calls, prefer that over free-form JSON text. Free-form prompting is where most production failures start.
Other Possible Causes
1) Markdown code fences around JSON
Models often return:
{
"status": "ok"
}
That is valid for humans, not for json.loads() unless you strip the fences first.
raw = result["content"]
# Fails if raw includes ```json ... ```
data = json.loads(raw)
Fix it by stripping fences or forcing strict output in the system message.
clean = raw.replace("```json", "").replace("```", "").strip()
data = json.loads(clean)
2) Tool output is being re-parsed as model output
If a function/tool returns a Python dict but gets serialized into a string with single quotes, AutoGen may pass something like {'status': 'ok'} downstream. That is not JSON.
# BAD tool return
def get_status():
return {"status": "ok"} # later stringified incorrectly as "{'status': 'ok'}"
Return proper JSON serialization if your pipeline expects text:
import json
def get_status():
return json.dumps({"status": "ok"})
3) Conversation history polluted the expected payload
In ConversableAgent, older messages can leak into later turns. If your parser assumes the last assistant message is pure JSON, but the agent also included reasoning text earlier in the conversation, parsing fails.
# BAD: parsing arbitrary assistant content from a long chat history
last_msg = chat_result.chat_history[-1]["content"]
payload = json.loads(last_msg)
Use a dedicated response field or isolate the structured turn:
structured_turn = next(
msg for msg in reversed(chat_result.chat_history)
if msg["role"] == "assistant" and msg["content"].strip().startswith("{")
)
payload = json.loads(structured_turn["content"])
4) Temperature too high for structured output
At higher temperature values, models drift more often. You’ll see valid-looking prose instead of strict JSON.
llm_config = {
"config_list": [{"model": "gpt-4o-mini", "api_key": "..."}],
"temperature": 0.8,
}
For production parsers, set temperature low:
llm_config = {
"config_list": [{"model": "gpt-4o-mini", "api_key": "..."}],
"temperature": 0,
}
How to Debug It
- •
Log the exact raw assistant content
- •Don’t log the parsed object.
- •Log
repr(result["content"])so you can see fences, whitespace, and hidden characters.
- •
Check whether the failure is before or after AutoGen
- •If you see
JSONDecodeError, the model output is malformed. - •If you see
pydantic.ValidationError, the JSON parsed but didn’t match your schema. - •If you see AutoGen-specific message handling errors inside
AssistantAgentorConversableAgent, inspect message formatting first.
- •If you see
- •
Replay with temperature set to zero
- •Run the same prompt locally with identical config.
- •If it works at
temperature=0but fails in prod at higher values, you’ve found drift.
- •
Validate against a schema before touching business logic
- •Use Pydantic or explicit key checks.
- •Example:
from pydantic import BaseModel
class PaymentPayload(BaseModel):
status: str
amount: float
payload = PaymentPayload.model_validate_json(raw)
If this fails consistently, your prompt contract is wrong, not your downstream code.
Prevention
- •Use structured outputs where possible instead of asking for “JSON” in plain English.
- •Keep temperature at
0for any agent turn that feeds a parser. - •Add a hard validation layer:
- •strip markdown fences
- •parse JSON
- •validate schema
- •reject anything else
- •Write one integration test that stores real assistant output and parses it exactly like production does.
The main lesson: don’t trust “looks like JSON.” In AutoGen pipelines, treat every model response as untrusted input until it passes parsing and schema validation.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit