Repository navigation
fix: validate custom routing weights (NaN, range, unknown keys) and return 422 instead of 500 - #76
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
optimize_for: "custom"only checked that the four weights summed to 1. That let through:{"w_performance": 2, "w_cost": -1, ...}),NaN(abs(nan - 1) > 1e-6isFalse, so the sum check passes),w_speed, silently ignored.The first two reached the scorer and
/routeanswered 500 (ModelScore.scoreout of[0, 1]).Changes
fix(schemas): each weight must be a finite number in[0, 1]; unknown keys are rejected.fix(api): app-levelRequestValidationErrorhandler. FastAPI echoes the rejected input, and aNaN/1e999in it made JSON serialization raise, so the 422 became a 500.test(schemas): newtests/test_schemas.py(unit +/route422 cases, valid custom weights still 200).docs(openapi): regenerated schema (field description).docs(changelog).Checks
ruff check,ruff format --check,mypy(strict) cleanpytest: 199 passed, coverage 91.6%config/presets.yamlstill valid