Replies: 2 comments 2 replies
-
Thanks for the request. The typical way to do this would be by interpreting certain updates as code rather than data. Example: from langgraph.graph import StateGraph
from langgraph.constants import START
from typing import Annotated, TypedDict
def extend_field(existing: list, update: list | str):
if update == "DELETE":
return []
return (existing or []) + update
class State(TypedDict):
my_field: Annotated[list, extend_field]
def my_node(state):
return {"my_field": ["This is appended"]}
def other_node(state):
return {"my_field": "DELETE"}
graph = (
StateGraph(State)
.add_node(my_node)
.add_node(other_node)
.add_edge(START, "my_node")
.add_edge("my_node", "other_node")
.compile()
)
for update in graph.stream({"my_field": ["First val"]}, stream_mode="values"):
print(update) Out: {'my_field': ['First val']}
{'my_field': ['First val', 'This is appended']}
{'my_field': []} If you want finer grain control you can further modify it to delete by ID. This is what add_messages does, for instance. |
Beta Was this translation helpful? Give feedback.
2 replies
-
same question. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Context: In LangGraph, the Annotated[list, operator.add] state field is very useful for accumulating results from parallel nodes. This is especially valuable in workflows where multiple nodes run in parallel, and their results need to be combined into a single list.
However, this design comes with a significant limitation: once an Annotated[list, operator.add] field starts accumulating data, there is no way to clear its contents during the workflow. This leads to the following problems:
The list grows indefinitely, which can cause memory issues or exceed LangSmith's state size limit.
Old data from previous nodes can interfere with subsequent steps in the graph.
Use Case: Imagine a workflow with three parallel nodes that aggregate their outputs into a single state["relation_verify_info"] field. After processing this information in a downstream node, it is desirable to clear the field to prevent it from growing indefinitely. However, with the current design, this is not possible.
Additional Context: This issue is particularly relevant for workflows with many parallel branches that feed into a single aggregation point. Without a way to clear the accumulated data, the workflow becomes harder to manage and scale.
Additional Context: This issue is particularly relevant for workflows with many parallel branches that feed into a single aggregation point. Without a way to clear the accumulated data, the workflow becomes harder to manage and scale.
Idea or request for content:
Proposed Solution: Introduce a mechanism to clear Annotated[list, operator.add] fields during the workflow. Some possible approaches:
Allow explicit clearing of the field at a specific node, e.g., state["relation_verify_info"] = [].
Add a new annotation, such as Annotated[list, operator.add_and_clearable], which enables both accumulation and clearing.
Provide a method like state.clear_add_field("relation_verify_info") to programmatically clear the contents.
Impact: This change would make LangGraph more flexible and efficient, especially for long-running workflows or workflows with high concurrency. It would also help prevent state size issues in LangSmith and improve overall usability.
Proposed Solution: Introduce a mechanism to clear Annotated[list, operator.add] fields during the workflow. Some possible approaches:
Allow explicit clearing of the field at a specific node, e.g., state["relation_verify_info"] = [].
Add a new annotation, such as Annotated[list, operator.add_and_clearable], which enables both accumulation and clearing.
Provide a method like state.clear_add_field("relation_verify_info") to programmatically clear the contents.
Impact: This change would make LangGraph more flexible and efficient, especially for long-running workflows or workflows with high concurrency. It would also help prevent state size issues in LangSmith and improve overall usability.
Beta Was this translation helpful? Give feedback.
All reactions