How to Fix 'JSON parsing error when scaling' in AutoGen (TypeScript)

By Cyprian AaronsUpdated 2026-04-21
json-parsing-error-when-scalingautogentypescript

What the error means

If you see JSON parsing error when scaling in AutoGen TypeScript, it usually means one of your agents or tools returned text that AutoGen expected to be valid JSON, but the payload was malformed. This typically shows up when you enable structured outputs, tool calling, or any workflow where AutoGen tries to deserialize model output into an object.

In practice, it often happens during agent-to-agent handoff, function execution, or when a model response includes extra prose around JSON.

The Most Common Cause

The #1 cause is returning plain text instead of strict JSON from a tool, callback, or assistant response that AutoGen is trying to parse.

Here’s the broken pattern:

import { AssistantAgent } from "@autogen/agent";

const assistant = new AssistantAgent({
  name: "support_agent",
  systemMessage: "Return JSON only.",
});

const result = await assistant.run("Summarize the ticket and return JSON.");
console.log(result);

The model may answer with something like:

Sure — here is the summary:
{"priority":"high","category":"billing"}

That looks fine to a human, but it is not valid JSON because of the leading text. AutoGen then throws a parsing error similar to:

Error: JSON parsing error when scaling
SyntaxError: Unexpected token S in JSON at position 0

The fix is to enforce strict structured output and validate before handing the data back into the agent workflow.

Wrong vs right

Broken patternFixed pattern
Model returns prose plus JSONModel returns JSON only
Tool returns a stringified explanationTool returns JSON.stringify(...) with no extra text
No schema validationValidate before parsing
// WRONG
const tool = async () => {
  return `Done. {"status":"ok","count":3}`;
};

// RIGHT
const tool = async () => {
  return JSON.stringify({ status: "ok", count: 3 });
};

If you are using a structured response API, keep the schema tight and reject anything outside it.

type TicketSummary = {
  priority: "low" | "medium" | "high";
  category: string;
};

function parseTicketSummary(raw: string): TicketSummary {
  const parsed = JSON.parse(raw);
  if (!["low", "medium", "high"].includes(parsed.priority)) {
    throw new Error("Invalid priority");
  }
  return parsed;
}

Other Possible Causes

1) Double-stringifying JSON

This happens when you call JSON.stringify() on something that is already a string.

// BROKEN
const payload = JSON.stringify(JSON.stringify({ foo: "bar" }));
JSON.parse(payload); // parses to a string, not an object

// FIXED
const payload2 = JSON.stringify({ foo: "bar" });
const obj = JSON.parse(payload2);

In AutoGen pipelines, this usually breaks downstream code that expects an object and instead receives "{"foo":"bar"}".

2) Invalid escaping in tool output

If your tool returns raw strings with unescaped quotes or newlines, parsing fails immediately.

// BROKEN
return '{"message":"He said "hello""}';

// FIXED
return JSON.stringify({ message: 'He said "hello"' });

This is common when building wrappers around CRM notes, email bodies, or LLM-generated summaries.

3) Mixing chat text with structured tool results

Some teams log debug text inside the same channel used for machine-readable output.

// BROKEN
return `
DEBUG: completed lookup
{"customerId":"123","status":"active"}
`;

// FIXED
return JSON.stringify({
  customerId: "123",
  status: "active",
});

If AutoGen expects a pure object response from FunctionTool, AssistantAgent, or a handoff handler, any extra text will break parsing.

4) Schema mismatch between prompt and parser

You tell the model to return one shape, then parse another.

// BROKEN expectation
type Output = { name: string };

// But model returns:
`{"fullName":"Jane Doe"}`

// FIXED expectation matches output:
type Output = { fullName: string };

This shows up when prompts drift from code after refactors. In TypeScript projects, this gets worse if your runtime parser is looser than your compile-time type.

How to Debug It

  1. Log the raw model output before parsing

    • Don’t inspect only the final object.
    • Print the exact string that hits JSON.parse() or AutoGen’s parser.
    console.log("RAW OUTPUT:", rawResponse);
    
  2. Check whether there is extra text around the JSON

    • Look for prefixes like Sure, Here you go, markdown fences, or debug lines.
    • If you see triple backticks or commentary, that’s your bug.
  3. Verify every tool returns machine-only output

    • Search for return \...${...}`` in tools and callbacks.
    • Make sure each path returns valid JSON without narration.
  4. Compare prompt contract vs runtime schema

    • If your prompt says { id, label } but your parser expects { customerId, status }, fix one side.
    • In TypeScript, keep one source of truth for the shape.

Prevention

  • Use explicit schemas for every structured response. Zod works well with AutoGen TypeScript flows.
  • Never mix human-readable logging with machine-readable payloads.
  • Add unit tests that feed malformed strings into your parser and assert failure early.
  • Keep prompt instructions and TypeScript interfaces in sync during refactors.

If you are building multi-agent workflows in AutoGen TypeScript, treat every boundary as hostile input. Parse strictly at the edges, validate aggressively, and never assume an LLM will emit clean JSON just because you asked nicely.


Keep learning

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

Related Guides