{"id":"ce0bb2c3c10104d0","repo":"pydantic/pydantic","slug":"config-both","errorCode":"config-both","errorMessage":"\"Config\" and \"model_config\" cannot be used together","messagePattern":"\"Config\" and \"model_config\" cannot be used together","errorType":"error_code","errorClass":"PydanticUserError","httpStatus":null,"severity":"error","filePath":"pydantic/_internal/_config.py","lineNumber":172,"sourceCode":"            A `ConfigWrapper` instance for `BaseModel`.\n        \"\"\"\n        config_new = ConfigDict()\n        for base in bases:\n            config = getattr(base, 'model_config', None)\n            if config:\n                config_new.update(config.copy())\n\n        config_class_from_namespace = namespace.get('Config')\n        config_dict_from_namespace = namespace.get('model_config')\n\n        if raw_annotations.get('model_config') and config_dict_from_namespace is None:\n            raise PydanticUserError(\n                '`model_config` cannot be used as a model field name. Use `model_config` for model configuration.',\n                code='model-config-invalid-field-name',\n            )\n\n        if config_class_from_namespace and config_dict_from_namespace:\n            raise PydanticUserError('\"Config\" and \"model_config\" cannot be used together', code='config-both')\n\n        config_from_namespace = config_dict_from_namespace or prepare_config(config_class_from_namespace)\n\n        config_new.update(config_from_namespace)\n\n        for k in list(kwargs.keys()):\n            if k in config_keys:\n                config_new[k] = kwargs.pop(k)\n\n        return cls(config_new)\n\n    # we don't show `__getattr__` to type checkers so missing attributes cause errors\n    if not TYPE_CHECKING:  # pragma: no branch\n\n        def __getattr__(self, name: str) -> Any:\n            try:\n                return self.config_dict[name]\n            except KeyError:","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/pydantic/pydantic/blob/2e5f0e2b4218de31709f1cf9c5bc61ea97a68835/pydantic/_internal/_config.py#L154-L190","documentation":"Pydantic V2 supports two ways to configure a model: the legacy V1 inner `class Config` and the new `model_config = ConfigDict(...)`. They are mutually exclusive. In ConfigWrapper.for_model (line 171-172), if both `Config` and `model_config` are present in the class namespace, PydanticUserError with code config-both is raised because mixing them creates ambiguity about which settings win.","triggerScenarios":"Defining a model that contains both an inner `class Config:` (V1 style) and a `model_config = ConfigDict(...)` (V2 style) at the same class level. Also occurs via inheritance if a base defines one style and the subclass adds the other in the same namespace block being processed.","commonSituations":"Partially migrating a V1 model: developer adds model_config but forgets to delete the old Config class; copy-pasting a V1 model and appending V2 config on top.","solutions":["Remove the legacy `class Config:` block and keep only `model_config = ConfigDict(...)`.","Alternatively, keep `class Config:` only (deprecated, warns) and remove model_config — but the recommended path is the V2 ConfigDict.","If config came from a base class, override it consistently in one style in the subclass."],"exampleFix":"# before\nclass M(BaseModel):\n    class Config:\n        frozen = True\n    model_config = ConfigDict(str_strip_whitespace=True)\n\n# after\nclass M(BaseModel):\n    model_config = ConfigDict(frozen=True, str_strip_whitespace=True)","handlingStrategy":"validation","validationCode":"def check_single_config_style(namespace: dict) -> None:\n    if 'Config' in namespace and 'model_config' in namespace:\n        raise TypeError('Define either Config or model_config, not both')","typeGuard":"def uses_single_config_style(cls_dict: dict) -> bool:\n    return not ('Config' in cls_dict and 'model_config' in cls_dict)","tryCatchPattern":"try:\n    class M(BaseModel): ...\nexcept PydanticUserError as e:\n    if e.code == 'config-both':\n        # delete the inner class Config and keep model_config\n        raise\n    raise","preventionTips":["Standardize on model_config = ConfigDict(...) and delete any V1 Config class.","During migration, search for 'class Config' inside model definitions and remove them.","Do not mix config styles across base and subclass."],"tags":["python","pydantic","config","v2-migration","config-both","reserved-name"],"analyzedSha":"2e5f0e2b4218de31709f1cf9c5bc61ea97a68835","analyzedAt":"2026-08-04T19:54:21.281Z","schemaVersion":2}