Mintplex-Labs/anything-llm · error

The request body of this API Call block is not valid JSON…

Error message

The request body of this API Call block is not valid JSON. Open the flow in the Agent Builder and check the body for syntax errors such as missing commas, brackets, or quotes.

What it means

The Agent Flow API Call block validates that a JSON-typed request body actually parses as JSON before sending. When safeJsonParse returns null and the body is non-empty, the block aborts with this instructional message telling the flow author to fix the JSON syntax in the Agent Builder.

Solutions

  1. Open the flow in the Agent Builder and paste the body into a JSON linter to find the syntax error.
  2. Remove trailing commas, single quotes, and comments from the body.
  3. If the body contains variables, switch bodyType to 'text'/'form' or ensure the substituted values are valid JSON.
  4. Validate the resolved body (after variable substitution) not just the literal template.
  5. Re-run the flow after fixing; the error is deterministic for the same body.

Example fix

// before (body)
{"name": 'test',}
// after
{"name": "test"}
Defensive patterns

Strategy: validation

Validate before calling

function isValidJsonBody(body) {
  if (!body || !body.trim()) return true; // empty is allowed
  try { JSON.parse(body); return true; } catch { return false; }
}
// call before triggering the flow
if (!isValidJsonBody(block.body)) throw new Error('API Call block body is not valid JSON');

Type guard

function isJsonObject(v) {
  return typeof v === 'object' && v !== null && !Array.isArray(v);
}

Try / catch

try {
  await runAgentFlow(flowId, vars);
} catch (e) {
  if (/API Call block is not valid JSON/.test(e.message)) {
    // surface a 400 to the flow author with the block name
    return res.status(400).json({ error: 'Fix the API Call block body in the Agent Builder' });
  }
  throw e;
}

Prevention

When it happens

Trigger: An agent flow with an API Call block whose bodyType is 'json' and whose body contains malformed JSON — trailing commas, unquoted keys, single quotes, unescaped newlines, or a template placeholder left unresolved producing invalid JSON.

Common situations: Hand-editing flow JSON and introducing syntax errors; pasting a body from curl with shell-style quotes; using {{variable}} placeholders that expand to non-JSON values; copying JSON with comments into the body field.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@f92433b4ea (2026-09-22). Data as JSON: /api/errors/cc5bfbae62a7a9b9. Report an issue: GitHub.

Appendix: source

Thrown at server/utils/agentFlows/executors/api-call.js:32

  const requestConfig = {
    method,
    headers: headers.reduce((acc, h) => ({ ...acc, [h.key]: h.value }), {}),
  };

  if (["POST", "PUT", "PATCH"].includes(method)) {
    if (bodyType === "form") {
      const formDataObj = new URLSearchParams();
      formData.forEach(({ key, value }) => formDataObj.append(key, value));
      requestConfig.body = formDataObj.toString();
      requestConfig.headers["Content-Type"] =
        "application/x-www-form-urlencoded";
    } else if (bodyType === "json") {
      const parsedBody = safeJsonParse(body, null);
      if (parsedBody !== null) {
        requestConfig.body = JSON.stringify(parsedBody);
      } else if (body?.trim()) {
        throw new Error(
          "The request body of this API Call block is not valid JSON. Open the flow in the Agent Builder and check the body for syntax errors such as missing commas, brackets, or quotes."
        );
      }
      requestConfig.headers["Content-Type"] = "application/json";
    } else if (bodyType === "text") {
      requestConfig.body = String(body);
    } else {
      requestConfig.body = body;
    }
  }

  try {
    introspect(`Sending body to ${url}: ${requestConfig?.body || "No body"}`);
    const response = await fetch(url, requestConfig);
    if (!response.ok) {
      introspect(`Request failed with status ${response.status}`);
      throw new Error(`HTTP error! status: ${response.status}`);
    }

View on GitHub (pinned to f92433b4ea)