{"record":{"id":"e9b1c63e1bb839bf","repo":"run-llama/llama_index","slug":"cannot-add-two-handlers-of-the-same-type-type-new","errorCode":null,"errorMessage":"Cannot add two handlers of the same type {type(new_handler)} to the callback manager.","messagePattern":"Cannot add two handlers of the same type (.+?) to the callback manager\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/callbacks/base.py","lineNumber":70,"sourceCode":"            ...\r\n            event.on_end(payload={key, val})\r\n\r\n    \"\"\"\r\n\r\n    def __init__(self, handlers: Optional[List[BaseCallbackHandler]] = None):\r\n        \"\"\"Initialize the manager with a list of handlers.\"\"\"\r\n        from llama_index.core import global_handler\r\n\r\n        handlers = handlers or []\r\n\r\n        # add eval handlers based on global defaults\r\n        if global_handler is not None:\r\n            new_handler = global_handler\r\n            # go through existing handlers, check if any are same type as new handler\r\n            # if so, error\r\n            for existing_handler in handlers:\r\n                if isinstance(existing_handler, type(new_handler)):\r\n                    raise ValueError(\r\n                        \"Cannot add two handlers of the same type \"\r\n                        f\"{type(new_handler)} to the callback manager.\"\r\n                    )\r\n            handlers.append(new_handler)\r\n\r\n        # if we passed in no handlers, use the global default\r\n        if len(handlers) == 0:\r\n            from llama_index.core.settings import Settings\r\n\r\n            # hidden var access to prevent recursion in getter\r\n            cb_manager = Settings._callback_manager\r\n            if cb_manager is not None:\r\n                handlers = cb_manager.handlers\r\n\r\n        self.handlers: List[BaseCallbackHandler] = handlers\r\n        self._trace_map: Dict[str, List[str]] = defaultdict(list)\r\n\r\n    def on_event_start(\r","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/callbacks/base.py#L52-L88","documentation":"Raised by CallbackManager.__init__ when a global handler (set via llama_index.core.global_handler) has the same type as one of the handlers passed explicitly. LlamaIndex forbids two handlers of the same type in one manager to avoid double-counting traced events (e.g. duplicate token counts or spans).","triggerScenarios":"Setting a global handler (e.g. ArgillaInstrumentor or any BaseCallbackHandler via set_global_handler) and then constructing CallbackManager(handlers=[SameTypeHandler(...)]); commonly triggered implicitly when any component builds a fresh CallbackManager while a global handler is active.","commonSituations":"Enabling observability globally (set_global_handler) in a notebook/app that also passes its own handler instance into LlamaIndex constructors; upgrading to a version where global-handler merging became strict; initializing the same instrumentor twice (startup + config reload).","solutions":["Remove the duplicate: if a global handler of type T is set, do not also pass a T instance into CallbackManager — rely on the global one.","Clear the global handler before constructing managers with your own handler of the same type (set_global_handler(None) / unset before init).","Check for double initialization of instrumentation at startup (config reload, notebook re-runs) and guard with an if-not-already-set flag."],"exampleFix":"# before\nfrom llama_index.core import set_global_handler\nset_global_handler(\"argilla\")\ncb = CallbackManager(handlers=[ArgillaInstrumentor().get_callback_handler()])  # duplicate type\n\n# after\nset_global_handler(\"argilla\")\ncb = CallbackManager(handlers=[])  # global handler is merged automatically","handlingStrategy":"validation","validationCode":"from llama_index.core import global_handler\nif global_handler is not None:\n    handler_types = {type(h) for h in my_handlers}\n    if type(global_handler) in handler_types:\n        my_handlers = [h for h in my_handlers if type(h) is not type(global_handler)]\ncb = CallbackManager(handlers=my_handlers)","typeGuard":null,"tryCatchPattern":"try:\n    cb = CallbackManager(handlers=my_handlers)\nexcept ValueError as e:\n    if \"same type\" in str(e):\n        my_handlers = [h for h in my_handlers if type(h) is not type(global_handler)]\n        cb = CallbackManager(handlers=my_handlers)\n    else:\n        raise","preventionTips":["Choose one instrumentation mechanism: global handler OR explicit handlers, not both.","Guard observability setup against double initialization on reloads."],"tags":["llama-index","callbacks","observability","config","duplicates"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}