microsoft/autogen · error · ValueError
Factory registered using the wrong type.
Error message
Factory registered using the wrong type.
What it means
register_factory accepts an optional expected_class parameter. When the wrapped factory runs and the produced object's type() does not equal expected_class, the wrapper raises ValueError('Factory registered using the wrong type.'). This is a programmatic assertion that the declared type matches what the factory actually constructs, catching mismatches before the agent enters message processing.
Source
Thrown at python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py:736
expected_class: type[T] | None = None,
) -> AgentType:
if isinstance(type, str):
type = AgentType(type)
if type.type in self._agent_factories:
raise ValueError(f"Agent with type {type} already exists.")
if self._host_connection is None:
raise RuntimeError("Host connection is not set.")
async def factory_wrapper() -> T:
maybe_agent_instance = agent_factory()
if inspect.isawaitable(maybe_agent_instance):
agent_instance = await maybe_agent_instance
else:
agent_instance = maybe_agent_instance
if expected_class is not None and type_func_alias(agent_instance) != expected_class:
raise ValueError("Factory registered using the wrong type.")
return agent_instance
self._agent_factories[type.type] = factory_wrapper
# Send the registration request message to the host.
await self._register_agent_type(type.type)
return type
async def register_agent_instance(
self,
agent_instance: Agent,
agent_id: AgentId,
) -> AgentId:
def agent_factory() -> Agent:
raise RuntimeError(
"Agent factory was invoked for an agent instance that was not registered. This is likely due to the agent type being incorrectly subscribed to a topic. If this exception occurs when publishing a message to the DefaultTopicId, then it is likely that `skip_class_subscriptions` needs to be turned off when registering the agent."
)View on GitHub (pinned to 027ecf0a37)
Solutions
- Pass expected_class that is exactly the class the factory instantiates (or omit expected_class entirely if you rely on subclassing)
- Update expected_class after refactoring the factory's return type
- Fix the factory so it cannot return None or an alternative class on any code path
- If subclass support is needed, drop expected_class and do your own isinstance validation inside the factory
Example fix
# before
await runtime.register_factory(
'agent', lambda: MyAgent(), expected_class=BaseAgent # ValueError: MyAgent != BaseAgent
)
# after
await runtime.register_factory('agent', lambda: MyAgent(), expected_class=MyAgent)
# or simply omit expected_class Defensive patterns
Strategy: validation
Validate before calling
def factory_ok(factory, expected_class) -> bool:
import inspect
if inspect.isawaitable(factory):
return False
return True # verify by trial instantiation instead:
# trial = factory(); assert type(trial) is expected_class Try / catch
try:
agent = await runtime._get_agent(agent_id) # triggers factory_wrapper
except ValueError as e:
if 'wrong type' in str(e):
raise TypeError('expected_class does not match factory output') from e
raise Prevention
- Pass expected_class equal to the exact class the factory constructs, or omit it
- Trial-instantiate the factory in tests and assert type(obj) is expected_class
- Remember the check is exact type equality — subclasses fail
- Update expected_class whenever the factory's return class changes
When it happens
Trigger: Passing expected_class=X while the factory returns a subclass, a different class, or None; type_func_alias(agent_instance) (the instance's concrete type) differing from expected_class — note subclass instances fail because the comparison is exact type equality, not isinstance; factories whose return value depends on runtime state and can return different classes.
Common situations: Registering with expected_class=BaseAgent while the factory builds MyAgent(BaseAgent) — exact-type comparison rejects it; refactoring that changes what a factory returns without updating expected_class; factories returning None on a failed initialization path.
Related errors
- Factory registered using the wrong type: expected {expected_
- Agent factories and agent instances cannot be registered to
- Agent instances must be the same object type.
- Unexpected message type: {message?.GetType()}
- Cannot convert state of type {this.deserializedValue.GetType
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/4621bbd747ff731c.
Report an issue: GitHub.