Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 6, 2026

Related GitHub Issue

Closes: #10481

Description

This PR attempts to address Issue #10481 regarding BAML for parsing LLM output to correct malformed responses. Feedback and guidance are welcome.

Implementation approach:
Instead of integrating BAML (which would require Rust compilation), this PR implements a simpler approach using the jsonrepair npm package. This provides a lightweight solution that handles common JSON malformation issues that models like Grok produce.

Key implementation details:

  • Added jsonrepair dependency for JSON repair functionality
  • Created src/utils/json-repair.ts utility module with helper functions
  • Integrated repair logic into NativeToolCallParser.parseToolCall()
  • Added malformedJsonRepair experimental setting (disabled by default)
  • Wired up experiment toggle in Task.ts

Design choices:

  • Feature is gated behind an experimental flag to allow opt-in testing
  • Repair only attempted when standard JSON.parse() fails (no performance impact for valid JSON)
  • Uses jsonrepair library which handles: missing/trailing commas, unquoted keys, missing brackets, comments, etc.

Test Procedure

  1. Run the new test suite:

    cd src && npx vitest run utils/__tests__/json-repair.spec.ts
  2. Run experiments tests:

    cd src && npx vitest run shared/__tests__/experiments.spec.ts
  3. To test manually:

    • Enable the malformedJsonRepair experiment in settings
    • Use a model that produces malformed JSON (like Grok) in native tool calling mode
    • Observe that tool calls are successfully parsed even with minor JSON issues

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.
  • Yes, documentation updates may be required if this feature graduates from experimental status - users would need to know about the new experiment toggle.

Additional Notes

This is an initial implementation using jsonrepair as a simpler alternative to BAML. The original issue suggested BAML, but this approach:

  • Has no Rust compilation dependencies
  • Uses a well-maintained npm package
  • Provides immediate value for fixing common JSON issues
  • Can be replaced with BAML in the future if needed

The feature handles common malformation patterns:

  • Missing or trailing commas
  • Unquoted property names or strings
  • Missing closing brackets/braces
  • Comments in JSON (single-line and block)
  • Newlines in strings
  • Escape character issues
  • Mixed quote styles

This adds support for repairing malformed JSON in LLM tool call responses,
particularly useful for models like Grok that struggle with strict JSON formatting.

Changes:
- Add jsonrepair dependency for JSON repair functionality
- Create json-repair utility module with tryRepairJson, repairJson, parseWithRepair, isValidJson
- Integrate repair logic into NativeToolCallParser.parseToolCall()
- Add malformedJsonRepair experimental setting (disabled by default)
- Wire up experiment toggle in Task.ts

The feature is gated behind an experimental flag and only activates when:
1. The experiment is enabled in settings
2. Standard JSON.parse() fails on tool call arguments

This addresses issue #10481 regarding BAML for parsing LLM output, using
a simpler jsonrepair approach as an initial solution.
@roomote
Copy link
Contributor Author

roomote bot commented Jan 6, 2026

Rooviewer Clock   See task on Roo Cloud

Reviewed the experimental malformed JSON repair feature. The implementation is clean and well-tested. Found one suggestion for improved coverage.

  • Add JSON repair logic to parseDynamicMcpTool() for MCP tools consistency

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

args = JSON.parse(toolCall.arguments)
} catch (parseError) {
// JSON parsing failed - attempt repair if enabled
if (this.malformedJsonRepairEnabled) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repair logic only applies to regular tool calls via parseToolCall(), but parseDynamicMcpTool() (line 910) still uses plain JSON.parse(toolCall.arguments || "{}") without the repair fallback. MCP tool calls from models like Grok would still fail with malformed JSON even when this experiment is enabled. Consider adding the same repair logic there for consistency.

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jan 6, 2026
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jan 8, 2026
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[ENHANCEMENT] Use of BAML for parsing llm output to correct malformed responses

3 participants