{"record":{"id":"43312378739571f1","repo":"ComposioHQ/composio","slug":"experimental-subagent-structured-output-failed-s","errorCode":null,"errorMessage":"experimental_subAgent() structured output failed schema validation: ${summarizeValidationError(validation.error)}","messagePattern":"experimental_subAgent\\(\\) structured output failed schema validation: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ts/packages/cli/src/services/run-subagent-shared.ts","lineNumber":235,"sourceCode":"  [\n    'Your previous response was not valid structured output.',\n    'Do not read files. Do not run terminal commands. Do not inspect the workspace again.',\n    'Reuse the analysis you already completed.',\n    toolName\n      ? `If the MCP tool \\`${toolName}\\` is available, call it exactly once with the final structured result. Otherwise reply with only raw JSON matching the schema.`\n      : 'Reply with only raw JSON matching the schema.',\n    'Do not include prose, markdown fences, or any extra text.',\n    JSON.stringify(structuredSchema, null, 2),\n  ].join('\\n');\n\nexport const validateStructuredOutput = (\n  parsed: unknown,\n  options: InvokeAgentNormalizedOptions\n): unknown => {\n  if (options.zodSchema && typeof options.zodSchema.safeParse === 'function') {\n    const validation = options.zodSchema.safeParse(parsed);\n    if (!validation.success) {\n      throw new Error(\n        `experimental_subAgent() structured output failed schema validation: ${summarizeValidationError(validation.error)}`\n      );\n    }\n\n    return validation.data;\n  }\n\n  return parsed;\n};\n\nconst tryParseStructuredJson = (text: string): unknown | undefined => {\n  const trimmed = text.trim();\n  if (!trimmed) {\n    return undefined;\n  }\n\n  // A parse failure here is the signal to fall through to more permissive\n  // extraction for agents that emit a short status line before the final JSON","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/ComposioHQ/composio/blob/64b1b85502b1beeb2379e6c9e8bf1104504fa637/ts/packages/cli/src/services/run-subagent-shared.ts#L217-L253","documentation":"When experimental_subAgent() is called with a Zod schema, the subagent's parsed JSON output is re-validated with zodSchema.safeParse. If the model's output conforms to JSON syntax but violates the schema, this error embeds a summarized Zod validation failure.","triggerScenarios":"experimental_subAgent(prompt, { schema: z.object({ count: z.number() }) }) where the subagent returns {\"count\":\"five\"}, extra/missing required fields, wrong nesting, or string-vs-number type mismatches in the JSON it emits.","commonSituations":"LLM subagents returning numbers as strings, adding unexpected fields when the schema is strict, omitting optional-looking but required fields, or an over-strict schema that does not match what the prompt asks for.","solutions":["Loosen or correct the schema to match realistic model output (e.g. z.coerce.number(), optional fields)","Strengthen the prompt to explicitly specify the exact JSON shape matching the schema","Retry the subagent call; LLM output is nondeterministic and may validate on another attempt","Pre-validate/repair the raw output before safeParse if you control the parsing path"],"exampleFix":"// before\nconst s = z.object({ count: z.number() });\n// after\nconst s = z.object({ count: z.coerce.number() }); // model often emits \"5\"","handlingStrategy":"retry","validationCode":"const probe = schema.safeParse(candidateOutput); if (!probe.success) { /* re-prompt the subagent with the Zod issue summary */ }","typeGuard":null,"tryCatchPattern":"catch (e) { if (e instanceof Error && e.message.includes('failed schema validation')) { return await experimental_subAgent(prompt, { schema }); /* retry once */ } throw e; }","preventionTips":["Use z.coerce for numeric/boolean fields models commonly stringify","Keep schemas permissive (passthrough/optional) for LLM output","Retry once or twice before surfacing the failure"],"tags":["subagent","zod","schema","llm-output"],"backgroundTag":"schema-validation-failed","analyzedSha":"64b1b85502b1beeb2379e6c9e8bf1104504fa637","analyzedAt":"2026-08-28T15:39:33.623Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}