{"record":{"id":"c22ea28873029e0d","repo":"ComposioHQ/composio","slug":"experimental-subagent-expected-valid-json-output","errorCode":null,"errorMessage":"experimental_subAgent() expected valid JSON output for structured response.","messagePattern":"experimental_subAgent\\(\\) expected valid JSON output for structured response\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ts/packages/cli/src/services/run-subagent-shared.ts","lineNumber":394,"sourceCode":"  }\n\n  return bestCandidate?.value;\n};\n\nexport const finalizeInvokeAgentText = (\n  text: string,\n  options: InvokeAgentNormalizedOptions\n): Pick<InvokeAgentResponse, 'result' | 'structuredOutput'> => {\n  const trimmed = text.trim();\n  if (!options.structuredSchema) {\n    return {\n      result: trimmed,\n    };\n  }\n\n  const parsed = tryParseStructuredJson(trimmed);\n  if (parsed === undefined) {\n    throw new Error('experimental_subAgent() expected valid JSON output for structured response.');\n  }\n\n  return {\n    result: null,\n    structuredOutput: validateStructuredOutput(parsed, options),\n  };\n};\n","sourceCodeStart":376,"sourceCodeEnd":402,"githubUrl":"https://github.com/ComposioHQ/composio/blob/64b1b85502b1beeb2379e6c9e8bf1104504fa637/ts/packages/cli/src/services/run-subagent-shared.ts#L376-L402","documentation":"When a schema is configured, experimental_subAgent() expects the subagent's trimmed final text to be parseable JSON (tryParseStructuredJson). If parsing returns undefined — the text is not valid JSON — this error is thrown in finalizeInvokeAgentText.","triggerScenarios":"experimental_subAgent(prompt, { schema }) where the agent replies with prose, markdown-fenced JSON that is not extracted, truncated JSON, or leading chatter like 'Here is the result: {...}'.","commonSituations":"Prompts that do not explicitly demand JSON-only output, models wrapping JSON in ```json fences or commentary, long outputs hitting token limits and getting truncated, or the schema not being conveyed to the subagent.","solutions":["Make the prompt explicitly require raw JSON only, with no prose or code fences","Retry the call — nondeterministic models sometimes emit prose intermittently","Reduce output size or simplify the schema to avoid truncation","If the model wraps output in fences, instruct it not to, since the parser does not strip them"],"exampleFix":"// before\nawait experimental_subAgent('Summarize this repo', { schema });\n// after\nawait experimental_subAgent('Summarize this repo. Respond with ONLY raw JSON matching the schema — no prose, no markdown fences.', { schema });","handlingStrategy":"retry","validationCode":"const looksLikeJson = (s: string) => { try { JSON.parse(s); return true; } catch { return false; } };","typeGuard":"const looksLikeJson = (s: string): boolean => { try { JSON.parse(s); return true; } catch { return false; } };","tryCatchPattern":"catch (e) { if (e instanceof Error && e.message.includes('valid JSON output')) { return await experimental_subAgent(prompt + ' Output ONLY raw JSON.', { schema }); } throw e; }","preventionTips":["State 'respond with only raw JSON, no fences' in every structured prompt","Retry: JSON compliance is nondeterministic","Strip nothing yourself — the parser does not remove fences, so prevent them via prompt"],"tags":["subagent","json","parsing","llm-output"],"backgroundTag":"invalid-json-response","analyzedSha":"64b1b85502b1beeb2379e6c9e8bf1104504fa637","analyzedAt":"2026-08-28T15:39:33.623Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}