{"record":{"id":"2cf616d8040dd58d","repo":"eyaltoledano/claude-task-master","slug":"generated-object-does-not-match-schema-validati","errorCode":null,"errorMessage":"Generated object does not match schema: ${validationError.message}. Generated: ${JSON.stringify(parsedObject, null, 2)}","messagePattern":"Generated object does not match schema: (.+?)\\. Generated: (.+?)","errorType":"validation","errorClass":"MCPError","httpStatus":null,"severity":"error","filePath":"mcp-server/src/custom-sdk/language-model.js","lineNumber":173,"sourceCode":"\t\t\t// Validate against schema\n\t\t\ttry {\n\t\t\t\tconst validatedObject = schema.parse(parsedObject);\n\n\t\t\t\treturn {\n\t\t\t\t\tobject: validatedObject,\n\t\t\t\t\tfinishReason: result.finishReason || 'stop',\n\t\t\t\t\tusage: {\n\t\t\t\t\t\tpromptTokens: result.usage?.inputTokens || 0,\n\t\t\t\t\t\tcompletionTokens: result.usage?.outputTokens || 0,\n\t\t\t\t\t\ttotalTokens:\n\t\t\t\t\t\t\t(result.usage?.inputTokens || 0) +\n\t\t\t\t\t\t\t(result.usage?.outputTokens || 0)\n\t\t\t\t\t},\n\t\t\t\t\trawResponse: response,\n\t\t\t\t\twarnings: result.warnings\n\t\t\t\t};\n\t\t\t} catch (validationError) {\n\t\t\t\tthrow new MCPError(\n\t\t\t\t\t`Generated object does not match schema: ${validationError.message}. Generated: ${JSON.stringify(parsedObject, null, 2)}`\n\t\t\t\t);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthrow mapMCPError(error);\n\t\t}\n\t}\n\n\t/**\n\t * Stream text generation using MCP session sampling\n\t * Note: MCP may not support native streaming, so this may simulate streaming\n\t * @param {object} options - Generation options\n\t * @returns {AsyncIterable} Stream of generation chunks\n\t */\n\tasync doStream(options) {\n\t\ttry {\n\t\t\t// For now, simulate streaming by chunking the complete response\n\t\t\t// TODO: Implement native streaming if MCP supports it","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/eyaltoledano/claude-task-master/blob/c0c98d367c55296bfe69e65680625b6db437af02/mcp-server/src/custom-sdk/language-model.js#L155-L191","documentation":"After parsing the model's JSON, doGenerateObject validates it against the provided schema; if validation fails it throws an MCPError containing the validator's message plus the full pretty-printed generated object. This means the model produced valid JSON that doesn't conform to the requested structure (missing fields, wrong types, extra/invalid values).","triggerScenarios":"Model returns JSON whose shape fails schema validation — missing required properties, wrong types (string vs number), or values violating constraints — when calling doGenerateObject/generateObject through the MCP language model.","commonSituations":"Weak models hallucinating fields or omitting required ones; overly strict schemas (enums, formats) the model can't satisfy; schema/prompt mismatch after refactoring.","solutions":["Make the schema lenient where possible (optional fields, defaults, unions) and re-prompt with explicit field requirements and an example output","Retry the generation — schema-adherence failures are often stochastic; add 'Respond ONLY with JSON matching this exact schema' instructions","Post-process/repair the object (fill defaults, coerce types) before validation, or use a repair/re-ask loop"],"exampleFix":"// before\nconst schema = z.object({ count: z.number() });\n// after\nconst schema = z.object({ count: z.coerce.number().default(0) });","handlingStrategy":"try-catch","validationCode":"// Pre-validate expected fields if you control the prompt/output contract\nif (!jsonText.includes('\"requiredField\"')) console.warn('Response may be missing required fields');","typeGuard":null,"tryCatchPattern":"try { obj = await model.doGenerateObject({ schema, prompt }); } catch (e) {\n  if (e.message.startsWith('Generated object does not match schema')) {\n    // retry with a stricter prompt or relax the schema\n  } else throw e;\n}","preventionTips":["Include an explicit JSON example matching the schema in the prompt","Keep schemas simple: avoid strict enums/formats the model may violate; mark non-essential fields optional","Implement one retry-with-feedback loop: feed the validation error back to the model on failure"],"tags":["mcp","ai-sdk","schema","llm-output","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"c0c98d367c55296bfe69e65680625b6db437af02","analyzedAt":"2026-08-29T02:56:26.071Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}