How to Fix 'output parsing error when scaling' in AutoGen (TypeScript)
When AutoGen throws an output parsing error when scaling, it usually means the agent got a response that did not match the structured format the runtime expected. In TypeScript, this often shows up after you add more tools, more agents, or longer conversations and the model starts returning extra text, malformed JSON, or a plain-English answer where AutoGen expected a schema.
This is usually not a “scaling” infrastructure problem. It is a parsing problem triggered by a prompt, tool, or response-format mismatch that only becomes visible once your workflow gets more complex.
The Most Common Cause
The #1 cause is using a structured output path without forcing the model to return valid JSON every time.
In AutoGen TypeScript, this typically happens when you use AssistantAgent with tool calling or structured response handling, but your prompt still allows free-form text. Once the agent returns something like:
Sure — here's the result:
{"status":"ok","count":3}
the parser fails because it expected pure JSON.
Broken vs fixed
| Broken pattern | Fixed pattern |
|---|---|
| Lets the model add prose around JSON | Forces strict JSON-only output |
| No explicit schema constraints | Uses a typed result contract |
| Parser receives mixed text + JSON | Parser receives valid structured data |
// BROKEN
import { AssistantAgent } from "@autogen/agent";
const agent = new AssistantAgent({
name: "report_agent",
modelClient,
systemMessage: "Return the result as JSON.",
});
const result = await agent.run("Summarize these claims and return JSON.");
// Often fails later with something like:
// Error: output parsing error when scaling
// or
// Error: Failed to parse model output as JSON
// FIXED
import { AssistantAgent } from "@autogen/agent";
const agent = new AssistantAgent({
name: "report_agent",
modelClient,
systemMessage: [
"Return ONLY valid JSON.",
"No markdown.",
"No explanation.",
'Format: {"status":"string","count":"number"}',
].join("\n"),
});
const result = await agent.run("Summarize these claims.");
If you are using structured outputs, make the contract explicit in both the system message and the consuming code. Do not rely on “the model knows what you mean.”
Other Possible Causes
1. Tool output is not serializable
If a tool returns undefined, a class instance, circular data, or raw objects with unsupported fields, AutoGen may fail while trying to parse or forward the result.
// BAD
const getClaimsTool = async () => {
return {
claims: dbClient.query("SELECT * FROM claims"), // Promise / non-serializable
};
};
// GOOD
const getClaimsTool = async () => {
const rows = await dbClient.query("SELECT * FROM claims");
return rows.map((r) => ({
id: r.id,
status: r.status,
}));
};
2. Your tool schema does not match what the agent sends
This happens when your function expects claimId, but the model sends claim_id, or when a required field is missing.
// BAD
const toolSchema = {
type: "object",
properties: {
claimId: { type: "string" },
},
required: ["claimId"],
};
// Model sends:
// {"claim_id":"123"}
Fix it by aligning names exactly and keeping schemas narrow.
// GOOD
const toolSchema = {
type: "object",
properties: {
claimId: { type: "string" },
},
required: ["claimId"],
};
3. Context got too large and the model started drifting
When scaling conversations, long histories can push important instructions out of context. The model then starts returning sloppy output that no longer matches your parser expectations.
// Bad pattern: keep appending everything forever
messages.push(...allPreviousTurns);
Use trimming or summarization before you hit token pressure.
// Better pattern: keep only recent turns + summary
messages = [summaryMessage, ...recentTurns];
4. You are mixing free-form chat and structured extraction in one agent
A single AssistantAgent used for both conversation and extraction is fragile. If one turn asks for analysis and another expects strict JSON, you will eventually get malformed output.
// BAD: one agent doing everything
const analystAgent = new AssistantAgent({
name: "analyst",
modelClient,
});
// GOOD: separate agents for separate contracts
const chatAgent = new AssistantAgent({ name: "chat", modelClient });
const extractorAgent = new AssistantAgent({
name: "extractor",
modelClient,
systemMessage: "Return ONLY JSON.",
});
How to Debug It
- •
Log the raw assistant message before parsing
- •Inspect exactly what came back from the model.
- •If you see prose around JSON, that is your culprit.
- •If you see invalid JSON syntax, fix formatting first.
- •
Disable tools temporarily
- •Run the same prompt with no function calls.
- •If the error disappears, your tool schema or tool return value is broken.
- •This isolates parser issues from agent reasoning issues.
- •
Reduce the conversation to one turn
- •Remove history and rerun with just system prompt + user prompt.
- •If it works in isolation but fails in production, context growth is causing drift.
- •Summarization or message trimming usually fixes it.
- •
Validate every tool response
- •Make sure each tool returns plain JSON-serializable data.
- •No classes, no promises, no
Map, noSet, no circular references. - •Log
JSON.stringify(toolResult)during development.
Prevention
- •Keep structured-output agents strict:
- •“Return ONLY valid JSON” should be in both your prompt and your tests.
- •Separate conversational agents from extraction agents:
- •One agent for chat, one agent for schema-bound output.
- •Add contract tests around tool input/output:
- •Validate payloads before they reach
AssistantAgentor your parser.
- •Validate payloads before they reach
- •Trim context aggressively:
- •Summarize old turns instead of letting every message accumulate forever.
If you are seeing output parsing error when scaling in AutoGen TypeScript, start with the response format first. In most cases, the runtime is fine — your prompt contract is not.
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