{"record":{"id":"8092f70ea5b14dec","repo":"FoundationAgents/MetaGPT","slug":"table-not-created-yet-please-add-data-first","errorCode":null,"errorMessage":"Table not created yet, please add data first.","messagePattern":"Table not created yet, please add data first\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"metagpt/document_store/lancedb_store.py","lineNumber":29,"sourceCode":"import lancedb\n\n\nclass LanceStore:\n    def __init__(self, name):\n        db = lancedb.connect(\"./data/lancedb\")\n        self.db = db\n        self.name = name\n        self.table = None\n\n    def search(self, query, n_results=2, metric=\"L2\", nprobes=20, **kwargs):\n        # This assumes query is a vector embedding\n        # kwargs can be used for optional filtering\n        # .select - only searches the specified columns\n        # .where - SQL syntax filtering for metadata (e.g. where(\"price > 100\"))\n        # .metric - specifies the distance metric to use\n        # .nprobes - values will yield better recall (more likely to find vectors if they exist) at the expense of latency.\n        if self.table is None:\n            raise Exception(\"Table not created yet, please add data first.\")\n\n        results = (\n            self.table.search(query)\n            .limit(n_results)\n            .select(kwargs.get(\"select\"))\n            .where(kwargs.get(\"where\"))\n            .metric(metric)\n            .nprobes(nprobes)\n            .to_df()\n        )\n        return results\n\n    def persist(self):\n        raise NotImplementedError\n\n    def write(self, data, metadatas, ids):\n        # This function is similar to add(), but it's for more generalized updates\n        # \"data\" is the list of embeddings","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/document_store/lancedb_store.py#L11-L47","documentation":"LanceStore.search raises a plain Exception when self.table is None. The table is only created lazily by LanceStore.add (which calls db.create_table on the first insertion), so searching a store that never received data — or one whose table was dropped — fails with 'Table not created yet, please add data first.'.","triggerScenarios":"Instantiating LanceStore(db, name) and calling search(query_vector, ...) before any successful add() call; or calling search after drop() removed the underlying .lance directory while the instance still exists.","commonSituations":"Fresh vector store in a new session being queried before ingestion; a crashed/interrupted first add() that never created the table; mixing a persisted db path with a new LanceStore object and assuming the table auto-reopens (it does not — this wrapper keeps table=None until add).","solutions":["Call store.add(...) (or add_n_docs / your ingestion path) at least once before search so the table is created via db.create_table.","If data was previously persisted, reopen the table explicitly instead of relying on lazy creation: store.table = store.db.open_table(store.name) before searching.","If operating on a shared db, verify the table exists: if name in [t.name for t in db.table_names()]: store.table = db.open_table(name).","Check for empty store at the application level and skip the search when nothing has been ingested yet."],"exampleFix":"# before\nstore = LanceStore(db, \"docs\")\nresults = store.search(query_vec)  # Exception: Table not created yet\n\n# after\nstore = LanceStore(db, \"docs\")\nif \"docs\" in db.table_names():\n    store.table = db.open_table(\"docs\")\nelse:\n    store.add(\"\", query_vec, {})  # creates table on first add\nresults = store.search(query_vec)","handlingStrategy":"validation","validationCode":"def ensure_lance_table(store, db):\n    \"\"\"Reopen or create the table so search() has something to query.\"\"\"\n    if store.table is None:\n        if store.name in db.table_names():\n            store.table = db.open_table(store.name)\n        else:\n            raise ValueError(f\"no data ingested for table '{store.name}'; call add() first\")","typeGuard":null,"tryCatchPattern":"try:\n    results = store.search(qvec, n_results=5)\nexcept Exception as e:\n    if \"Table not created yet\" in str(e):\n        raise RuntimeError(\"vector store is empty; ingest documents before querying\") from e\n    raise","preventionTips":["Always run at least one add() (or reopen the persisted table) in the same LanceStore instance before search.","Treat an empty store as an application-level condition: skip the search when the collection is empty rather than relying on the exception.","In long-lived services, construct/reopen tables at startup and fail fast there instead of on the first query."],"tags":["python","lancedb","vector-store","initialization-order"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}