Backtestspydanticrun-20261027-165153#13617

Issue #13617 Merged fix PR #13623 Run 27 Oct 2026 · run-20261027-165153 pydantic/pydantic

Cannot create defauldict field with dict value type

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the root cause identically: adding `dict: dict` (and `frozenset: frozenset`) to the allowed-default-types mapping, and all independent tests pass on both. Patch A is a minimal two-line change plus focused tests covering bare and parametrized dict/frozenset values and the issue's exact model. Patch B does the same but also hoists the mapping to module level and adds speculative, version-gated code for Python 3.15 that references an undefined name `frozendict` (suppressed with `noqa: F821`) and silently remaps `collections.abc.Mapping` to `frozendict` on that version. That is unrelated to the issue, changes existing behaviour (Mapping-valued defaultdicts would yield immutable values on 3.15), and risks a NameError at import time if `frozendict` is not a builtin there; the test file carries the same speculative reference. B's correction of 'keys' to 'values' in the error message and its deterministic ordering of the allowed-types list are small genuine improvements, and its parametrized test rewrite is reasonable, but they do not outweigh the risk introduced by the 3.15 branch. A resolves the issue cleanly with no added risk.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

4m 7s

Wall-clock time in the container

Full test suite

No new failures

385 passed · 4 skipped · 389 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

33vs 89 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

For now it's impossible to create a model with defaultdict(dict) value.
It's possible to create with Mapping and MutableMapping types, then why not add the basic simple type dict? Other basic types are allowed too.

The corresponding code is located here.

Example Code

from collections import defaultdict
from pydantic import BaseModel

class Test(BaseModel):
    prop: defaultdict[str, dict] = defaultdict(dict)
# ...
# pydantic.errors.PydanticSchemaGenerationError: Unable to infer a default factory for keys of type <class 'dict'>. Only float, frozenset, int, dict, str, set, list, tuple, bool are supported, other types require an explicit default factory set using `DefaultDict[..., Annotated[..., Field(default_factory=...)]]`

