Skip to content

trim_first_node() and trim_last_node() removes nodes that should not be removed #28411

@Tasif1

Description

@Tasif1

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from pydantic import BaseModel
from langchain_core.runnables.graph import Graph

def test_trim() -> None:
  class Scheme(BaseModel):
    a: str
  
  graph = Graph()
  start = graph.add_node(Scheme, id="__start__")
  a = graph.add_node(Scheme, id="a")
  end = graph.add_node(Scheme, id="__end__")

  graph.add_edge(start, a)
  graph.add_edge(a, end)
  graph.add_edge(start, end)

  graph.trim_first_node() # should not remove __start__ since it has 2 outgoing edges
  print(graph.first_node().id) # should be __start__ but prints 'a'

  graph.trim_last_node() # should not remove the __end__ node since it has 2 incoming edges
  print(graph.last_node().id) # should be __end__ but prints 'a'

if __name__ == "__main__":
  test_trim()

Error Message and Stack Trace (if applicable)

No response

Description

The trim_first_node() and trim_last_node() function trims nodes that have more than one outgoing/incoming edge. This seems to go against the described behaviours of the functions in their docstrings:

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."""

def trim_last_node(self) -> None:
        """Remove the last node if it exists and has a single incoming edge,
        i.e., if removing it would not leave the graph without a "last" node."""

This issue was discovered while investigating issue #1676 from the langgraph repository

System Info

System Information

OS: Windows
OS Version: 10.0.19045
Python Version: 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]

Package Information

langchain_core: 0.3.10
langchain: 0.3.3
langsmith: 0.1.134
langchain_text_splitters: 0.3.0

Optional packages not installed

langgraph
langserve

Other Dependencies

aiohttp: 3.10.10
async-timeout: Installed. No version info available.
httpx: 0.27.2
jsonpatch: 1.33
numpy: 1.26.4
orjson: 3.10.7
packaging: 24.1
pydantic: 2.9.2
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
SQLAlchemy: 2.0.35
tenacity: 8.5.0
typing-extensions: 4.12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelated to a bug, vulnerability, unexpected error with an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions