Skip to content

Default values for python dictionaries are incorrectly rendered #3045

@ashipilov

Description

@ashipilov

Describe the bug
Default values for objects with additionalProperties mapped to python dictionaries are incorrectly rendered in the generated pydantic models.

To Reproduce

  1. Save schema provided below as schema.json
  2. Generate the code using command datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py
  3. Incorrect schema is generated, see Expected behavior for details

Example schema:

{
  "$defs": {
    "ItemModel": {
      "properties": {
        "name": {
          "title": "Name",
          "type": "string"
        },
        "description": {
          "title": "Description",
          "type": "string"
        }
      },
      "required": [
        "name",
        "description"
      ],
      "title": "ItemModel",
      "type": "object"
    }
  },
  "properties": {
    "dict_with_defaults": {
      "additionalProperties": {
        "$ref": "#/$defs/ItemModel"
      },
      "default": {},
      "title": "Dict With Defaults",
      "type": "object"
    }
  },
  "title": "ParentModel",
  "type": "object"
}

Used commandline:

$ datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py

Expected behavior

Currently the following models are generated as following:

class ItemModel(BaseModel):
    name: str = Field(..., title='Name')
    description: str = Field(..., title='Description')


class ParentModel(BaseModel):
    dict_with_defaults: dict[str, ItemModel] | None = Field(
        default_factory=lambda: ItemModel.model_validate({}), title='Dict With Defaults'
    )

dict_with_defaults has incorrect default_factory, because it tries to validate the default as an instance of value class instead of a dictionary.
Expected line:

dict_with_defaults: dict[str, ItemModel] | None = Field(
        default_factory=dict, title='Dict With Defaults'
    )

Version:

  • OS: MacOs 26.2
  • Python version: 3.13
  • datamodel-code-generator version: 0.54.1

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions