Backtestspydanticrun-20261009-110034#13617

Issue #13617 Merged fix PR #13623 Run 9 Oct 2026 · run-20261009-110034 pydantic/pydantic

Cannot create defauldict field with dict value type

Better

Our patch was judged better than the merged fix.

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

Both patches fix the root cause the same way: they add `dict: dict` (and `frozenset: frozenset`) to the allowed default-factory mapping, so `defaultdict[str, dict]` now infers `dict` as its factory.

Patch B does only that. Its tests cover the issue directly:
- A parametrized test over bare and parametrized `dict` and `frozenset`. It checks that `default_factory` is the expected type, that missing keys return the empty container, and that values are coerced to the right type.
- A test that reproduces the issue's exact `BaseModel` with `prop: defaultdict[str, dict] = defaultdict(dict)`, including mutation of a missing key.

These tests fail before the change and pass after it.

Patch A makes the same core fix but adds churn and risk:
- It moves the table to module level.
- It adds a Python ≥3.15 branch that remaps `collections.abc.Mapping` from `dict` to `frozendict`. This is a silent behavior change for existing `defaultdict[..., Mapping]` users on 3.15, unrelated to the issue. It also refers to a bare `frozendict` name with `noqa: F821`.
- It changes the error message wording from "keys" to "values". This is a reasonable correction but out of scope.
- It rewrites the existing infer-default test into a parametrized one. That test is adequate but does not exercise the issue's model scenario or parametrized `dict[str, X]`.

On correctness both resolve the issue. B's tests are more targeted, and A's extra Mapping→frozendict remap adds regression risk without being asked for. B is the cleaner, safer fix.

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

5m 56s

Wall-clock time in the container

Full test suite

No new failures

12,411 passed · 508 skipped · 12,919 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

35vs 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+35 −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..51881f8e 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,38 @@ def test_defaultdict_infer_default_factory() -> None:     assert v.default_factory() == set()  +@pytest.mark.parametrize(+    ('value_type', 'expected_factory'),+    [+        (dict, dict),+        (dict[str, int], dict),+        (frozenset, frozenset),+        (frozenset[int], frozenset),+    ],+    ids=['dict', 'dict-parametrized', 'frozenset', 'frozenset-parametrized'],+)+def test_defaultdict_infer_default_factory_builtin_containers(value_type: Any, expected_factory: Any) -> None:+    ta = TypeAdapter(defaultdict[str, value_type])+    v = ta.validate_python({'a': {}} if expected_factory is dict else {'a': []})+    assert v.default_factory is expected_factory+    assert v['missing'] == expected_factory()+    assert type(v['a']) is expected_factory+++def test_defaultdict_dict_value_model_field() -> None:+    class Model(BaseModel):+        prop: defaultdict[str, dict] = defaultdict(dict)++    m = Model()+    assert m.prop.default_factory is dict++    m = Model(prop={'a': {'x': 1}})+    assert m.prop.default_factory is dict+    assert m.prop['a'] == {'x': 1}+    m.prop['b']['y'] = 2+    assert m.prop['b'] == {'y': 2}++ 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: