Skip to content

Commit a327f8b

Browse files
committed
fix(tool): coerce object args to string in edit and write tools
Some models (Qwen3-Coder, GLM4.7, and others) occasionally emit a JSON object rather than a plain string for tool parameters that expect strings — e.g. oldString / newString in the edit tool or content in the write tool. Zod rejects these outright, producing a hard validation error that causes the model to loop. Add a z.preprocess step on each affected parameter that serialises any incoming object value (via JSON.stringify) so it becomes a valid string before zod validates it. Plain string inputs are unchanged. Also reorder write tool parameters so filePath comes before content, matching the edit, read, and apply_patch tools. Small/quantised models are sensitive to property order in JSON schemas and more reliably emit filePath when it appears first. Fixes anomalyco#6918
1 parent bd53b65 commit a327f8b

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎packages/opencode/src/tool/edit.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ export const EditTool = Tool.define("edit", {
3838
description: DESCRIPTION,
3939
parameters: z.object({
4040
filePath: z.string().describe("The absolute path to the file to modify"),
41-
oldString: z.string().describe("The text to replace"),
42-
newString: z.string().describe("The text to replace it with (must be different from oldString)"),
41+
oldString: z
42+
.preprocess(
43+
(v) => (v !== null && typeof v === "object" ? JSON.stringify(v, null, 2) : v),
44+
z.string(),
45+
)
46+
.describe("The text to replace"),
47+
newString: z
48+
.preprocess(
49+
(v) => (v !== null && typeof v === "object" ? JSON.stringify(v, null, 2) : v),
50+
z.string(),
51+
)
52+
.describe("The text to replace it with (must be different from oldString)"),
4353
replaceAll: z.boolean().optional().describe("Replace all occurrences of oldString (default false)"),
4454
}),
4555
async execute(params, ctx) {

‎packages/opencode/src/tool/write.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ const MAX_PROJECT_DIAGNOSTICS_FILES = 5
2020
export const WriteTool = Tool.define("write", {
2121
description: DESCRIPTION,
2222
parameters: z.object({
23-
content: z.string().describe("The content to write to the file"),
2423
filePath: z.string().describe("The absolute path to the file to write (must be absolute, not relative)"),
24+
content: z
25+
.preprocess(
26+
(v) => (v !== null && typeof v === "object" ? JSON.stringify(v, null, 2) : v),
27+
z.string(),
28+
)
29+
.describe("The content to write to the file"),
2530
}),
2631
async execute(params, ctx) {
2732
const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)

0 commit comments

Comments
 (0)