fix(opencode): coerce stringified JSON arrays/objects in tool parameters#7513
fix(opencode): coerce stringified JSON arrays/objects in tool parameters#7513crottolo wants to merge 2 commits intoanomalyco:devfrom
Conversation
When LLMs return tool call arguments, they sometimes serialize arrays and objects as JSON strings instead of proper JSON values. This causes Zod validation to fail with 'expected array, received string'. This fix adds automatic coercion: if validation fails because a string was received where an array/object was expected, we attempt to parse the string as JSON and retry validation. Affected tools: Todowrite, Question, and any tool with array/object params. Applies to all providers (Anthropic, OpenAI, Google, etc.).
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, the only related PR that came up alongside PR #7513 is: PR #4715: "fix(write): support array content from Venice.ai models" This PR appears related as it also deals with handling array content from LLM models, which touches on similar issues of how arrays are passed and validated in tool parameters. However, this is an older PR (from the numbering), and the current PR #7513 appears to be a more comprehensive solution addressing stringified JSON in general (not just Venice.ai), with proper coercion logic using Zod's The searches did not reveal any open PRs directly duplicating the current PR's scope. The current PR (#7513) is the most recent one addressing this stringified JSON coercion issue. |
LLMs also send numbers as strings (e.g., timeout: "180000") and booleans as strings (e.g., enabled: "true"). Extended coercion to handle these cases as well. Now handles: - array/object: JSON.parse() - number: Number() for numeric strings - boolean: "true"/"false" conversion
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
|
Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR. |
Fixes #7512
Problem
LLMs sometimes send tool parameters as strings instead of proper typed values:
This causes Zod validation to fail with
expected X, received string.Solution
Added automatic coercion in
Tool.define():safeParse[or{prefix →JSON.parse()for arrays/objectsNumber()for numbers"true"/"false"→ boolean conversionVerification
Tested all affected tools:
Scope