{"record":{"id":"a4f9832e8f8f931a","repo":"microsoft/autogen","slug":"cannot-dump-component-with-local-class","errorCode":null,"errorMessage":"Cannot dump component with local class","messagePattern":"Cannot dump component with local class","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_component_config.py","lineNumber":166,"sourceCode":"        Raises:\n            TypeError: If the component is a local class.\n\n        Returns:\n            ComponentModel: The model representing the component.\n        \"\"\"\n        if self.component_provider_override is not None:\n            provider = self.component_provider_override\n        else:\n            provider = _type_to_provider_str(self.__class__)\n            # Warn if internal module name is used,\n            if \"._\" in provider:\n                warnings.warn(\n                    \"Internal module name used in provider string. This is not recommended and may cause issues in the future. Silence this warning by setting component_provider_override to this value.\",\n                    stacklevel=2,\n                )\n\n        if \"<locals>\" in provider:\n            raise TypeError(\"Cannot dump component with local class\")\n\n        if not hasattr(self, \"component_type\"):\n            raise AttributeError(\"component_type not defined\")\n\n        description = self.component_description\n        if description is None and self.__class__.__doc__:\n            # use docstring as description\n            docstring = self.__class__.__doc__.strip()\n            for marker in [\"\\n\\nArgs:\", \"\\n\\nParameters:\", \"\\n\\nAttributes:\", \"\\n\\n\"]:\n                docstring = docstring.split(marker)[0]\n            description = docstring.strip()\n\n        obj_config = self._to_config().model_dump(exclude_none=True)\n        model = ComponentModel(\n            provider=provider,\n            component_type=self.component_type,\n            version=self.component_version,\n            component_version=self.component_version,","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_component_config.py#L148-L184","documentation":"dump_component derives the provider string from the class's import path via _type_to_provider_str. If the class was defined inside a function (module path contains '<locals>'), the provider cannot round-trip: no import statement can reach it. dump_component raises TypeError('Cannot dump component with local class'). component_provider_override does not help — the check runs after it, but the override is only used if set; the guard applies to the derived provider.","triggerScenarios":"Defining a component class inside a function, pytest test body, or notebook cell scope, then calling dump_component(); dynamically created classes via type(...) inside a helper.","commonSituations":"Notebook prototypes declaring component classes inline; test-local components accidentally serialized; factory functions that create classes per call.","solutions":["Move the class to module top level so its provider becomes an importable 'package.module.Class'.","Set component_provider_override to an importable class path if the class must stay nested but is identical to an importable one.","Do not call dump_component on test-local/notebook-local classes; construct their configs directly."],"exampleFix":"# before\ndef make_component():\n    class LocalComponent(ComponentToConfig): ...\n    return LocalComponent().dump_component()  # TypeError\n\n# after\n# module scope: my_package/components.py\nclass LocalComponent(ComponentToConfig): ...\n\n# usage\nLocalComponent().dump_component()  # provider 'my_package.components.LocalComponent'","handlingStrategy":"type-guard","validationCode":"provider = f\"{type(comp).__module__}.{type(comp).__qualname__}\"\nif \"<locals>\" in provider:\n    raise TypeError(f\"cannot dump locally-defined class {type(comp).__qualname__}; move it to module scope\")","typeGuard":"def is_module_level_class(cls: type) -> bool:\n    return \"<locals>\" not in cls.__qualname__","tryCatchPattern":"try:\n    model = comp.dump_component()\nexcept TypeError as e:\n    if \"local class\" in str(e):\n        # reconstruct config manually for this nested class\n        raise\n    raise","preventionTips":["Define component classes at module top level, never inside functions or test bodies.","Set component_provider_override when a nested class mirrors an importable one.","Check '\"<locals>\" not in MyClass.__qualname__' in a lint/test rule for components."],"tags":["autogen-core","component-config","local-class","dump"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}