fix(ml-service): return 422 for non-numeric/NaN fields in /predict-yield - #1335
Closed
saidai-bhuvanesh wants to merge 1 commit into
Closed
fix(ml-service): return 422 for non-numeric/NaN fields in /predict-yield#1335saidai-bhuvanesh wants to merge 1 commit into
saidai-bhuvanesh wants to merge 1 commit into
Conversation
POST /predict-yield converted request fields with bare float() calls and no error handling. Non-numeric values (e.g. "abc") raised ValueError -> 500, and NaN/Infinity values propagated through the yield calculation into invalid JSON. Add parse_yield_numeric() which rejects non-numeric and NaN/inf values with a structured 422 (matching the /predict and /quality handlers), keeping the existing default-when-missing behaviour. Closes Nitya-003#1234
|
@openhands-agent is attempting to deploy a commit to the Nitya Gosain's projects Team on Vercel. A member of the Team first needs to authorize it. |
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 changed
In
ml-service/app.py, thePOST /predict-yieldhandler converted request fields with barefloat(...)calls and no error handling:{"area_hectares": "abc"}) raisedValueError→ generic 500.NaN/Infinityvalues parsed fine but propagated throughtemp_modifier/rain_modifierintoround(expected_yield, 2), yielding invalid JSON.Fix
Added
parse_yield_numeric(), which validates each present numeric field (area_hectares,avg_temperature,expected_rainfall):422with{"error": "Validation failed", "details": [...]}NaN/inf→422This matches the structured
422pattern already used by the/predictand/qualityhandlers.Tests
Added
ml-service/test_predict_yield.py(unittest) covering:area_hectares→ 422NaNarea → 422avg_temperature→ 422Infinityrainfall → 422All 6 tests pass (
ML_API_KEY=test-key python -m unittest test_predict_yield).Closes #1234
This PR was created by an AI agent (OpenHands) on behalf of @saidai-bhuvanesh.