Skip to content

fix: handle JSON parsing in Claude Code CLI responses (#6125) #6126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

roomote[bot]
Copy link

@roomote roomote bot commented Jul 23, 2025

Summary

This PR fixes the issue where Claude Code CLI responses in code mode were displaying unparsed JSON with escaped newlines (\n\n\n) instead of properly formatted text.

Problem

When Claude Code CLI returns responses (especially during errors or when output is truncated), the raw JSON was being displayed to users with escaped newline characters, making the output difficult to read and understand.

Solution

  • Added logic to detect and parse string chunks that contain JSON assistant messages
  • Extract and properly format text content from parsed messages
  • Handle malformed JSON gracefully by displaying it as-is (fallback behavior)
  • Refactored assistant message processing into a reusable method

Testing

  • Added comprehensive tests for the new parsing behavior
  • Tests cover successful parsing, malformed JSON handling, and mixed content types
  • All existing tests continue to pass

Related Issue

Fixes #6125

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Important

Fixes JSON parsing in Claude Code CLI, adds parsing method, and updates tests for proper formatting and error handling.

  • Behavior:
    • Fixes JSON parsing in Claude Code CLI responses to display formatted text instead of raw JSON with escaped newlines.
    • Adds attemptParseAssistantMessage() in claude-code.ts to parse JSON assistant messages.
    • Handles malformed JSON by displaying it as-is.
  • Refactoring:
    • Refactors assistant message processing into processAssistantMessage() in claude-code.ts.
  • Testing:
    • Adds tests in claude-code.spec.ts for JSON parsing, malformed JSON handling, and mixed content types.
    • Ensures all existing tests pass.

This description was created by Ellipsis for e28b9a9. You can customize this summary. It will automatically update as commits are pushed.

- Parse string chunks that contain JSON assistant messages
- Extract and properly format text content from parsed messages
- Handle malformed JSON gracefully by displaying as-is
- Add comprehensive tests for new parsing behavior

Fixes #6125
@roomote roomote bot requested review from mrubens, cte and jr as code owners July 23, 2025 16:25
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jul 23, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jul 23, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jul 24, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working PR - Needs Preliminary Review size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: PR [Needs Prelim Review]
Development

Successfully merging this pull request may close these issues.

Claude Code CLI - API parsing and response issues
2 participants