Skip to content

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

Closed
ahamza360 wants to merge 2 commits intoanomalyco:devfrom
ahamza360:dev
Closed

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

Conversation

@ahamza360
Copy link
Copy Markdown

@ahamza360 ahamza360 commented Apr 17, 2026

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 github-actions bot added needs:title needs:compliance This means the issue will auto-close after 2 hours. labels Apr 17, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Hey! Your PR title Fix JSON parsing errors in tool call arguments 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 results, I found one potentially related PR:

Related PR:

The current PR (#23064) appears to be the primary fix for JSON parsing errors in tool call arguments, addressing a specific issue that wasn't directly targeted in previous PRs.

@ahamza360 ahamza360 changed the title Fix JSON parsing errors in tool call arguments fix(opencode): repair malformed JSON in tool call arguments Apr 17, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 17, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

- 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
@ahamza360
Copy link
Copy Markdown
Author

Updated the PR with improved JSON repair logic that handles more complex cases including duplicated/overlapping JSON objects. This addresses the specific issue mentioned in the comments where tool calls like confluence_confluence_search_content were still failing with JSON parsing errors.

@ahamza360
Copy link
Copy Markdown
Author

This PR has been superseded by #23067 which contains improved JSON repair logic that handles more complex cases including duplicated/overlapping JSON objects.

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