core[patch]: Fixed trim functions, and added corresponding unit test for the solved issue (#28429)

- **Description:** 
- Trim functions were incorrectly deleting nodes with more than 1
outgoing/incoming edge, so an extra condition was added to check for
this directly. A unit test "test_trim_multi_edge" was written to test
this test case specifically.
- **Issue:** 
  - Fixes #28411 
  - Fixes https://github.com/langchain-ai/langgraph/issues/1676
- **Dependencies:** 
  - No changes were made to the dependencies

- [x] Unit tests were added to verify the changes.
- [x] Updated documentation where necessary.
- [x] Ran make format, make lint, and make test to ensure compliance
with project standards.

---------

Co-authored-by: Tasif Hussain <tasif006@gmail.com>
This commit is contained in:
Fahim Zaman
2024-12-08 23:45:28 -05:00
committed by GitHub
parent 54fba7e520
commit 481c4bfaba
2 changed files with 30 additions and 2 deletions

View File

@@ -69,6 +69,26 @@ def test_trim(snapshot: SnapshotAssertion) -> None:
assert graph.last_node() is end
def test_trim_multi_edge() -> None:
class Scheme(BaseModel):
a: str
graph = Graph()
start = graph.add_node(Scheme, id="__start__")
a = graph.add_node(Scheme, id="a")
last = graph.add_node(Scheme, id="__end__")
graph.add_edge(start, a)
graph.add_edge(a, last)
graph.add_edge(start, last)
graph.trim_first_node() # should not remove __start__ since it has 2 outgoing edges
assert graph.first_node() is start
graph.trim_last_node() # should not remove the __end__ node since it has 2 incoming edges
assert graph.last_node() is last
def test_graph_sequence(snapshot: SnapshotAssertion) -> None:
fake_llm = FakeListLLM(responses=["a"])
prompt = PromptTemplate.from_template("Hello, {name}!")