You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ool arguments with stringified JSON arrays/objects fail Zod validation #8249
When AI models output tool arguments with nested arrays/objects as JSON strings instead of actual JSON structures, tool validation fails. This is a known behavior where some models double-encode nested JSON.
Steps to Reproduce
Use the question tool (AskUserQuestion)
Model outputs arguments like:
{
"questions": "[{"question": "What did you mean?", "header": "Clarify", "options": [...]}]"
}
Validation fails because questions is a string, not an array
Expected Behavior
The tool should detect and parse stringified JSON before Zod validation.
Actual Behavior
Error: The question tool was called with invalid arguments: [
{
"expected": "array",
"code": "invalid_type",
"path": ["questions"],
"message": "Invalid input: expected array, received string"
}
]
The model retries repeatedly with the same format, causing a loop.
Proposed Fix
In packages/opencode/src/tool/tool.ts, add a helper to parse stringified JSON before validation:
function parseStringifiedJson(value: unknown): unknown {
if (typeof value === "string") {
const trimmed = value.trim()
if ((trimmed.startsWith("[") && trimmed.endsWith("]")) ||
(trimmed.startsWith("{") && trimmed.endsWith("}"))) {
try {
return parseStringifiedJson(JSON.parse(trimmed))
} catch {
return value
}
}
return value
}
if (Array.isArray(value)) {
return value.map(parseStringifiedJson)
}
if (value !== null && typeof value === "object") {
const result: Record<string, unknown> = {}
for (const [key, val] of Object.entries(value)) {
result[key] = parseStringifiedJson(val)
}
return result
}
return value
Affected tools: question (and potentially any tool with nested array/object params)
Additional Context
This is a common issue with LLM tool use where models sometimes serialize nested structures as strings rather than native JSON. Other frameworks handle this by detecting and parsing stringified JSON before schema validation.
Activity
github-actions commentedon Jan 13, 2026
This issue might be a duplicate of existing issues. Please check:
Feel free to ignore if none of these address your specific case.
kruxigt commentedon Jan 13, 2026
It probably is! Sorry
rekram1-node commentedon Jan 13, 2026
What model are you getting this with tho? Most models should be smart enough to do this
kruxigt commentedon Jan 13, 2026
i-m-sid commentedon Feb 18, 2026
getting the same issue with claude models