Skip to content

fix(predict-yield): validate numeric fields and return 422 on bad input - #1347

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

fix(predict-yield): validate numeric fields and return 422 on bad input#1347
saidai-bhuvanesh wants to merge 1 commit into
Nitya-003:mainfrom
saidai-bhuvanesh:fix/1234-predict-yield-422-validation

Conversation

@saidai-bhuvanesh

Copy link
Copy Markdown
Contributor

Summary

Fixes #1234

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

  • Non-numeric values such as "abc" raised ValueError500.
  • Values parseable as NaN propagated through temp_modifier/rain_modifier into round(expected_yield, 2), yielding invalid JSON.

Fix

Validate each numeric field (area_hectares, avg_temperature, expected_rainfall): non-numeric or non-finite values return a structured 422 { "error": "Validation failed", "details": [...] } response — consistent with the existing /predict handler — instead of an unhandled 500.

+ import math
...
+ numeric_fields = { "area_hectares": ("area", 0), "avg_temperature": ("temp", 25), "expected_rainfall": ("rainfall", 100) }
+ for field, (var, default) in numeric_fields.items():
+     raw = body.get(field, default)
+     try: value = float(raw)
+     except (TypeError, ValueError): details.append(f"'{field}' must be a number"); parsed[var] = default; continue
+     if not math.isfinite(value): details.append(f"'{field}' must be a finite number"); parsed[var] = default; continue
+     parsed[var] = value
+ if details: return jsonify({"error": "Validation failed", "details": details}), 422
- area = float(body.get("area_hectares", 0))
- temp = float(body.get("avg_temperature", 25))
- rainfall = float(body.get("expected_rainfall", 100))

The existing area_hectares must be > 0 check (422) is preserved after parsing.

Files

  • ml-service/app.py

This PR was created by an AI agent (OpenHands) on behalf of @saidai-bhuvanesh.

@vercel

vercel Bot commented Aug 15, 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.

…003#1234)

Rebased onto current main. predict_yield called float() directly on
client-supplied area_hectares/avg_temperature/expected_rainfall, so a
non-numeric or NaN/inf value raised an unhandled TypeError/ValueError
(500) or produced a NaN that silently poisoned the yield formula. Parse
each field with explicit type/finite checks and return a 422 with
per-field detail when validation fails.
@saidai-bhuvanesh
saidai-bhuvanesh force-pushed the fix/1234-predict-yield-422-validation branch from be9e57c to 83abf1d Compare August 15, 2026 05:13
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.

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

2 participants