sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] feat: Add FuzzyEquals deterministic evaluator for structured data comparison with tolerance Labels:

Open sanjanasekar opened this issue 3 months ago • 1 comments

SDK Language

Python

Problem Statement

The current deterministic evaluators (Equals, Contains, StartsWith, StateEquals) only support exact matching. When evaluating agents that produce structured data outputs (SQL results, JSON objects, tabular data, numeric computations), exact matching is too brittle:

  1. Row order shouldn't matter in query results
  2. Column order shouldn't matter in tables
  3. "42" and 42 should match (type coercion)
  4. 3.14159 and 3.14 should match within tolerance
  5. Extra whitespace shouldn't fail a test
  6. Subset containment ("expected values are present") is often more appropriate than full equality

Teams are forced to either:

  1. Use OutputEvaluator (LLM-as-judge) — expensive, non-deterministic, overkill for structured data
  2. Build custom comparison logic per project — duplicated effort across teams

Proposed Solution

Proposed Solution

A FuzzyEquals deterministic evaluator with configurable knobs:

Proposed: A FuzzyEquals deterministic evaluator with configurable knobs:

  • ignore_order: bool (row/list order doesn't matter)
  • ignore_key_order: bool (dict/column order doesn't matter)
  • numeric_tolerance: float (±% for numeric values)
  • case_sensitive: bool
  • ignore_whitespace: bool
  • subset_mode: bool (actual ⊇ expected)
  • type_coercion: bool ("42" == 42)

from strands_evals.evaluators.deterministic import FuzzyEquals

evaluator = FuzzyEquals(
    ignore_order=True,           # list/row order doesn't matter
    ignore_key_order=True,       # dict/column order doesn't matter
    numeric_tolerance=0.01,      # ±1% for numeric values
    case_sensitive=False,        # string comparison
    ignore_whitespace=True,      # strip/normalize whitespace
    subset_mode=False,           # True = actual ⊇ expected (containment)
    type_coercion=True,          # "42" == 42, "true" == True
)

Use Case

SQL result comparison, JSON API validation, CSV/table output, numeric computation results, code output comparison.

Alternatives Solutions

Could use deepdiff under the hood. Should follow existing Evaluator[InputT, OutputT] pattern and return EvaluationOutput with detailed diff in reason field.

Additional Context

No response

sanjanasekar avatar Aug 10 '26 04:08 sanjanasekar

I've opened a first-draft implementation of FuzzyEquals in the strands-agents/evals repo (where the strands_evals deterministic evaluators live): strands-agents/evals#387

It implements all the proposed knobs (ignore_order, ignore_key_order, numeric_tolerance, case_sensitive, ignore_whitespace, subset_mode, type_coercion), follows the existing Evaluator[InputT, OutputT] pattern, and returns an EvaluationOutput with a detailed diff path in the reason field. Included 44 unit tests; formatter/linter/mypy and the deterministic test suite all pass locally.

It's marked Draft — I'd love maintainer feedback on the API surface and whether a hand-rolled recursive comparison (current approach) or deepdiff under the hood is preferred.

I use a coding assistant to help implement, and I review and take responsibility for the final change.

lifelmy avatar Aug 31 '26 21:08 lifelmy