{"record":{"id":"469a1d2235de75e5","repo":"run-llama/llama_index","slug":"no-async-database-uri-or-engine-provided-cannot-i","errorCode":null,"errorMessage":"No async database URI or engine provided, cannot initialize DB sessionmaker","messagePattern":"No async database URI or engine provided, cannot initialize DB sessionmaker","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/storage/chat_store/sql.py","lineNumber":132,"sourceCode":"    ) -> Tuple[AsyncEngine, sessionmaker]:\n        \"\"\"Set up database connections and session factories.\"\"\"\n        # Create async engine and session factory if async URI is provided\n        if self._async_session_factory is not None and self._async_engine is not None:\n            return self._async_engine, self._async_session_factory\n        elif self.async_database_uri or self._async_engine:\n            self._async_engine = self._async_engine or create_async_engine(\n                self.async_database_uri\n            )\n            if self.async_database_uri is None:\n                self.async_database_uri = self._async_engine.url\n\n            self._async_session_factory = sessionmaker(  # type: ignore\n                bind=self._async_engine, expire_on_commit=False, class_=AsyncSession\n            )\n\n            return self._async_engine, self._async_session_factory  # type: ignore\n        else:\n            raise ValueError(\n                \"No async database URI or engine provided, cannot initialize DB sessionmaker\"\n            )\n\n    async def _setup_tables(self, async_engine: AsyncEngine) -> Table:\n        \"\"\"Set up database tables.\"\"\"\n        # Create metadata with schema\n        if self.db_schema is not None and not self._is_sqlite_database():\n            # Only set schema for databases that support it\n            self._metadata = MetaData(schema=self.db_schema)\n\n            # Create schema if it doesn't exist (PostgreSQL, SQL Server, etc.)\n            async with async_engine.begin() as conn:\n                await conn.execute(\n                    text(f'CREATE SCHEMA IF NOT EXISTS \"{self.db_schema}\"')\n                )\n\n        # Create messages table with status column\n        self._table = Table(","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/storage/chat_store/sql.py#L114-L150","documentation":"SQLChatStore's async session factory (_get_async_sessionmaker) requires either an async_database_uri (e.g. 'sqlite+aiosqlite:///chat.db', 'postgresql+asyncpg://...') or an explicit async engine. If neither an async engine instance nor an async URI is configured at first async use, initialization raises immediately.","triggerScenarios":"Constructing SQLChatStore(database_uri='sqlite:///chat.db') (sync URI only, no +aiosqlite) and then calling an async method such as aget_messages() or aset_messages(), which triggers async engine creation.","commonSituations":"Reusing a sync database URI string for an async-first app; FastAPI/asyncio services configured with the plain sqlite/postgres URL; forgetting the asyncpg/aiosqlite dialect prefix after switching from sync to async usage.","solutions":["Provide an async URI: SQLChatStore(async_database_uri='sqlite+aiosqlite:///chat.db', table_name='chat_store').","Or pass a pre-built engine: create_async_engine(...) handed to SQLChatStore(async_engine=engine).","Install the async driver if missing (pip install aiosqlite or asyncpg) so the +aiosqlite/+asyncpg URL resolves.","For PostgreSQL pass both URIs: database_uri='postgresql+psycopg2://...' for sync paths and async_database_uri='postgresql+asyncpg://...' for async paths."],"exampleFix":"# before\nstore = SQLChatStore(database_uri='sqlite:///chat.db')\nmsgs = await store.aget_messages('sess1')  # ValueError\n\n# after\nstore = SQLChatStore(\n    database_uri='sqlite:///chat.db',\n    async_database_uri='sqlite+aiosqlite:///chat.db',\n)","handlingStrategy":"validation","validationCode":"def make_sql_chat_store(sync_uri: str | None, async_uri: str | None):\n    if not (async_uri or ('+' in (sync_uri or '') and any(d in sync_uri for d in ('aiosqlite', 'asyncpg')))):\n        raise ValueError('Provide async_database_uri or an async engine for async chat-store access')\n    return SQLChatStore(database_uri=sync_uri, async_database_uri=async_uri)","typeGuard":"def supports_async(uri_or_engine) -> bool:\n    if uri_or_engine is None:\n        return False\n    url = str(uri_or_engine)\n    return any(d in url for d in ('aiosqlite', 'asyncpg', 'asyncmy'))","tryCatchPattern":"try:\n    messages = await store.aget_messages(session_id)\nexcept ValueError as e:\n    if 'No async database URI' in str(e):\n        messages = store.get_messages(session_id)  # fall back to sync path\n    else:\n        raise","preventionTips":["Always configure SQLChatStore with both database_uri and async_database_uri when mixing sync and async code.","Use async dialect URLs: sqlite+aiosqlite:///, postgresql+asyncpg://.","Install the async driver (aiosqlite/asyncpg) in the deployment environment."],"tags":["chat-store","sql","async","database","configuration","llama-index"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}