fix(retrieval): harden create_chart_tool input validation, numeric coercion, and configurable resilience

GH
5d ago
+223-13

Failure-Class: none

Summary

Hardens backend/utils/retrieval/tools/chart_tools.py against runtime crashes during chat visualization generation and adds a comprehensive hermetic unit test suite:

  1. Defensive Input Validation & Null Guards:

    • Guards labels and values against None and non-list types, preventing TypeError: object of type 'NoneType' has no len().
    • Requires at least one data point and enforces matching lengths between labels and values.
  2. Case-Insensitive Chart Type Normalization:

    • Trims and lowercases chart_type (e.g. "Line", "BAR" normalize to "line", "bar"), avoiding false rejections from slight model casing variations.
  3. Numeric Coercion & Finite Value Validation:

    • Coerces string numerals (e.g. ["7.2", "8.0"] or [2024, 2025]) into floats while safely stringifying labels.
    • Rejects non-numeric, NaN, and Inf values cleanly to prevent downstream client-side chart crashes.
  4. Configurable Dictionary Resilience:

    • Ensures configurable is safely initialized even when config.get('configurable') returns None, eliminating TypeError: 'NoneType' object does not support item assignment.
  5. Hermetic Unit Test Suite:

    • Added backend/tests/unit/test_chart_tools.py with 10 unit tests (10/10 passing) covering valid line/bar charts, color assignment, casing normalization, invalid chart types, null guards, length mismatches, numeric coercion, invalid/NaN/Inf values, and None configurable resilience.

Bounty Proposal ($50 USD)

  • Proposed Tier: $50 USD
  • Contractor / Settlement: Available for backend & tool engineering contracts (mahmoudghazy2001@gmail.com).