Skip to content

[Bug] Structured config with dict as default value does not handle overrides correctly #2350

Closed

Description

@rsokl

🐛 Bug

Description

A structured config with a dictionary-valued default (specified via default factory) will not instantiate properly when that field is overridden with another dictionary during instantiation. The override will be merged with the default value during instantiation.

I found that this error manifests for hydra 1.1.0-1.2.0 and omegaconf 2.1.0 - 2.2.3. The error seems to be particular to dictionaries.

Checklist

  • I checked on the latest version of Hydra
  • I created a minimal repro (See this for tips).

To reproduce

from dataclasses import field, dataclass
from typing import Any, List, Dict

from hydra.utils import instantiate

def f(x): return x

@dataclass
class Buildsf:
    _target_: str = '__main__.f'
    x: Dict[str, int] = field(default_factory=lambda: dict({"K_DEFAULT": 10101}))

assert instantiate(Buildsf) == {"K_DEFAULT": 10101}  # ok
assert instantiate(Buildsf, x=dict(K_DEFAULT=2)) == {"K_DEFAULT": 2}  # ok
assert instantiate(Buildsf(x=dict(a=1))) == {"a": 1}  # ok

assert instantiate(Buildsf, x=dict(a=1)) ==  {"a": 1} # error: instantiation actually returns {"K_DEFAULT": 10101, "a": 1}

Minimal repro using hydra-zen :

from hydra_zen import make_config, instantiate
C = make_config(x={'a': 1})
expected = {'b':2}
actual = instantiate(C, x=expected).x

assert actual == expected, actual
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [1], in <cell line: 6>()
      3 expected = {'b':2}
      4 actual = instantiate(C, x=expected).x
----> 6 assert actual == expected, actual

AssertionError: {'a': 1, 'b': 2}

Expected Behavior

All of the above assertions should hold

System information

  • Hydra Version : 1.2.0
  • Python version : 3.9.13
  • Virtual environment type and version : conda
  • Operating system : Windows 11

Activity

  1. LucaButera commented on Apr 24, 2023

    @LucaButera

    Any update on this? I am currently facing the same issue

  2. omry commented on Jul 3, 2026

    @omry
    Collaborator

    Thanks for reporting this. I confirmed this still reproduces on current main.

    The current behavior comes from instantiate() recursively merging call-site kwargs into the config before instantiation. For normal Python call semantics, this is surprising: a kwarg passed to instantiate() should override/replace the configured argument value, not merge into it.

    We will handle this in Hydra 1.4 as a breaking bugfix: call-site kwargs should replace non-target argument values. We will preserve the documented nested-target patching behavior, e.g. instantiate(cfg.trainer, optimizer={"lr": 0.3}), where optimizer is itself an instantiate config.

    If we later want arbitrary call-site merge semantics, that should be explicit via a marker/wrapper rather than implicit recursive merging.

  3. added this to the Hydra 1.4.0 milestone on Jul 3, 2026
  4. added 13 commits that reference this issue on Aug 7, 2026
    7a9ed34
    22a2a51
    dd7ed75
    cab2f83
    acd48c6
    1173fe7
    64c88b5
    34b08c5
    bc134d1
    1252f1d
    520a86b
    1f4fbe0
    ca90fa7
  5. added a commit that references this issue on Aug 8, 2026
    991d2f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions