Skip to content

fix: two 500s on feed-details and simulation-details read endpoints - #22

Merged
satishbagali merged 2 commits into
mainfrom
fix/nan-json-serialization
Jul 25, 2026
Merged

fix: two 500s on feed-details and simulation-details read endpoints#22
satishbagali merged 2 commits into
mainfrom
fix/nan-json-serialization

Conversation

@satishbagali

@satishbagali satishbagali commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

End-to-end API testing against the live Test server surfaced two read endpoints returning HTTP 500. They looked like one issue but had two distinct root causes, each fixed here (one commit per fix).

1. GET /v1/animal/feed-details/{id} — NaN JSON serialization

Nutrient columns are Postgres NUMERIC and some feeds store the literal value NaN. get_feed_details did float(Decimal('NaN'))nan and returned it in the response dict; Starlette's JSONResponse serializes with allow_nan=False, so any NaN raised:

ValueError: Out of range float values are not JSON compliant: nan

Fix: pass the response through the existing ensure_json_safe() (NaN/inf → 0.0) — the same helper the recommendation/evaluation responses already use.

2. GET /v1/animal/simulations/{report_id} — field-name mismatch (not NaN)

Reports persist animal_inputs with the engine key An_StatePhys, but the endpoint validates cattle_info against CattleInfo, whose field is physiological_state (required since the animal-category feature). Building the model from the raw dict raised pydantic Field required → 500 for every report created after that feature shipped.

Fix: map An_StatePhysphysiological_state when assembling cattle_info.

Data note (applied separately, not in this PR)

On the Test DB, 81 feeds held NaN in nutrient columns (fd_cf, fd_nfe, fd_hemicellulose, fd_cellulose ×81, fd_ee ×1 = 325 cells); these were set to NULL (backed up first). That already cleared bug #1 on the live Test server; bug #2 needs this code deployed to clear live.

Verification

  • Reproduced both 500s from the server's own traceback.
  • Full API test suite against a local app running these changes: 164 pass / 0 fail / 6 skip (skips are intentional dangerous/email-sending happy paths), with clean test-data teardown.
  • Both files compile; app.main imports cleanly.

Scope

Two files, +19 / -3 lines: services/diet_service.py, services/report_service.py. No dependency or schema changes.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved feed detail responses by safely converting invalid numeric values for reliable JSON output.
    • Improved simulation detail compatibility by providing the expected physiological state when equivalent data is available.

satish and others added 2 commits July 25, 2026 16:07
Nutrient columns are Postgres NUMERIC and some feeds store the literal value
NaN. get_feed_details did float(Decimal('NaN')) -> nan and returned it in the
response dict; Starlette's JSONResponse serializes with allow_nan=False, so any
NaN raised "ValueError: Out of range float values are not JSON compliant: nan"
-> HTTP 500. On the Test DB this affected 81 feeds.

Pass the response through the existing ensure_json_safe() (NaN/inf -> 0.0), the
same helper the recommendation/evaluation responses already use.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…etails

Reports persist animal_inputs with the engine key `An_StatePhys`, but
GET /v1/animal/simulations/{report_id} validates cattle_info against CattleInfo,
whose field is `physiological_state` (required since the animal-category
feature). Building the model from the raw dict raised pydantic "Field required"
-> HTTP 500 for every report created after that feature shipped.

Map An_StatePhys -> physiological_state when assembling cattle_info so the model
validates and the endpoint returns 200.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 2c82754a-444d-4181-87e4-ec4bb47b175a

📥 Commits

Reviewing files that changed from the base of the PR and between 5673d90 and 683e083.

📒 Files selected for processing (2)
  • services/diet_service.py
  • services/report_service.py

📝 Walkthrough

Walkthrough

Two service responses are normalized before return: feed details sanitize non-finite numeric values, and simulation details populate physiological_state from An_StatePhys when needed.

Changes

Feed response normalization

Layer / File(s) Summary
Sanitize feed detail payload
services/diet_service.py
get_feed_details wraps its response with ensure_json_safe to normalize NaN and infinite numeric values.

Simulation cattle mapping

Layer / File(s) Summary
Map cattle physiological state
services/report_service.py
fetch_simulation_details copies animal_inputs and maps An_StatePhys to physiological_state when the expected field is absent.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the two endpoint 500 fixes and matches the main changes in the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/nan-json-serialization

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@satishbagali
satishbagali merged commit 509ee71 into main Jul 25, 2026
5 checks passed
@satishbagali
satishbagali deleted the fix/nan-json-serialization branch July 25, 2026 10:41
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