{"record":{"id":"e95672a6133b6741","repo":"FlowiseAI/Flowise","slug":"invalid-json-object-error-error","errorCode":null,"errorMessage":"Invalid JSON object. Error: ${error}","messagePattern":"Invalid JSON object\\. Error: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/nodes/agentflow/ConditionAgent/ConditionAgent.ts","lineNumber":242,"sourceCode":"        // Find end of JSON\n        if (startIndex !== -1) {\n            for (const e of ends) {\n                endIndex = jsonString.lastIndexOf(e, jsonString.length)\n                if (endIndex !== -1) {\n                    if (jsonString[endIndex] === '}') {\n                        endIndex += 1\n                    }\n                    break\n                }\n            }\n        }\n\n        if (startIndex !== -1 && endIndex !== -1 && startIndex < endIndex) {\n            const extractedContent = jsonString.slice(startIndex, endIndex).trim()\n            try {\n                return JSON.parse(extractedContent)\n            } catch (error) {\n                throw new Error(`Invalid JSON object. Error: ${error}`)\n            }\n        }\n\n        throw new Error('Could not find JSON block in the output.')\n    }\n\n    async run(nodeData: INodeData, question: string, options: ICommonObject): Promise<any> {\n        let llmIds: ICommonObject | undefined\n        let analyticHandlers = options.analyticHandlers as AnalyticHandler\n\n        try {\n            const abortController = options.abortController as AbortController\n\n            // Extract input parameters\n            const model = nodeData.inputs?.conditionAgentModel as string\n            const modelConfig = nodeData.inputs?.conditionAgentModelConfig as ICommonObject\n            if (!model) {\n                throw new Error('Model is required')","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/nodes/agentflow/ConditionAgent/ConditionAgent.ts#L224-L260","documentation":"ConditionAgent.parseJsonMarkdown locates a JSON block inside the LLM's response (handling ```json fences and bare braces), slices it out, and calls JSON.parse. If parse fails on the extracted substring, it throws 'Invalid JSON object. Error: <SyntaxError>'. This means a JSON-looking region was found but is syntactically malformed.","triggerScenarios":"LLM returns a fenced block that looks like JSON but contains trailing commas, single quotes, unquoted keys, comments, or truncated content; model output with mixed prose inside the braces; partial token streaming that cut off mid-object.","commonSituations":"Weaker/smaller model not following JSON instructions; temperature too high producing valid-ish but invalid JSON; max_tokens cutting the response mid-object; prompt change that confuses the model's output format.","solutions":["Use a stronger model or one with native JSON/structured-output mode for the Condition Agent.","Lower temperature and tighten the system prompt / few-shot examples.","Increase max_tokens so the JSON object is not truncated.","Pre-validate the LLM string with a tolerant JSON parser (jsonrepair) before parseJsonMarkdown, or sanitize common issues (trailing commas, single quotes)."],"exampleFix":"// before\nconst parsed = parseJsonMarkdown(responseContent) // throws on trailing comma\n\n// after — sanitize then parse\nimport { jsonrepair } from 'jsonrepair'\nconst repaired = jsonrepair(extractedContent)\nconst parsed = JSON.parse(repaired)","handlingStrategy":"validation","validationCode":"import { jsonrepair } from 'jsonrepair'\nfunction safeParseJsonMarkdown(extracted) {\n  let repaired\n  try { repaired = jsonrepair(extracted) }\n  catch { throw new Error('LLM output is not repairable JSON') }\n  return JSON.parse(repaired)\n}","typeGuard":"function looksLikeJsonObject(s) {\n  const t = (s ?? '').trim(); return t.startsWith('{') && t.endsWith('}') }","tryCatchPattern":"try { parsed = parseJsonMarkdown(responseContent) }\ncatch (e) {\n  if (/Invalid JSON object/.test(e.message)) {\n    // retry with a stricter prompt or a stronger model\n  } else throw e\n}","preventionTips":["Use models with native JSON/structured-output mode.","Keep temperature low for structured-output calls.","Sanitize/repair LLM JSON before strict parsing."],"tags":["condition-agent","json-parse","llm-output","structured-output"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}