Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 6, 2026

This PR attempts to address Issue #10481.

Summary

Adds an experimental feature to automatically repair malformed JSON responses from LLMs that struggle with strict output formats (e.g., Grok Code).

Changes

New Experimental Setting

  • Added llmResponseRepair to the experiments system, disabled by default

JSON Repair Utility (src/utils/repair-json.ts)

Repairs common JSON malformations including:

  • Trailing commas
  • Single quotes to double quotes conversion
  • Unquoted keys
  • Missing closing brackets/braces
  • Prefixed text stripping (when LLMs add explanation before JSON)
  • Markdown code block extraction
  • Control character escaping

Integration

  • Modified NativeToolCallParser to use the repair utility as a fallback when JSON.parse fails (only when the experiment is enabled)
  • Added static methods to enable/disable the feature at runtime

Tests

  • Added comprehensive test suite with 33 test cases for the repair utility

Notes

This is a simpler alternative to full BAML integration (which would require Rust compilation) and focuses specifically on the attempt_completion and tool call issues mentioned in the issue. The pattern-based approach handles the most common malformed JSON patterns seen in LLM outputs.

Feedback and guidance are welcome!

This PR adds an experimental feature to automatically repair malformed JSON
responses from LLMs that struggle with strict output formats.

Changes:
- Add llmResponseRepair experimental setting
- Create JSON repair utility (repair-json.ts) with support for:
  - Trailing commas
  - Single quotes to double quotes conversion
  - Unquoted keys
  - Missing closing brackets/braces
  - Prefixed text stripping
  - Markdown code block extraction
- Integrate repair into NativeToolCallParser as fallback when JSON.parse fails
- Add comprehensive test suite for the repair utility

Closes #10481
@roomote
Copy link
Contributor Author

roomote bot commented Jan 6, 2026

Rooviewer Clock   See task on Roo Cloud

Reviewed this PR. Found 1 issue that needs to be addressed before merging.

  • The experimental feature llmResponseRepair cannot be enabled - setRepairEnabled() is never called (comment)

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

Comment on lines +55 to +75
/**
* Configuration for LLM response repair feature.
* When enabled, attempts to repair malformed JSON from LLM responses.
* @see Issue #10481
*/
private static repairEnabled = false

/**
* Enable or disable the LLM response repair feature.
* This should be called when the experimental setting changes.
*/
public static setRepairEnabled(enabled: boolean): void {
this.repairEnabled = enabled
}

/**
* Check if the repair feature is enabled.
*/
public static isRepairEnabled(): boolean {
return this.repairEnabled
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The setRepairEnabled method is defined but never called anywhere in the codebase. This means the experimental setting llmResponseRepair will have no effect - even if a user enables it, repairEnabled will always remain false. Other experiments in the codebase use a different pattern: they check experiments.isEnabled(state?.experiments ?? {}, EXPERIMENT_IDS.XXX) at runtime when the feature is needed. Either wire up setRepairEnabled() to be called when the experimental setting changes (e.g., in the extension activation or state update path), or refactor to check the experiment state dynamically at runtime like other experiments do.

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

@DarkEden-coding
Copy link

DarkEden-coding commented Jan 6, 2026

This is an improvement but as described in #10481 BAML would be more robust and extendable for future formats and LLM's

Also BAML will not require rust compilation as BAML has packaged binaries and an npm typescript package that would make implementation relatively easy.

@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 Triage to Done in Roo Code Roadmap Jan 8, 2026
@github-project-automation github-project-automation bot moved this from New 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.

4 participants