Skip to content

Pydantic nested dict in RootModel does not validate correctly #2819

Closed

Description

@sbusch-mobivention

Describe the Bug

Given the following bit of code:

from pydantic import BaseModel, RootModel


class InnerModel(BaseModel):
    a: int


InnerModelDict = RootModel[dict[str, InnerModel]]


class OuterModel(BaseModel):
    data: InnerModelDict


def serialize(d: InnerModelDict) -> OuterModel:
    return OuterModel(data=d)


if __name__ == "__main__":
    print(serialize(InnerModelDict({"a": InnerModel(a=1)})))

This works without issues with pydantic and I dont see any actual errors in the typing, however pyrefly reports

ERROR Argument `RootModel[dict[str, InnerModel]]` is not assignable to parameter `data` with type `Mapping[str, Any] | RootModel[Mapping[str, Any]]` in function `OuterModel.__init__` [bad-argument-type]
  --> test.py:16:28
   |
16 |     return OuterModel(data=d)
   |                            ^
   |

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Activity

  1. self-assigned this
    on Mar 17, 2026
  2. migeed-z commented on Mar 17, 2026

    @migeed-z
    Contributor

    Thank you for the report. I will take a look!

  3. github-actions commented on Apr 9, 2026

    @github-actions

    This issue has someone assigned, but has not had recent activity for more than 2 weeks.

    If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.

    Thank you for your contributions!

  4. added a commit that references this issue on Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions