{"record":{"id":"1494b281fd83a880","repo":"mem0ai/mem0","slug":"config-must-be-a-config-class-name-instance","errorCode":null,"errorMessage":"Config must be a {config_class.__name__} instance or dict","messagePattern":"Config must be a (.+?) instance or dict","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/utils/factory.py","lineNumber":272,"sourceCode":"        Returns:\n            Reranker instance configured for the specified provider\n\n        Raises:\n            ImportError: If the provider class cannot be imported\n            ValueError: If the provider is not supported\n        \"\"\"\n        if provider_name not in cls.provider_to_class:\n            raise ValueError(f\"Unsupported reranker provider: {provider_name}\")\n\n        class_path, config_class = cls.provider_to_class[provider_name]\n\n        # Handle configuration\n        if config is None:\n            config = config_class(**kwargs)\n        elif isinstance(config, dict):\n            config = config_class(**config, **kwargs)\n        elif not isinstance(config, BaseRerankerConfig):\n            raise ValueError(f\"Config must be a {config_class.__name__} instance or dict\")\n\n        # Import and create the reranker class\n        try:\n            reranker_class = load_class(class_path)\n        except (ImportError, AttributeError) as e:\n            raise ImportError(f\"Could not import reranker for provider '{provider_name}': {e}\")\n\n        return reranker_class(config)\n","sourceCodeStart":254,"sourceCodeEnd":281,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/utils/factory.py#L254-L281","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a plain dict (it is merged with kwargs into the provider config class) or leave config=None and use kwargs","Pass a BaseRerankerConfig-derived instance such as CohereRerankerConfig(api_key=...)","If config arrives as JSON text, parse it first: json.loads(config_str)","Do not pass LLM/embedder config objects into the reranker slot"],"exampleFix":"# before\nreranker = RerankerFactory.create('cohere', config=BaseLlmConfig(model='x'))\n\n# after\nreranker = RerankerFactory.create('cohere', config={'model': 'rerank-v3.5', 'api_key': key})","handlingStrategy":"type-guard","validationCode":"from mem0.configs.rerankers.base import BaseRerankerConfig\ncfg = reranker_cfg.get('config')\nif cfg is not None and not isinstance(cfg, (dict, BaseRerankerConfig)):\n    raise TypeError('reranker config must be dict, BaseRerankerConfig, or None')","typeGuard":"def is_valid_reranker_config(c) -> bool:\n    from mem0.configs.rerankers.base import BaseRerankerConfig\n    return c is None or isinstance(c, (dict, BaseRerankerConfig))","tryCatchPattern":"try:\n    reranker = RerankerFactory.create('cohere', config=maybe_bad)\nexcept ValueError as e:\n    if 'Config must be a' in str(e):\n        raise TypeError('pass a dict or BaseRerankerConfig') from e\n    raise","preventionTips":["Always pass plain dicts for config in YAML/JSON-driven setups","json.loads any config strings before passing","Never reuse config objects across provider categories"],"tags":["configuration","reranker","type-error","validation"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}