{"record":{"id":"4ee9421eff6ca2d0","repo":"microsoft/autogen","slug":"source-node-source-name-must-be-added-before-a","errorCode":null,"errorMessage":"Source node '{source_name}' must be added before adding an edge.","messagePattern":"Source node '(.+?)' must be added before adding an edge\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_graph/_graph_builder.py","lineNumber":139,"sourceCode":"\n        Args:\n            source: Source node (agent name or agent object)\n            target: Target node (agent name or agent object)\n            condition: Optional condition for edge activation.\n                If string, activates when substring is found in message.\n                If callable, activates when function returns True for the message.\n\n        Returns:\n            Self for method chaining\n\n        Raises:\n            ValueError: If source or target node doesn't exist in the builder\n        \"\"\"\n        source_name = self._get_name(source)\n        target_name = self._get_name(target)\n\n        if source_name not in self.nodes:\n            raise ValueError(f\"Source node '{source_name}' must be added before adding an edge.\")\n        if target_name not in self.nodes:\n            raise ValueError(f\"Target node '{target_name}' must be added before adding an edge.\")\n        if activation_group is None:\n            activation_group = target_name\n        if activation_condition is None:\n            activation_condition = \"all\"\n        self.nodes[source_name].edges.append(\n            DiGraphEdge(\n                target=target_name,\n                condition=condition,\n                activation_group=activation_group,\n                activation_condition=activation_condition,\n            )\n        )\n        return self\n\n    def add_conditional_edges(\n        self, source: Union[str, ChatAgent], condition_to_target: Dict[str, Union[str, ChatAgent]]","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_graph/_graph_builder.py#L121-L157","documentation":"Raised by DiGraphBuilder.add_edge when the source endpoint of the edge is not a node already known to the builder. Nodes come into existence only through add_edge (both endpoints), so referencing a source agent/object that was never used in a previous add_edge — or a name string that does not match any existing node — is rejected.","triggerScenarios":"Calling add_edge(new_agent, other) where new_agent was never an endpoint before but the roles are swapped in your head; passing source as a name string that is misspelled or refers to an agent added under a different name; building edges before wiring participants (e.g. empty participants list while using names).","commonSituations":"Confusion about which endpoint creates the node when both are new; name/object mismatches when the same agent is passed under aliases; refactoring edge order in generated graphs.","solutions":["Ensure the source node exists: it must have appeared as an endpoint of a prior add_edge, or be the same object/name you are passing now.","When passing names as strings, verify they exactly match the names of agents used elsewhere in the builder.","Construct edges in a deterministic order (entry -> ... -> exit) so sources are always established first."],"exampleFix":"// before\nbuilder.add_edge(\"reviwer\", drafter)  # typo: node 'reviwer' never created correctly downstream\n\n// after\nbuilder.add_edge(reviewer_agent, drafter)  # use the agent objects; both endpoints get created","handlingStrategy":"validation","validationCode":"def edge_source_exists(builder: DiGraphBuilder, source) -> bool:\n    name = source if isinstance(source, str) else source.name\n    return name in builder.nodes\n# assert before add_edge when passing name strings","typeGuard":"def is_known_node(builder: DiGraphBuilder, agent_or_name: ChatAgent | Team | str) -> bool:\n    name = agent_or_name if isinstance(agent_or_name, str) else agent_or_name.name\n    return name in builder.nodes","tryCatchPattern":"try:\n    builder.add_edge(source, target)\nexcept ValueError as e:\n    if \"Source node\" in str(e) and \"must be added\" in str(e):\n        # create the source node first (prior add_edge) or fix the name/object\n        ...","preventionTips":["Pass agent objects rather than name strings to add_edge to avoid name mismatches.","Build edges in workflow order so sources are established before being referenced.","Keep a single {name: agent} registry and reference agents through it."],"tags":["graph","builder","edges","configuration"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}