Skip to content

fix(ml-service): return 422 for non-numeric/NaN fields in /predict-yield - #1335

Closed
saidai-bhuvanesh wants to merge 1 commit into
Nitya-003:mainfrom
saidai-bhuvanesh:fix/predict-yield-422-validation
Closed

fix(ml-service): return 422 for non-numeric/NaN fields in /predict-yield#1335
saidai-bhuvanesh wants to merge 1 commit into
Nitya-003:mainfrom
saidai-bhuvanesh:fix/predict-yield-422-validation

Conversation

@saidai-bhuvanesh

Copy link
Copy Markdown
Contributor

What changed

In ml-service/app.py, the POST /predict-yield handler converted request fields with bare float(...) calls and no error handling:

  • Non-numeric values (e.g. {"area_hectares": "abc"}) raised ValueError → generic 500.
  • NaN/Infinity values parsed fine but propagated through temp_modifier/rain_modifier into round(expected_yield, 2), yielding invalid JSON.

Fix

Added parse_yield_numeric(), which validates each present numeric field (area_hectares, avg_temperature, expected_rainfall):

  • non-numeric → 422 with {"error": "Validation failed", "details": [...]}
  • NaN/inf422
  • missing fields keep the existing default behaviour (no regression)

This matches the structured 422 pattern already used by the /predict and /quality handlers.

Tests

Added ml-service/test_predict_yield.py (unittest) covering:

  • non-numeric area_hectares → 422
  • NaN area → 422
  • non-numeric avg_temperature → 422
  • Infinity rainfall → 422
  • valid request → 200
  • missing numeric fields (defaults) → 200

All 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.

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
@vercel

vercel Bot commented Aug 14, 2026

Copy link
Copy Markdown

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle non-numeric fields in /predict-yield with 422

3 participants