{"record":{"id":"6c1ac55df54b32a0","repo":"chroma-core/chroma","slug":"trying-to-instantiate-configuration-of-type-cls","errorCode":null,"errorMessage":"Trying to instantiate configuration of type {cls.__name__} from JSON with type {json_map['_type']}","messagePattern":"Trying to instantiate configuration of type (.+?) from JSON with type (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"chromadb/api/configuration.py","lineNumber":208,"sourceCode":"    @override\n    def to_json(self) -> Dict[str, Any]:\n        \"\"\"Returns the JSON compatible dictionary representation of the configuration.\"\"\"\n        json_dict = {\n            name: parameter.value.to_json()\n            if isinstance(parameter.value, ConfigurationInternal)\n            else parameter.value\n            for name, parameter in self.parameter_map.items()\n        }\n        # What kind of configuration is this?\n        json_dict[\"_type\"] = self.__class__.__name__\n        return json_dict\n\n    @classmethod\n    @override\n    def from_json(cls, json_map: Dict[str, Any]) -> Self:\n        \"\"\"Returns a configuration from the given JSON string.\"\"\"\n        if cls.__name__ != json_map.get(\"_type\", None):\n            raise ValueError(\n                f\"Trying to instantiate configuration of type {cls.__name__} from JSON with type {json_map['_type']}\"\n            )\n        parameters = []\n        for name, value in json_map.items():\n            # Type value is only for storage\n            if name == \"_type\":\n                continue\n            parameters.append(ConfigurationParameter(name=name, value=value))\n        return cls(parameters=parameters)\n\n\nclass HNSWConfigurationInternal(ConfigurationInternal):\n    \"\"\"Internal representation of the HNSW configuration.\n    Used for validation, defaults, serialization and deserialization.\"\"\"\n\n    definitions = {\n        \"space\": ConfigurationDefinition(\n            name=\"space\",","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/chromadb/api/configuration.py#L190-L226","documentation":"ConfigurationInternal.from_json (chromadb/api/configuration.py:208) raises this ValueError when the \"_type\" discriminator in the JSON map does not equal the class you are deserializing into. to_json stamps every serialized configuration with _type = its class name, and from_json enforces an exact match (cls.__name__ != json_map[\"_type\"]), so loading a map of one configuration class through another class is rejected instead of silently misinterpreting fields.","triggerScenarios":"HNSWConfigurationInternal.from_json({\"_type\": \"SomeOtherConfigurationInternal\", ...}); a _type value with different casing; loading nested configuration JSON with the outer class's from_json instead of the nested class; version skew where the producer class was renamed.","commonSituations":"Generic loading code that picks a fixed class regardless of _type; copy-pasting deserialization calls between configuration types; renamed configuration classes across chromadb versions invalidating stored JSON.","solutions":["Dispatch on the discriminator: look up the class by json_map[\"_type\"] (e.g. via the module's ConfigurationInternal subclasses) and call its from_json","Or call the exact matching class: HNSWConfigurationInternal.from_json(map_with_matching_type)","Regenerate stored JSON with the current version via to_json so _type matches"],"exampleFix":"# before\ncfg = HNSWConfigurationInternal.from_json({\"_type\": \"OtherConfiguration\", ...})\n# after\nm = json.loads(raw)\ncls = {c.__name__: c for c in ConfigurationInternal.__subclasses__()}[m[\"_type\"]]\ncfg = cls.from_json(m)","handlingStrategy":"validation","validationCode":"def from_json_typed(cls, json_map):\n    actual = json_map.get(\"_type\")\n    if actual != cls.__name__:\n        raise ValueError(\n            f\"_type {actual!r} does not match {cls.__name__}; dispatch on _type instead\"\n        )\n    return cls.from_json(json_map)\n\ndef from_json_dispatch(json_map):\n    import chromadb.api.configuration as m\n    target = getattr(m, json_map[\"_type\"], None)\n    if target is None:\n        raise ValueError(f\"unknown configuration type {json_map['_type']!r}\")\n    return target.from_json(json_map)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Dispatch deserialization on the _type discriminator instead of hardcoding a class","Round-trip configs only through to_json/from_json of the same version","Match class names exactly, including case"],"tags":["chromadb","configuration","serialization","type-discriminator"],"backgroundTag":"configuration-validation-failed","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}