Python, Pydantic & OS Version

             pydantic version: 2.13.4
        pydantic-core version: 2.46.4
          pydantic-core build: profile=release pgo=false
               python version: 3.11.9 (tags/v3.11.9:de54cf5, Apr  2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
                     platform: Windows-10-10.0.26200-SP0
             related packages: fastapi-0.116.0 pydantic-settings-2.9.1 typing_extensions-4.15.0
                       commit: unknown

The two patches

Our patch+33 −0
pydantic/_internal/_validators.pyindex 729c5fae..5562206c 100644@@ -428,7 +428,9 @@ def get_defaultdict_default_default_factory(values_source_type: Any) -> Callable             list: list,             set: set,             collections.abc.MutableSet: set,+            frozenset: frozenset,             collections.abc.Set: frozenset,+            dict: dict,             collections.abc.Mapping: dict,             collections.abc.MutableMapping: dict,             float: float,tests/types/test_defaultdict.pyindex 67131324..b7c56a63 100644@@ -6,6 +6,7 @@ from pydantic_core import CoreSchema, core_schema from typing_extensions import get_args  # noqa: UP035 (for `get_args`)  from pydantic import (+    BaseModel,     Field,     GetCoreSchemaHandler,     PydanticSchemaGenerationError,@@ -51,6 +52,36 @@ def test_defaultdict_infer_default_factory() -> None:     assert v.default_factory() == set()  +@pytest.mark.parametrize(+    ('value_type', 'expected_default'),+    [+        (dict, {}),+        (dict[str, int], {}),+        (frozenset, frozenset()),+        (frozenset[int], frozenset()),+    ],+)+def test_defaultdict_infer_default_factory_builtin_containers(value_type: Any, expected_default: Any) -> None:+    """https://github.com/pydantic/pydantic/issues/13617"""+    ta = TypeAdapter(defaultdict[str, value_type])+    v = ta.validate_python({})+    assert v.default_factory is not None+    assert v.default_factory() == expected_default+    assert type(v['missing']) is type(expected_default)+++def test_defaultdict_dict_value_model_field() -> None:+    """https://github.com/pydantic/pydantic/issues/13617"""++    class Model(BaseModel):+        prop: defaultdict[str, dict] = defaultdict(dict)++    m = Model(prop={'a': {'x': 1}})+    assert m.prop['a'] == {'x': 1}+    assert m.prop['b'] == {}+    assert m.prop.default_factory is dict++ def test_defaultdict_explicit_default_factory() -> None:     class MyList(list[int]):         pass 
Merged fix · PR #13623+54 −35
pydantic/_internal/_validators.pyindex 729c5fae4..9ce71efe7 100644@@ -8,6 +8,7 @@ from __future__ import annotations as _annotations import collections.abc import math import re+import sys import typing from collections.abc import Callable, Sequence from decimal import Decimal@@ -415,27 +416,35 @@ def defaultdict_validator(         return collections.defaultdict(default_default_factory, handler(input_value))  +_defaultdict_allowed_default_types: dict[type[Any], type[Any]] = {+    float: float,+    int: int,+    str: str,+    bool: bool,+    tuple: tuple,+    collections.abc.Sequence: tuple,+    collections.abc.MutableSequence: list,+    list: list,+    set: set,+    collections.abc.MutableSet: set,+    frozenset: frozenset,+    collections.abc.Set: frozenset,+    dict: dict,+    collections.abc.Mapping: dict,+    collections.abc.MutableMapping: dict,+}++if sys.version_info >= (3, 15):+    _defaultdict_allowed_default_types[collections.abc.Mapping] = frozendict  # noqa: F821+    _defaultdict_allowed_default_types[frozendict] = frozendict  # noqa: F821++ def get_defaultdict_default_default_factory(values_source_type: Any) -> Callable[[], Any]:     FieldInfo = import_cached_field_info()      values_type_origin = get_origin(values_source_type)      def infer_default() -> Callable[[], Any]:-        allowed_default_types: dict[Any, Any] = {-            tuple: tuple,-            collections.abc.Sequence: tuple,-            collections.abc.MutableSequence: list,-            list: list,-            set: set,-            collections.abc.MutableSet: set,-            collections.abc.Set: frozenset,-            collections.abc.Mapping: dict,-            collections.abc.MutableMapping: dict,-            float: float,-            int: int,-            str: str,-            bool: bool,-        }         values_type = values_type_origin or values_source_type         instructions = 'set using `DefaultDict[..., Annotated[..., Field(default_factory=...)]]`'         if typing_objects.is_typevar(values_type):@@ -447,15 +456,15 @@ def get_defaultdict_default_default_factory(values_source_type: Any) -> Callable                 )              return type_var_default_factory-        elif values_type not in allowed_default_types:+        elif values_type not in _defaultdict_allowed_default_types:             # a somewhat subjective set of types that have reasonable default values-            allowed_msg = ', '.join([t.__name__ for t in set(allowed_default_types.keys())])+            allowed_msg = ', '.join([t.__name__ for t in _defaultdict_allowed_default_types.keys()])             raise PydanticSchemaGenerationError(-                f'Unable to infer a default factory for keys of type {values_source_type}.'+                f'Unable to infer a default factory for values of type {values_source_type}.'                 f' Only {allowed_msg} are supported, other types require an explicit default factory'                 ' ' + instructions             )-        return allowed_default_types[values_type]+        return _defaultdict_allowed_default_types[values_type]      # Assume Annotated[..., Field(...)]     if typing_objects.is_annotated(values_type_origin):tests/types/test_defaultdict.pyindex 671313244..b688da7f4 100644@@ -1,3 +1,4 @@+import sys from collections import defaultdict from typing import Annotated, Any, TypeVar @@ -24,31 +25,40 @@ def test_typing_coercion_defaultdict():   def test_defaultdict_unknown_default_factory() -> None:-    """-    https://github.com/pydantic/pydantic/issues/4687-    """+    """https://github.com/pydantic/pydantic/issues/4687"""     with pytest.raises(         PydanticSchemaGenerationError,-        match=r'Unable to infer a default factory for keys of type collections.defaultdict\[int, int\]',+        match=r'Unable to infer a default factory for values of type collections.defaultdict\[int, int\]',     ):         TypeAdapter(defaultdict[int, defaultdict[int, int]])  -def test_defaultdict_infer_default_factory() -> None:-    ta_list = TypeAdapter(defaultdict[int, list[int]])-    v = ta_list.validate_python({})-    assert v.default_factory is not None-    assert v.default_factory() == []+defaultdict_types_params = [+    (int, 0),+    (float, 0.0),+    (str, ''),+    (bool, False),+    (tuple, ()),+    (list[int], []),+    (list, []),+    (set, set()),+    (frozenset, frozenset()),+    (dict, {}),+] -    ta_int = TypeAdapter(defaultdict[int, int])-    v = ta_int.validate_python({})-    assert v.default_factory is not None-    assert v.default_factory() == 0+if sys.version_info >= (3, 15):+    defaultdict_types_params.append((frozendict, frozendict()))  # noqa: F821 -    ta_set = TypeAdapter(defaultdict[int, set])-    v = ta_set.validate_python({})++@pytest.mark.parametrize(+    ['default_factory_type', 'value'],+    defaultdict_types_params,+)+def test_defaultdict_infer_default_factory(default_factory_type: Any, value: Any) -> None:+    ta_list = TypeAdapter(defaultdict[int, default_factory_type])+    v = ta_list.validate_python({})     assert v.default_factory is not None-    assert v.default_factory() == set()+    assert v.default_factory() == value   def test_defaultdict_explicit_default_factory() -> None: