crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

feat: enhance JSON console formatting for tool input (#3474)

Open devenchawla009 opened this issue 1 year ago โ€ข 2 comments

โœจ Solution

Implemented comprehensive JSON formatting with two approaches:

1. Enhanced ANSI Color Formatting (agent_utils.py)

  • 4-space indentation for better readability
  • Sorted keys for consistency
  • ANSI color syntax highlighting:
    • ๐Ÿ”ต Keys: Bright cyan
    • ๐ŸŸข Strings: Green
    • ๐ŸŸก Numbers: Yellow
    • ๐ŸŸฃ Booleans/null: Magenta
    • โšช Structural chars: White
  • String tool_input handling: Attempts JSON parsing or wraps in {"input": "..."}

2. Rich Integration (console_formatter.py)

  • Enhanced panels with syntax highlighting for verbose mode
  • Configurable formatting (pretty vs standard modes)
  • Improved styling with emoji icons and clean borders

๐Ÿ“ Files Changed (4 files)

  • src/crewai/utilities/agent_utils.py - ANSI color JSON formatting
  • src/crewai/events/utils/console_formatter.py - Rich-based JSON formatting
  • tests/utilities/test_agent_utils.py - Comprehensive tests (22 tests)
  • tests/utilities/test_console_formatter_json.py - Rich formatter tests (14 tests)

๐Ÿงช Testing

  • โœ… 36 tests total - all passing
  • โœ… Complete coverage of formatting scenarios
  • โœ… Edge cases tested (large objects, unicode, errors)
  • โœ… Integration tests for both verbose and non-verbose modes
  • โœ… Error handling for non-serializable objects

๐Ÿ” Before/After Example

Before:

{"search_query": "AI research", "limit": 10}

After:

{
    "limit": 10,
    "search_query": "AI research" 
}

(With beautiful color syntax highlighting in actual console output)

๐Ÿš€ Key Features

  • Backward compatible - no breaking changes
  • Unicode support - preserves emoji and international characters
  • Error resilient - graceful fallback for non-serializable objects
  • Performance optimized - minimal overhead
  • Configurable - supports both basic and rich formatting modes

devenchawla009 avatar Sep 15 '25 06:09 devenchawla009

Requesting @lorenzejay for review

devenchawla009 avatar Sep 15 '25 06:09 devenchawla009

This PR is stale because it has been open for 45 days with no activity.

github-actions[bot] avatar Oct 30 '25 12:10 github-actions[bot]