feat: Add experimental malformed JSON repair for tool calls #10483
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
jsonrepairnpm package. This provides a lightweight solution that handles common JSON malformation issues that models like Grok produce.Key implementation details:
jsonrepairdependency for JSON repair functionalitysrc/utils/json-repair.tsutility module with helper functionsNativeToolCallParser.parseToolCall()malformedJsonRepairexperimental setting (disabled by default)Task.tsDesign choices:
JSON.parse()fails (no performance impact for valid JSON)jsonrepairlibrary which handles: missing/trailing commas, unquoted keys, missing brackets, comments, etc.Test Procedure
Run the new test suite:
Run experiments tests:
To test manually:
malformedJsonRepairexperiment in settingsPre-Submission Checklist
Documentation Updates
Additional Notes
This is an initial implementation using
jsonrepairas a simpler alternative to BAML. The original issue suggested BAML, but this approach:The feature handles common malformation patterns: