mem0ai/mem0 · error · ValueError

Config must be a {config_class.__name__} instance or dict

Error message

Config must be a {config_class.__name__} instance or dict

What it means

Thrown by RerankerFactory.create when a config argument was supplied that is neither None, a plain dict, nor an instance of BaseRerankerConfig. The factory only accepts those three shapes; any other object (e.g. a dict subclass that is not a dict, an LLM config, a string) is rejected. Note the message names the provider-specific config class (e.g. CohereRerankerConfig) even though the accepted base type is BaseRerankerConfig.

Source

Thrown at mem0/utils/factory.py:272

        Returns:
            Reranker instance configured for the specified provider

        Raises:
            ImportError: If the provider class cannot be imported
            ValueError: If the provider is not supported
        """
        if provider_name not in cls.provider_to_class:
            raise ValueError(f"Unsupported reranker provider: {provider_name}")

        class_path, config_class = cls.provider_to_class[provider_name]

        # Handle configuration
        if config is None:
            config = config_class(**kwargs)
        elif isinstance(config, dict):
            config = config_class(**config, **kwargs)
        elif not isinstance(config, BaseRerankerConfig):
            raise ValueError(f"Config must be a {config_class.__name__} instance or dict")

        # Import and create the reranker class
        try:
            reranker_class = load_class(class_path)
        except (ImportError, AttributeError) as e:
            raise ImportError(f"Could not import reranker for provider '{provider_name}': {e}")

        return reranker_class(config)

View on GitHub (pinned to 001c235229)

Solutions

  1. Pass a plain dict (it is merged with kwargs into the provider config class) or leave config=None and use kwargs
  2. Pass a BaseRerankerConfig-derived instance such as CohereRerankerConfig(api_key=...)
  3. If config arrives as JSON text, parse it first: json.loads(config_str)
  4. Do not pass LLM/embedder config objects into the reranker slot

Example fix

# before
reranker = RerankerFactory.create('cohere', config=BaseLlmConfig(model='x'))

# after
reranker = RerankerFactory.create('cohere', config={'model': 'rerank-v3.5', 'api_key': key})
Defensive patterns

Strategy: type-guard

Validate before calling

from mem0.configs.rerankers.base import BaseRerankerConfig
cfg = reranker_cfg.get('config')
if cfg is not None and not isinstance(cfg, (dict, BaseRerankerConfig)):
    raise TypeError('reranker config must be dict, BaseRerankerConfig, or None')

Type guard

def is_valid_reranker_config(c) -> bool:
    from mem0.configs.rerankers.base import BaseRerankerConfig
    return c is None or isinstance(c, (dict, BaseRerankerConfig))

Try / catch

try:
    reranker = RerankerFactory.create('cohere', config=maybe_bad)
except ValueError as e:
    if 'Config must be a' in str(e):
        raise TypeError('pass a dict or BaseRerankerConfig') from e
    raise

Prevention

When it happens

Trigger: Passing a BaseLlmConfig or BaseEmbedderConfig object as the reranker config; passing a JSON string; passing a pydantic model of a different category; passing an already-instantiated reranker object instead of its config.

Common situations: Reusing a config object across categories because both are pydantic models; loading config from JSON and forgetting json.loads so a str is passed; refactoring from dict configs to config classes and passing the wrong class.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/1494b281fd83a880. Report an issue: GitHub.