{"record":{"id":"cf83c0b4cd8c4c48","repo":"pydantic/pydantic","slug":"expected-a-class-got-type-param-r","errorCode":null,"errorMessage":"Expected a class, got {type_param!r}","messagePattern":"Expected a class, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pydantic/_internal/_generate_schema.py","lineNumber":1792,"sourceCode":"            else:\n                return self._type_schema()\n        elif is_union_origin(get_origin(type_param)):\n            return self._union_is_subclass_schema(type_param)\n        else:\n            if typing_objects.is_self(type_param):\n                type_param = self._resolve_self_type(type_param)\n            if _typing_extra.is_generic_alias(type_param):\n                raise PydanticUserError(\n                    'Subscripting `type[]` with an already parametrized type is not supported. '\n                    f'Instead of using type[{type_param!r}], use type[{_repr.display_as_type(get_origin(type_param))}].',\n                    code=None,\n                )\n            if not inspect.isclass(type_param):\n                # when using type[None], this doesn't type convert to type[NoneType], and None isn't a class\n                # so we handle it manually here\n                if type_param is None:\n                    return core_schema.is_subclass_schema(NoneType)\n                raise TypeError(f'Expected a class, got {type_param!r}')\n            return core_schema.is_subclass_schema(type_param)\n\n    def _sequence_schema(self, items_type: Any) -> core_schema.CoreSchema:\n        \"\"\"Generate schema for a Sequence, e.g. `Sequence[int]`.\"\"\"\n        from ._serializers import serialize_sequence_via_list\n\n        item_type_schema = self.generate_schema(items_type)\n        list_schema = core_schema.list_schema(item_type_schema)\n\n        json_schema = smart_deepcopy(list_schema)\n        python_schema = core_schema.is_instance_schema(typing.Sequence, cls_repr='Sequence')\n        if not typing_objects.is_any(items_type):\n            from ._validators import sequence_validator\n\n            python_schema = core_schema.chain_schema(\n                [python_schema, core_schema.no_info_wrap_validator_function(sequence_validator, list_schema)],\n            )\n","sourceCodeStart":1774,"sourceCodeEnd":1810,"githubUrl":"https://github.com/pydantic/pydantic/blob/cc13d1b8c978eaf78ed5308329cd41f03ecc3144/pydantic/_internal/_generate_schema.py#L1774-L1810","documentation":"A TypeError raised in _subclass_schema when the argument supplied to type[...] is neither a class nor None. After resolving Self types and rejecting generic aliases, the code calls inspect.isclass(type_param); if it is not a class (and is not None, which is special-cased to NoneType), it raises with the repr of the offending value. This is a low-level type-correctness guard, not a user-facing PydanticUserError.","triggerScenarios":"Annotating with type[<non-class>] such as type[42], type[\"SomeForwardRefString\"], type[lambda], or passing an instance/module where a class is expected. Forward references that failed to resolve into an actual class object also surface here.","commonSituations":"Passing a string forward reference inside type[...] that never gets resolved to a class; bugs in dynamic schema construction that pass arbitrary objects as the type argument; confusion between type[X] (class of X) and X itself.","solutions":["Ensure the argument to type[...] is an actual class object, not a string, instance, or callable.","If using a forward reference, make sure it resolves (import the class or call model_rebuild() after definition).","Replace type[value] with the class itself if you meant 'an instance of this class'.","For None, use type[None] explicitly (handled internally) rather than passing None as a runtime value."],"exampleFix":"// before\nhandler: type[\"MyHandler\"]  # string never resolved\n\n// after\nfrom myapp import MyHandler\nhandler: type[MyHandler]","handlingStrategy":"validation","validationCode":"import inspect, typing\n\ndef type_arg_is_class(tp) -> bool:\n    args = typing.get_args(tp)\n    if not args:\n        return True\n    inner = args[0]\n    return inner is None or inspect.isclass(inner)","typeGuard":"import inspect, typing\n\ndef is_valid_type_of(tp) -> bool:\n    if typing.get_origin(tp) is not type:\n        return True\n    inner = typing.get_args(tp)[0]\n    return inspect.isclass(inner) or inner is None","tryCatchPattern":"try:\n    Model.model_rebuild()\nexcept TypeError as e:\n    if 'Expected a class' in str(e):\n        # replace the non-class argument to type[...]\n        ...","preventionTips":["Ensure type[...] is parameterized with actual class objects, not strings or instances.","Resolve forward references before using them as type[...] arguments.","Static-check type[...] annotations with mypy/pyright to catch non-class arguments."],"tags":["pydantic","type-subscript","typeerror","schema-generation","invalid-type"],"backgroundTag":null,"analyzedSha":"cc13d1b8c978eaf78ed5308329cd41f03ecc3144","analyzedAt":"2026-08-11T16:38:52.905Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}