{"record":{"id":"6dee8b6854eac522","repo":"infiniflow/ragflow","slug":"100-6dee8b","errorCode":"100","errorMessage":"Fail to create a session!","messagePattern":"Fail to create a session!","errorType":"exception","errorClass":"LookupError","httpStatus":null,"severity":"error","filePath":"api/apps/restful_apis/chat_api.py","lineNumber":225,"sourceCode":"    )\n\n\nasync def _create_session_for_completion(chat_id, dialog, user_id, save_session=True):\n    conv = {\n        \"id\": get_uuid(),\n        \"dialog_id\": chat_id,\n        \"name\": \"New session\",\n        \"message\": [{\"role\": \"assistant\", \"content\": dialog.prompt_config.get(\"prologue\", \"\")}],\n        \"user_id\": user_id,\n        \"reference\": [],\n    }\n    if not save_session:\n        conv[\"id\"] = None\n        return SimpleNamespace(**conv)\n    await thread_pool_exec(ConversationService.save, **conv)\n    ok, conv_obj = await thread_pool_exec(ConversationService.get_by_id, conv[\"id\"])\n    if not ok:\n        raise LookupError(\"Fail to create a session!\")\n    return conv_obj\n\n\ndef _get_bool_request_flag(req, *names, default=False):\n    for name in names:\n        if name not in req:\n            continue\n        value = req.pop(name)\n        if isinstance(value, str):\n            return value.strip().lower() in {\"1\", \"true\", \"yes\", \"on\"}\n        return bool(value)\n    return default\n\n\ndef _normalize_completion_messages(req):\n    messages = req.get(\"messages\")\n    if messages is None:\n        question = req.get(\"question\")","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/infiniflow/ragflow/blob/554fb1133ac3861732235ad9c377eb5e0a770665/api/apps/restful_apis/chat_api.py#L207-L243","documentation":"Raised in chat_api when creating a chat session with save_session enabled: ConversationService.save persisted the conversation but the immediate get_by_id lookup for the new conv['id'] returned ok=False. It means the session row could not be read back from the database right after insertion, so the API cannot return a session object.","triggerScenarios":"POST to create a chat session (or an internal completion flow that saves sessions) where the DB insert succeeds or silently fails and the follow-up SELECT by the generated conversation id finds no row — e.g. a transaction not committed, the id not backfilled by save(), or the row being deleted concurrently.","commonSituations":"Database connectivity glitches, a misconfigured Peewee/MySQL setup where auto-commit is off, the ConversationService.save implementation not setting the generated id, or concurrent cleanup jobs deleting sessions immediately.","solutions":["Check server logs for the underlying DB error around ConversationService.save (connection loss, deadlock, constraint failure).","Retry the create-session request once — transient read-after-write failures often clear.","Verify the database is healthy and not in a read-only or migration-pending state (check /system health endpoints).","If it persists, inspect ConversationService.save to confirm it commits and returns/backfills conv['id']."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"async def healthcheck_db():\n    ok, _ = await thread_pool_exec(ConversationService.get_by_id, \"00000000-0000-0000-0000-000000000000\")\n    return ok is not None  # raises nothing, proves SELECT path works\n\n# only attempt session creation when DB is reachable\nif await healthcheck_db():\n    conv = await create_session(chat_id, user_id)","typeGuard":null,"tryCatchPattern":"for attempt in range(2):\n    try:\n        conv = await create_session(chat_id, user_id)\n        break\n    except LookupError as e:\n        if \"Fail to create a session\" not in str(e) or attempt == 1:\n            raise","preventionTips":["Monitor DB health before chat workloads; treat this error as a DB symptom, not a client bug.","Keep one retry for read-after-write session lookups in SDK wrappers.","Alert on repeated occurrences — it can indicate auto-commit or replication lag problems."],"tags":["chat-api","session","database","read-after-write"],"backgroundTag":null,"analyzedSha":"554fb1133ac3861732235ad9c377eb5e0a770665","analyzedAt":"2026-08-15T09:20:16.380Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}