{"record":{"id":"c603c3518cf109de","repo":"mem0ai/mem0","slug":"mem0-runtime-has-not-been-initialized","errorCode":null,"errorMessage":"Mem0 runtime has not been initialized.","messagePattern":"Mem0 runtime has not been initialized\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"server/server_state.py","lineNumber":106,"sourceCode":"    with _state_lock:\n        next_config = _merge_config(_current_config, updates)\n        _current_config = next_config\n        _memory_instance = Memory.from_config(next_config)\n        overrides = _load_overrides()\n        overrides = _merge_config(overrides, updates)\n        _save_overrides(overrides)\n        return deepcopy(_current_config)\n\n\ndef get_current_config() -> Dict[str, Any]:\n    with _state_lock:\n        return deepcopy(_current_config)\n\n\ndef get_memory_instance() -> Memory:\n    with _state_lock:\n        if _memory_instance is None:\n            raise RuntimeError(\"Mem0 runtime has not been initialized.\")\n        return _memory_instance\n","sourceCodeStart":88,"sourceCodeEnd":108,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/server/server_state.py#L88-L108","documentation":"Raised by get_memory_instance() in server/server_state.py when the shared Mem0 Memory runtime is requested before initialize has stored it in _memory_instance. The server lazily initializes the memory runtime during startup/config application; any route or job that needs Memory must go through this accessor, and calling it before initialization completes (or after a failure) raises this RuntimeError.","triggerScenarios":"A request hits a memory-backed endpoint before the startup initialization has run (e.g. config was never applied, or the app was imported and called directly without the startup hook); initialization failed earlier (bad LLM/vector-store config) leaving _memory_instance None while the API still serves; a background task starts before server startup finishes; tests call get_memory_instance() without booting server state.","commonSituations":"Missing or invalid MEM0/LLM environment variables so startup init silently skipped or failed; running the FastAPI app in a test harness that never triggers startup events; race on slow initialization where early requests arrive first; config update path reset the runtime and no re-init followed.","solutions":["Ensure the server's startup/config-initialization path runs before serving requests (FastAPI startup handler / lifespan that calls the initialize function in server_state)","Check server logs for a failed initialization (invalid API keys, unreachable vector store) and fix the underlying config error, then restart so init completes","In tests, call the initialization helper (or a fixture that applies a test config) before invoking get_memory_instance()","Guard early requests: return 503 from memory routes when get_memory_instance() raises, so clients retry after startup completes"],"exampleFix":"# before: module-level call at import time, before startup ran\nmemory = get_memory_instance()\n\n# after: resolve lazily inside the request handler after startup\nfrom fastapi import HTTPException\n\ndef memory_or_503():\n    try:\n        return get_memory_instance()\n    except RuntimeError:\n        raise HTTPException(status_code=503, detail=\"Mem0 runtime is not ready yet.\")","handlingStrategy":"try-catch","validationCode":"from server.server_state import get_current_config\n\n# a config that exists implies initialization ran (or will run) on startup\ncfg = get_current_config()\n# in FastAPI: put init in the lifespan/startup handler so it always precedes traffic","typeGuard":"def memory_ready() -> bool:\n    from server import server_state\n    with server_state._state_lock:\n        return server_state._memory_instance is not None","tryCatchPattern":"try:\n    memory = get_memory_instance()\nexcept RuntimeError:\n    raise HTTPException(status_code=503, detail=\"Memory runtime not ready\")  # client retries with backoff","preventionTips":["Initialize the Mem0 runtime in the app's startup/lifespan hook before the server accepts traffic","Fail fast on startup if required LLM/vector-store env vars are missing, rather than serving with _memory_instance None","In tests, use a fixture that applies config and initializes server_state before any handler runs","Return 503 with Retry-After from memory routes during the init window instead of letting a RuntimeError escape"],"tags":["mem0","initialization","runtime-state","startup-ordering"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}