LightMem icon indicating copy to clipboard operation
LightMem copied to clipboard

BaseMemoryConfigs() cannot build a LightMemory: retrieve_strategy defaults to "embedding" but embedding_retriever defaults to None

Open breken-ai opened this issue 2 months ago • 1 comments

What happens

LightMemory cannot be built with the default configuration declared in its own signature. LightMemory.__init__ is def __init__(self, config: BaseMemoryConfigs = BaseMemoryConfigs()): (src/lightmem/memory/lightmem.py:108), and its docstring lists retrieval as optional — "retrieve_strategy (optional)", "embedding_retriever (optional): Embedding-based retriever if retrieve_strategy is 'embedding' or 'hybrid'" (src/lightmem/memory/lightmem.py:129-131).

In practice that default raises:

  File ".../src/lightmem/memory/lightmem.py", line 184, in __init__
    self.embedding_retriever = EmbeddingRetrieverFactory.from_config(self.config.embedding_retriever)
  File ".../src/lightmem/factory/retriever/embeddingretriever/factory.py", line 25, in from_config
    model_name = config.model_name
AttributeError: 'NoneType' object has no attribute 'model_name'

So there is no way to construct an instance for indexing/extraction only: any configuration that does not spell out a vector store dies at construction, with an error that names neither the field you omitted nor the setting that demanded it.

Reproducing it

Observed on main at 8449d574df6bae1bdf3314a1564da65e2f37e046, macOS arm64, CPython 3.11.15.

from lightmem.configs.base import BaseMemoryConfigs
from lightmem.memory.lightmem import LightMemory

LightMemory(BaseMemoryConfigs())

One caveat about my environment: without the openai extra installed this fails one step earlier, with ImportError: Could not import manager 'lightmem.factory.memory_manager.openai.OpenaiManager': No module named 'openai', because the default memory_manager builds an OpenAI client. Stepping past that with a single unittest.mock.patch.object on MemoryManagerFactory.from_config, returning a stub manager, produces the AttributeError above. That one patch stands in for the missing extra; nothing else in __init__ is stubbed — ShortMemBufferManager and the retriever factory run for real, and the retriever factory is where it dies.

Root cause

Two adjacent field defaults contradict each other:

  • src/lightmem/configs/base.py:77-81 — retrieve_strategy defaults to "embedding".
  • src/lightmem/configs/base.py:86-89 — embedding_retriever defaults to None.
  • src/lightmem/memory/lightmem.py:178-184 — __init__ follows the strategy and, for "embedding" or "hybrid", unconditionally calls EmbeddingRetrieverFactory.from_config(self.config.embedding_retriever).
  • src/lightmem/factory/retriever/embeddingretriever/factory.py:25 — that factory dereferences config.model_name on the None it was handed.

Nothing validates the pairing, so the default configuration always asks for a retriever the default configuration never supplies. The sibling field already avoids this: index_strategy defaults to None (src/lightmem/configs/base.py:60-61), which is why an unset text_embedder is harmless while an unset embedding_retriever is fatal.

Proposed change

Bring retrieve_strategy in line with index_strategy:

  1. Default retrieve_strategy to None.
  2. Add a pydantic after-model validator on BaseMemoryConfigs that:
    • when the strategy is omitted, infers it from the retriever configs actually supplied — hybrid for both, context or embedding for one, and leaves it None when neither is configured, so LightMemory skips both retriever factories and constructs;
    • when the strategy is set explicitly, requires its matching retriever config and raises a ValueError naming the missing field, instead of failing later inside a factory.
  3. Update the retrieve_strategy row of the README configuration table (README.md:516) to describe the new default and the inference rule.

model_validator is already imported and unused in src/lightmem/configs/base.py at the commit above, so this makes an existing import live rather than adding one.

I have this implemented with regression tests and can open the PR immediately; I am filing here first because the README asks for an issue on major changes and this changes a documented public default.

Compatibility

Enumerated with git grep -n retrieve_strategy on 8449d57: 33 matching lines across 17 files. Twelve of those lines assign a value; all twelve set "embedding", and all twelve supply an embedding_retriever on the immediately following line, so none of them would change behaviour:

Config site retrieve_strategy embedding_retriever
README.md 272 273
examples/run_lightmem_ollama.py 117 118
examples/run_lightmem_transformers.py 172 173
experiments/locomo/add_locomo.py 202 203
experiments/longmemeval/offline_update.py 15 16
experiments/longmemeval/run_lightmem_gpt.py 122 123
experiments/longmemeval/run_lightmem_qwen.py 140 141
mcp/example.json 40 41
tutorial-notebooks/LightMem_Example_code.ipynb 399 400
tutorial-notebooks/LightMem_Example_longmemeval.ipynb 383 384
tutorial-notebooks/LightMem_Example_travel.ipynb 300 301
web/backend/app/configspec.py (_base_preset) 116 117

The remaining 21 lines are the field declaration and neighbouring descriptions, consumers that read the resolved value (src/lightmem/memory/lightmem.py, mcp/server.py:288-291, web/backend/app/instance.py:187, 285, web/backend/app/configspec.py:81, 305), the README configuration table, and the frontend type (web/frontend/src/lib/api.ts:148). None assigns the field.

No configuration that works today would be newly rejected: every input the proposed validator refuses already crashes inside a retriever factory the moment it reaches LightMemory.

Three decisions I would rather you make than me

  1. Explicit retrieve_strategy=None. A model validator cannot distinguish an omitted field from one explicitly set to None, so BaseMemoryConfigs(retrieve_strategy=None, embedding_retriever=...) would resolve to "embedding" where today it stays None and builds no retriever. Nothing in this repository does that, but out-of-tree code might. Gating the inference on "retrieve_strategy" not in self.model_fields_set would preserve the old meaning at the cost of a subtler rule. Which do you prefer?

  2. The web console would report this misconfiguration twice. configspec.validate() both constructs BaseMemoryConfigs(**probe) (web/backend/app/configspec.py:218-225) and re-checks the same condition by hand (web/backend/app/configspec.py:305), so the new pydantic error would be appended alongside the existing one. The new entry has an empty path (the frontend cannot anchor it to a field) and no message_zh. Once the model enforces the rule, the check at line 305 is redundant and could be deleted — but that is a web/backend/ change and I did not want to bundle it with a change to src/lightmem/configs/base.py uninvited. Should the PR include it?

  3. Where should "retrieval is not configured" be reported? With this change a retriever-less instance constructs, so the failure moves to first use: LightMemory.retrieve dereferences the optional components without a guard and raises AttributeError: 'LightMemory' object has no attribute 'text_embedder' (src/lightmem/memory/lightmem.py:674). The web backend already handles its own path nicely, with an InstanceNotReady carrying an actionable message (web/backend/app/instance.py:282-286). Adding the equivalent guard to LightMemory.retrieve seems right but is a separate API decision, so I left it out of the proposed change.

Happy to adjust any of the above before opening the PR.

breken-ai avatar Sep 10 '26 07:09 breken-ai

Duplicate of #78.

breken-ai avatar Sep 11 '26 04:09 breken-ai