diff --git a/deeptutor/api/routers/question_notebook.py b/deeptutor/api/routers/question_notebook.py index 699f3745f..9e542dbb7 100644 --- a/deeptutor/api/routers/question_notebook.py +++ b/deeptutor/api/routers/question_notebook.py @@ -453,6 +453,16 @@ async def get_entry(entry_id: int) -> NotebookEntryItem: @router.patch("/entries/{entry_id}") async def update_entry(entry_id: int, payload: EntryUpdateRequest): + if payload.ai_verdict is not None: + try: + json.dumps(payload.ai_verdict, allow_nan=False) + except ValueError: + # Stored, ``NaN``/``Infinity`` would break every later read. Not a + # model validator: its 422 body echoes the value and cannot be + # serialised either. + raise HTTPException( + status_code=422, detail="ai_verdict must not contain NaN or Infinity" + ) from None store = get_sqlite_session_store() updates = payload.model_dump(exclude_none=True) if not updates: diff --git a/tests/api/test_practice.py b/tests/api/test_practice.py index 561fa517b..afa48b044 100644 --- a/tests/api/test_practice.py +++ b/tests/api/test_practice.py @@ -647,6 +647,26 @@ def test_entry_patch_rejects_contradictory_or_malformed_grades(bank, patch): assert client.patch(f"/bank/entries/{entry['id']}", json=patch).status_code == 422 +def test_entry_patch_rejects_non_finite_verdict_numbers(bank): + store, _, client = bank + entry = seed(store) + # ``json.loads`` accepts these literals; stored, they would break every read. + for literal in ("Infinity", "NaN", "1e999"): + body = ( + '{"ai_verdict": {"result": "correct", "final": true, ' + f'"awarded_points": {literal}}}}}' + ) + response = client.patch( + f"/bank/entries/{entry['id']}", + content=body, + headers={"content-type": "application/json"}, + ) + assert response.status_code == 422 + stored = client.get(f"/bank/entries/{entry['id']}") + assert stored.status_code == 200 + assert stored.json()["ai_verdict"] is None + + def test_entry_patch_derives_correctness_from_the_result(bank): store, _, client = bank entry = seed(store, **_open_question(result="ungraded"))