trim_first_node() — langchain Function Reference
Architecture documentation for the trim_first_node() function in graph.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 600fa640_4465_baee_3ef3_0c1fda5bf94b["trim_first_node()"] 646f3bb5_a594_563e_cfbf_e18bdc9efb90["Graph"] 600fa640_4465_baee_3ef3_0c1fda5bf94b -->|defined in| 646f3bb5_a594_563e_cfbf_e18bdc9efb90 202074c8_de09_73dd_415c_edea3bae12f1["first_node()"] 600fa640_4465_baee_3ef3_0c1fda5bf94b -->|calls| 202074c8_de09_73dd_415c_edea3bae12f1 1eb728cf_7631_9204_a3a2_e3d486989562["remove_node()"] 600fa640_4465_baee_3ef3_0c1fda5bf94b -->|calls| 1eb728cf_7631_9204_a3a2_e3d486989562 c2fe47ba_c1ff_8826_55de_cd87be044850["_first_node()"] 600fa640_4465_baee_3ef3_0c1fda5bf94b -->|calls| c2fe47ba_c1ff_8826_55de_cd87be044850 style 600fa640_4465_baee_3ef3_0c1fda5bf94b fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/runnables/graph.py lines 481–492
def trim_first_node(self) -> None:
"""Remove the first node if it exists and has a single outgoing edge.
i.e., if removing it would not leave the graph without a "first" node.
"""
first_node = self.first_node()
if (
first_node
and _first_node(self, exclude=[first_node.id])
and len({e for e in self.edges if e.source == first_node.id}) == 1
):
self.remove_node(first_node)
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does trim_first_node() do?
trim_first_node() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/graph.py.
Where is trim_first_node() defined?
trim_first_node() is defined in libs/core/langchain_core/runnables/graph.py at line 481.
What does trim_first_node() call?
trim_first_node() calls 3 function(s): _first_node, first_node, remove_node.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free