Skip to content

Add JSON sanitizer for Kimi models (fix malformed tool call arguments)#6691

Closed
nosleepcassette wants to merge 3 commits into
NousResearch:mainfrom
nosleepcassette:fix/kimi-json-sanitizer
Closed

Add JSON sanitizer for Kimi models (fix malformed tool call arguments)#6691
nosleepcassette wants to merge 3 commits into
NousResearch:mainfrom
nosleepcassette:fix/kimi-json-sanitizer

Conversation

@nosleepcassette

Copy link
Copy Markdown
Contributor

Problem

The moonshotai/kimi-k2-instruct model (and similar Kimi variants) produce malformed JSON in tool call arguments, causing cascading failures:

  • Unterminated string errors at char 1
  • Missing closing braces/brackets
  • Empty tool name fields
  • Truncated key-value pairs

The current retry logic (3 retries) amplifies the problem since the same malformed JSON gets re-serialized unchanged.

Solution

This PR adds a model-specific JSON sanitizer that:

  1. Detects known problematic models via is_kimi_model() check
  2. Attempts repair of common malformation patterns:
    • Adds missing closing quotes/braces
    • Removes truncated key-value pairs
    • Falls back to regex extraction as last resort
  3. Hooks into validation loop at run_agent.py:8820-8834
  4. Fails gracefully if sanitizer unavailable (ImportError caught)

Files Changed

  • tools/kimi_json_sanitizer.py - New sanitizer module
  • run_agent.py - Hook into JSON validation for repair attempt before declaring invalid

Testing

Tested against the following malformed JSON patterns:

'{"tool": "", "arguments": {"command": "'  # Missing tool name
'{"tool": "terminal", "arguments": {"command": "ls -la", "timeout"'  # Unterminated
'{"tool": "read_file", "arguments": {"path": "/tmp/test.txt"'  # Missing bracket

Notes

  • Only activates for models matching *kimi* pattern
  • No impact on other models/providers
  • Logging added for repaired vs regex-extracted cases

This enables use of capable-but-janky models without polluting the core agent loop with model-specific hacks.

- Use git checkout --force to handle untracked files that conflict with target branch
- Add error handling with helpful message if checkout fails
- Fixes issue where hermes update would fail on detached HEAD with untracked files

This commonly affects developers with session logs, generated skills, or other
untracked files in the working directory.
These models produce malformed JSON in tool call arguments:
- Unterminated strings at char 1
- Missing closing braces
- Truncated key-value pairs

This patch:
- Adds tools/kimi_json_sanitizer.py with repair logic
- Hooks into run_agent.py JSON validation (line 8820-8834)
- Only activates for known problematic models
- Falls back gracefully if sanitizer unavailable

Fixes cascading retry failures on models with JSON serialization issues.
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for this contribution! The malformed tool call argument problem you diagnosed is real and was already fixed on main via a parallel effort.

This is an automated hermes-sweeper review.

  • A general-purpose repair pipeline (_repair_tool_call_arguments) was cherry-picked from PR fix(agent): repair malformed tool_call arguments before API send #12252 (cross-referenced on this PR) into main at commit 9eeaaa4f1 and shipped in v2026.4.23.
  • That pipeline covers every repair stage in this PR (trailing commas, unclosed braces/brackets, Python None, empty strings) and applies to all models, not just Kimi.
  • A follow-up commit 17fc84c25 extended the same repair to the streaming assembly path.
  • tools/kimi_json_sanitizer.py (this PR's new module) is not present on main — the broader fix supersedes it.

Closing as implemented. If you believe there are Kimi-specific edge cases the general pipeline misses, please open a targeted issue with a reproduction.

@teknium1 teknium1 closed this Apr 27, 2026
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.

2 participants