HKUDS/DeepTutor · error · HTTPException

Upsert failed

Error message

Upsert failed

What it means

Failure guard in upsert_single_entry: persisting the prepared entry through the SQLite session store's upsert raised an exception (constraint violation, locked database, or schema mismatch); the generic message hides the underlying store error from the response.

Solutions

  1. Check server logs for the underlying store exception (the detail only says 'Upsert failed')
  2. If the DB is locked, retry after the concurrent writer finishes
  3. Ensure session_id/question_id match existing rows or are provided consistently for an insert
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at deeptutor/api/routers/question_notebook.py:220 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HKUDS/DeepTutor@3e82f13042 (2026-08-27). Data as JSON: /api/errors/4763f385907a700f. Report an issue: GitHub.

Appendix: source

Thrown at deeptutor/api/routers/question_notebook.py:220

async def upsert_single_entry(payload: UpsertEntryRequest):
    store = get_sqlite_session_store()
    images_records = await _persist_answer_images(payload.session_id, payload.user_answer_images)
    item = payload.model_dump()
    # The store expects ``user_answer_images`` as a plain list of dicts
    # (or absent to mean "leave the stored images alone"). Strip the
    # upload payload version and replace with the persisted records.
    item.pop("user_answer_images", None)
    if images_records is not None:
        item["user_answer_images"] = images_records
    try:
        await store.upsert_notebook_entries(payload.session_id, [item])
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    entry = await store.find_notebook_entry(
        payload.session_id, payload.question_id, turn_id=payload.turn_id
    )
    if entry is None:
        raise HTTPException(status_code=500, detail="Upsert failed")
    return entry


@router.get("/entries", response_model=NotebookEntryListResponse)
async def list_entries(
    category_id: int | None = Query(default=None),
    uncategorized: bool = Query(
        default=False,
        description="Only entries filed under no category — the triage inbox. "
        "Ignored when category_id is set.",
    ),
    bookmarked: bool | None = Query(default=None),
    is_correct: bool | None = Query(default=None),
    search: str = Query(default="", max_length=200),
    sort: str = Query(default="recent", pattern="^(recent|oldest)$"),
    limit: int = Query(default=50, ge=1, le=200),
    offset: int = Query(default=0, ge=0),
) -> NotebookEntryListResponse:

View on GitHub (pinned to 3e82f13042)