datawhalechina/hello-agents · error · HTTPException

diet_run_id 不存在

Error message

diet_run_id 不存在

What it means

HTTP 404 raised by POST /diet/reflect when get_diet_run(body.diet_run_id) returns no row — the given diet_run_id does not exist in the diet_runs store. Reflect is meant to record user feedback against an existing recommendation run, so the run must have been created earlier via /diet/recommend.

Source

Thrown at Co-creation-projects/Shawnxyxy-HealthRecordAgent/backend/api/routes/diet.py:91

    """
    饮食推荐:阶段 2 为 **Nutritionist → Coach → Habit** 三 Agent,固定 JSON schema + Pydantic 校验;
    每阶段最多 2 次尝试,失败则降级并写入 `errors` / `degraded`。
    仍落库 `diet_runs`,并读取 Reflect 记忆。
    """
    svc = DietRecommendService()
    ctx = body.context.model_dump()
    result = await svc.run(body.user_id, ctx)
    return result


@router.post("/diet/reflect")
async def diet_reflect(body: DietReflectRequest):
    """
    Reflect:用户反馈是否执行及原因,写入 diet_reflect;下次 recommend 自动读取。
    """
    row = get_diet_run(body.diet_run_id)
    if not row:
        raise HTTPException(status_code=404, detail="diet_run_id 不存在")
    if row.get("user_id") != body.user_id:
        raise HTTPException(status_code=403, detail="该 run 不属于此 user_id")

    rc = body.reason_code
    if body.followed and rc is None:
        rc = "executed_ok"

    rid = insert_diet_reflect(
        user_id=body.user_id,
        diet_run_id=body.diet_run_id,
        followed=body.followed,
        reason_code=rc,
        reason_detail=body.reason_detail,
    )
    asyncio.create_task(asyncio.to_thread(index_reflect_event, rid))
    return {
        "ok": True,
        "reflect_id": rid,

View on GitHub (pinned to 606a07d341)

Solutions

  1. Create a run first via POST /diet/recommend and use the returned diet_run_id verbatim.
  2. Verify existence with GET /diet/runs/{run_id} before reflecting.
  3. If runs vanish after restart, switch from in-memory storage to the persistent DB so run ids survive.

Example fix

# before
requests.post(f"{base}/diet/reflect", json={"user_id": "u1", "diet_run_id": "does-not-exist", "followed": True})  # 404

# after
run = requests.post(f"{base}/diet/recommend", json={...}).json()
rid = run["diet_run_id"]  # use the exact key returned by /diet/recommend
requests.post(f"{base}/diet/reflect", json={"user_id": "u1", "diet_run_id": rid, "followed": True})
Defensive patterns

Strategy: validation

Validate before calling

resp = requests.get(f"{base}/diet/runs/{diet_run_id}")
if resp.status_code == 404:
    raise LookupError(f"diet_run_id {diet_run_id} not found; call /diet/recommend first")

Try / catch

try:
    r = requests.post(f"{base}/diet/reflect", json=payload)
    r.raise_for_status()
except requests.HTTPError as e:
    if r.status_code == 404:
        # run id stale/unknown -> recreate the run, do not retry
        ...
    else:
        raise

Prevention

When it happens

Trigger: Calling POST /diet/reflect with a diet_run_id that was never created, was typo'd/truncated (note the model requires 8-64 chars), or belongs to a different (e.g. reset) database.

Common situations: Using an expired/in-memory DB that lost runs after a backend restart; passing the reflect endpoint the run's output id field with the wrong name; stale frontend state referencing an old run.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/57aefd0f1d22467a. Report an issue: GitHub.