Skip to content

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

Closed
peterdenham wants to merge 6 commits into
anomalyco:devfrom
peterdenham:dev
Closed

fix(opencode): write.ts coerce JSON objects to strings#14832
peterdenham wants to merge 6 commits into
anomalyco: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
@bhuv-z
Copy link
Copy Markdown

bhuv-z commented Apr 12, 2026

Can this be merged please, possible fix for an issue with tool calling when using Qwen3-coder

@rekram1-node
Copy link
Copy Markdown
Collaborator

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

3 participants