{"record":{"id":"2c300568b30c6660","repo":"mlflow/mlflow","slug":"the-two-merging-dictionaries-contains-duplicate-ke","errorCode":null,"errorMessage":"The two merging dictionaries contains duplicate keys: {duplicate_keys}.","messagePattern":"The two merging dictionaries contains duplicate keys: (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"mlflow/utils/__init__.py","lineNumber":132,"sourceCode":"    return truncated\n\n\ndef merge_dicts(dict_a, dict_b, raise_on_duplicates=True):\n    \"\"\"This function takes two dictionaries and returns one singular merged dictionary.\n\n    Args:\n        dict_a: The first dictionary.\n        dict_b: The second dictionary.\n        raise_on_duplicates: If True, the function raises ValueError if there are duplicate keys.\n            Otherwise, duplicate keys in `dict_b` will override the ones in `dict_a`.\n\n    Returns:\n        A merged dictionary.\n\n    \"\"\"\n    duplicate_keys = dict_a.keys() & dict_b.keys()\n    if raise_on_duplicates and len(duplicate_keys) > 0:\n        raise ValueError(f\"The two merging dictionaries contains duplicate keys: {duplicate_keys}.\")\n    return dict_a | dict_b\n\n\ndef _get_fully_qualified_class_name(obj):\n    \"\"\"\n    Obtains the fully qualified class name of the given object.\n    \"\"\"\n    return obj.__class__.__module__ + \".\" + obj.__class__.__name__\n\n\ndef _inspect_original_var_name(var, fallback_name):\n    \"\"\"\n    Inspect variable name, will search above frames and fetch the same instance variable name\n    in the most outer frame.\n    If inspect failed, return fallback_name\n    \"\"\"\n    if var is None:\n        return fallback_name","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/utils/__init__.py#L114-L150","documentation":"mlflow.utils.merge_dicts merges two dicts but, when raise_on_duplicates=True (the default), refuses to silently overwrite keys present in both inputs. It raises ValueError listing the duplicate keys so data loss is explicit.","triggerScenarios":"Calling merge_dicts(dict_a, dict_b) where the dicts share one or more keys, e.g. merging two params/config dicts built from overlapping sources; also directly in tests.","commonSituations":"Merging default and user config that share keys; combining run params from two runs with overlapping names; environment-derived dicts colliding with defaults.","solutions":["Remove the duplicate keys from one dict before merging","Pass raise_on_duplicates=False if last-wins (dict_b) overwrite is acceptable","Rename keys to make them unique before merging"],"exampleFix":"// before\nmerged = merge_dicts(defaults, user_cfg)  # KeyError-free but ValueError on dup keys\n// after\nmerged = merge_dicts(defaults, user_cfg, raise_on_duplicates=False)  # or de-duplicate user_cfg first","handlingStrategy":"try-catch","validationCode":"dups = dict_a.keys() & dict_b.keys()\nif dups:\n    print(f\"Duplicate keys will raise: {dups}\")","typeGuard":null,"tryCatchPattern":"try:\n    merged = merge_dicts(dict_a, dict_b)\nexcept ValueError as e:\n    # decide policy: last-wins merge\n    merged = {**dict_a, **dict_b}","preventionTips":["De-duplicate config sources before merging","Pass raise_on_duplicates=False deliberately when overwrite is intended","Log duplicate keys in config pipelines"],"tags":["mlflow","utils","dictionaries","duplicate-keys","valueerror"],"backgroundTag":"duplicate-key-conflict","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}