Skip to content

fix(opencode): repair malformed JSON in tool call arguments#23067

Open
ahamza360 wants to merge 2 commits intoanomalyco:devfrom
ahamza360:dev-sync
Open

fix(opencode): repair malformed JSON in tool call arguments#23067
ahamza360 wants to merge 2 commits intoanomalyco:devfrom
ahamza360:dev-sync

Conversation

@ahamza360
Copy link
Copy Markdown

Issue for this PR

Closes #23065

Type of change

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

What does this PR do?

This PR fixes the recurring JSON parsing errors that occur when LLMs generate malformed/truncated JSON for tool call arguments.

Problem:
LLMs occasionally generate malformed/truncated JSON for tool call arguments, resulting in "JSON parsing failed" errors.

Solution:
Enhanced the experimental_repairToolCall callback in packages/opencode/src/session/llm.ts with improved JSON repair logic that:

  • Detects JSON parsing errors in tool call arguments
  • Attempts to repair common truncation issues:
    • Unterminated strings ("value"value")
    • Missing closing brackets/braces ({ "key": "value"{ "key": "value" })
    • Nested structure completion
    • Complex cases like duplicated/overlapping JSON objects
  • Falls back to existing 'invalid' tool handler if repair fails

This makes opencode more robust when dealing with imperfect LLM JSON output.

How did you verify your code works?

  • Created unit tests covering various malformed JSON cases including complex scenarios
  • Built and tested a working binary with the fix
  • Verified the solution resolves the JSON parsing errors including the specific case with confluence_confluence_search_content

Checklist

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

@github-actions
Copy link
Copy Markdown
Contributor

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

Based on the search results, I found two potentially related PRs:

  1. PR fix(opencode): tolerate wrapped workflow tool arguments #20769 - fix(opencode): tolerate wrapped workflow tool arguments

    • Related to handling tool argument parsing issues
  2. PR fix(opencode): recover from truncated tool calls instead of failing silently #21688 - fix(opencode): recover from truncated tool calls instead of failing silently

These PRs may have addressed related JSON/tool call argument issues. You should verify if they already implement similar repair logic or if this PR provides additional improvements over the previous truncation handling.

- Add strategy to handle duplicated/overlapping JSON objects
- Add pattern matching for specific malformed JSON cases
- Insert missing commas that might cause parsing issues
- Improve logging for repair attempts
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.

JSON parsing errors in tool call arguments

1 participant