Skip to content

fix(opencode): write.ts coerce JSON objects to strings#14832

Open
peterdenham wants to merge 8 commits intoanomalyco:devfrom
peterdenham:dev
Open

fix(opencode): write.ts coerce JSON objects to strings#14832
peterdenham wants to merge 8 commits intoanomalyco:devfrom
peterdenham:dev

Conversation

@peterdenham
Copy link
Copy Markdown

Issue for this PR

Closes #6918 #1498

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When writing JSON-based file formats (.excalidraw, .json, etc.), small/quantized models e.g. qwen3-coder, pass the content parameter as a native JSON object rather than a serialized string. The z.string() schema rejects this with:

  Error: The write tool was called with invalid arguments: [
    {
      "expected": "string",
      "code": "invalid_type",
      "path": [
        "content"
      ],
      "message": "Invalid input: expected string, received object"
    }
  ]

I've observed at least one other model hit this error, and it by-passed the write tool and wrote the file itself. Frontier models will hit the error and will know to retry with a serialized string.

This fix adds a z.preprocess() step that serializes any object passed as content to a pretty-printed JSON string before Zod validates it.

This fix makes sense to me, but I'm open to hearing other solutions too.

How did you verify your code works?

The reproduction prompt write a simple diagram in excalidraw, with a qwen3-coder model, now passes.

Screenshots / recordings

n/a

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Copy Markdown
Contributor

Hey! Your PR title write.ts coerce JSON objects to strings doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, there is one potentially related PR:

#7513: fix(opencode): coerce stringified JSON arrays/objects in tool parameters

This PR may be related as it also addresses JSON coercion in tool parameters, though it appears to focus on already-stringified JSON rather than raw objects. You may want to review it to ensure there's no overlap or if it provides relevant context for this fix.

@peterdenham peterdenham changed the title write.ts coerce JSON objects to strings fix: write.ts coerce JSON objects to strings Feb 23, 2026
@peterdenham peterdenham changed the title fix: write.ts coerce JSON objects to strings fix(opencode): write.ts coerce JSON objects to strings Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

qwen3-coder fails to call edit tool

1 participant