{"record":{"id":"aafd6a5d957b297d","repo":"microsoft/semantic-kernel","slug":"subscription-already-exists","errorCode":null,"errorMessage":"Subscription already exists","messagePattern":"Subscription already exists","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/runtime/in_process/runtime_impl_helpers.py","lineNumber":57,"sourceCode":"class SubscriptionManager:\n    \"\"\"Manages subscriptions for agents.\"\"\"\n\n    def __init__(self) -> None:\n        \"\"\"Initialize the SubscriptionManager.\"\"\"\n        self._subscriptions: list[Subscription] = []\n        self._seen_topics: set[TopicId] = set()\n        self._subscribed_recipients: DefaultDict[TopicId, list[AgentId]] = defaultdict(list)\n\n    @property\n    def subscriptions(self) -> Sequence[Subscription]:\n        \"\"\"Get the list of subscriptions.\"\"\"\n        return self._subscriptions\n\n    async def add_subscription(self, subscription: Subscription) -> None:\n        \"\"\"Add a subscription to the manager.\"\"\"\n        # Check if the subscription already exists\n        if any(sub == subscription for sub in self._subscriptions):\n            raise ValueError(\"Subscription already exists\")\n\n        self._subscriptions.append(subscription)\n        self._rebuild_subscriptions(self._seen_topics)\n\n    async def remove_subscription(self, id: str) -> None:\n        \"\"\"Remove a subscription from the manager.\"\"\"\n        # Check if the subscription exists\n        if not any(sub.id == id for sub in self._subscriptions):\n            raise ValueError(\"Subscription does not exist\")\n\n        def is_not_sub(x: Subscription) -> bool:\n            return x.id != id\n\n        self._subscriptions = list(filter(is_not_sub, self._subscriptions))\n\n        # Rebuild the subscriptions\n        self._rebuild_subscriptions(self._seen_topics)\n","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/runtime/in_process/runtime_impl_helpers.py#L39-L75","documentation":"The subscription manager's add_subscription checks for duplicates by comparing the new subscription against all existing ones using __eq__. If an identical subscription (same id, topic, agent_type) already exists, ValueError is raised. This prevents duplicate routing entries that would cause double-delivery.","triggerScenarios":"Calling runtime.add_subscription(DefaultSubscription(...)) twice with identical parameters. Also triggered by the @default_subscription decorator combined with an explicit add_subscription call for the same agent type and topic.","commonSituations":"A registration helper that runs on every test or every app start without checking existing subscriptions. Combining decorator-based subscriptions with manual add_subscription calls. Module-level subscription setup that runs on import in a hot-reload scenario.","solutions":["Track which subscriptions have been added and skip duplicates in your own code.","Call remove_subscription before re-adding if you need to refresh a subscription.","Create a fresh runtime instance for each test rather than reusing one with accumulated subscriptions.","Use unique topic_type values if you intentionally need multiple subscriptions."],"exampleFix":"# before\nawait runtime.add_subscription(DefaultSubscription(agent_type='my_agent'))\nawait runtime.add_subscription(DefaultSubscription(agent_type='my_agent'))  # raises\n\n# after\nawait runtime.add_subscription(DefaultSubscription(agent_type='my_agent'))\n# only add once; or remove first:\nawait runtime.remove_subscription(existing_id)\nawait runtime.add_subscription(DefaultSubscription(agent_type='my_agent'))","handlingStrategy":"validation","validationCode":"# Track existing subscription IDs before adding\nexisting_sub_ids: set[str] = set()\nasync def safe_add_subscription(runtime, subscription):\n    if subscription.id in existing_sub_ids:\n        return  # already added\n    await runtime.add_subscription(subscription)\n    existing_sub_ids.add(subscription.id)","typeGuard":"null","tryCatchPattern":"try:\n    await runtime.add_subscription(subscription)\nexcept ValueError:\n    pass  # duplicate — acceptable in idempotent setup","preventionTips":["Track subscription IDs in your own code to avoid duplicate add_subscription calls.","Create a fresh runtime per test/session rather than accumulating subscriptions.","Use remove_subscription before re-adding if refreshing a subscription.","Make subscription setup idempotent by catching ValueError."],"tags":["agent-runtime","subscription","duplicate","registration","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}