{"record":{"id":"d7134a3a65cf730b","repo":"agentscope-ai/agentscope","slug":"factory-must-be-a-callable-got-type-factory-n","errorCode":null,"errorMessage":"factory must be a callable, got {type(factory).__name__}","messagePattern":"factory must be a callable, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/agentscope/_utils/_common.py","lineNumber":45,"sourceCode":"    startup to substitute a different strategy.\n\n    .. note::\n        Security-sensitive tokens (gateway tokens, Redis lock tokens)\n        are **not** affected and always use ``uuid.uuid4().hex``.\n\n    Args:\n        factory (`Callable[[], str]`):\n            A no-arg callable returning a string ID.\n\n    Raises:\n        TypeError: If ``factory`` is not callable.\n\n    Example:\n        >>> from agentscope import set_id_factory\n        >>> set_id_factory(lambda: uuid7().hex)\n    \"\"\"\n    if not callable(factory):\n        raise TypeError(\n            f\"factory must be a callable, got {type(factory).__name__}\",\n        )\n    global _id_factory\n    _id_factory = factory\n\n\ndef set_timestamp_factory(factory: Callable[[], str]) -> None:\n    \"\"\"Override the global timestamp factory used by all AgentScope entities.\n\n    Args:\n        factory (`Callable[[], str]`):\n            A no-arg callable returning a string ID.\n\n    Raises:\n        TypeError: If ``factory`` is not callable.\n    \"\"\"\n    if not callable(factory):\n        raise TypeError(","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/_utils/_common.py#L27-L63","documentation":"agentscope's set_id_factory installs a global factory used to generate IDs for entities (messages, blocks, etc.). It requires a no-argument callable; anything non-callable (a string, a class instance, None) raises this TypeError immediately as a fail-fast guard. The factory set here affects every entity ID generated afterwards.","triggerScenarios":"Calling set_id_factory with a non-callable, e.g. set_id_factory(\"uuid4\"), set_id_factory(uuid4()), or set_id_factory(None). Tests like test_custom_factory_affects_entities rely on this contract when injecting custom factories (e.g. lambda: uuid7().hex).","commonSituations":"Passing the result of calling a factory instead of the factory itself (uuid7() instead of uuid7), passing a UUID string constant, or passing a class name as a string from config. Also hitting stale global state across tests because the factory is a module-level global.","solutions":["Pass the callable itself, not its result: set_id_factory(uuid7) or set_id_factory(lambda: uuid7().hex)","Verify with callable(factory) before setting it","If the value comes from config, wrap it: set_id_factory(eval-free resolver) or functools.partial for parameterized factories"],"exampleFix":"# before\nset_id_factory(uuid7().hex)  # called the function -> passed a str\n\n# after\nset_id_factory(lambda: uuid7().hex)","handlingStrategy":"type-guard","validationCode":"if not callable(factory):\n    raise TypeError(f\"expected a callable, got {type(factory).__name__}\")\nset_id_factory(factory)","typeGuard":"from typing import Callable, TypeGuard\n\ndef is_id_factory(v: object) -> TypeGuard[Callable[[], str]]:\n    return callable(v)","tryCatchPattern":"try:\n    set_id_factory(factory)\nexcept TypeError as e:\n    if \"must be a callable\" in str(e):\n        set_id_factory(lambda: uuid7().hex)  # sensible default\n    else:\n        raise","preventionTips":["Always pass the function object (uuid7), never its result (uuid7())","Wrap factories needing arguments in a lambda or functools.partial","In test suites, reset the factory between tests to avoid leaked global state"],"tags":["typeerror","configuration","callable","id-generation","python"],"backgroundTag":"argument-not-callable","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}