Skip to content

fix: validate custom routing weights (NaN, range, unknown keys) and return 422 instead of 500 - #76

Merged
Caezarr merged 5 commits into
mainfrom
fix/custom-weight-validation
Dec 3, 2026
Merged

Caezarr merged 5 commits into
mainfrom
fix/custom-weight-validation

Conversation

@Caezarr

@Caezarr Caezarr commented Dec 3, 2026

Copy link
Copy Markdown
Owner

What

optimize_for: "custom" only checked that the four weights summed to 1. That let through:

  • negative / >1 weights that still sum to 1 ({"w_performance": 2, "w_cost": -1, ...}),
  • NaN (abs(nan - 1) > 1e-6 is False, so the sum check passes),
  • unknown keys like w_speed, silently ignored.

The first two reached the scorer and /route answered 500 (ModelScore.score out of [0, 1]).

Changes

  1. fix(schemas): each weight must be a finite number in [0, 1]; unknown keys are rejected.
  2. fix(api): app-level RequestValidationError handler. FastAPI echoes the rejected input, and a NaN / 1e999 in it made JSON serialization raise, so the 422 became a 500.
  3. test(schemas): new tests/test_schemas.py (unit + /route 422 cases, valid custom weights still 200).
  4. docs(openapi): regenerated schema (field description).
  5. docs(changelog).

Checks

  • ruff check, ruff format --check, mypy (strict) clean
  • pytest: 199 passed, coverage 91.6%
  • YAML presets in config/presets.yaml still valid

The sum check alone let through weights like {w_performance: 2, w_cost: -1}
and NaN (abs(nan - 1) > eps is False), which then crashed /route with a 500
when ModelScore validated a composite score outside [0, 1]. Unknown keys such
as w_speed were silently ignored.
FastAPI's default handler echoes the invalid input back, and serializing a
non-finite float raises, so a NaN / 1e999 body became a 500 instead of a 422.
@Caezarr
Caezarr merged commit 767e3da into main Dec 3, 2026
5 checks passed
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.

1 participant