Skip to content

Commit

Permalink
core: add RemoveMessage (#23636)
Browse files Browse the repository at this point in the history
This change adds a new message type `RemoveMessage`. This will enable
`langgraph` users to manually modify graph state (or have the graph
nodes modify the state) to remove messages by `id`

Examples:

* allow users to delete messages from state by calling

```python
graph.update_state(config, values=[RemoveMessage(id=state.values[-1].id)])
```

* allow nodes to delete messages

```python
graph.add_node("delete_messages", lambda state: [RemoveMessage(id=state[-1].id)])
```
  • Loading branch information
vbarda authored Jun 28, 2024
1 parent 8fce8c6 commit e8d7700
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libs/community/tests/unit_tests/load/test_serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def test_serializable_mapping() -> None:
"structured",
"StructuredPrompt",
),
# This is not exported from langchain, only langchain_core
("langchain", "schema", "messages", "RemoveMessage"): (
"langchain_core",
"messages",
"modifier",
"RemoveMessage",
),
}
serializable_modules = import_all_modules("langchain")

Expand Down
6 changes: 6 additions & 0 deletions libs/core/langchain_core/load/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
"tool",
"ToolMessage",
),
("langchain", "schema", "messages", "RemoveMessage"): (
"langchain_core",
"messages",
"modifier",
"RemoveMessage",
),
("langchain", "schema", "agent", "AgentAction"): (
"langchain_core",
"agents",
Expand Down
2 changes: 2 additions & 0 deletions libs/core/langchain_core/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from langchain_core.messages.chat import ChatMessage, ChatMessageChunk
from langchain_core.messages.function import FunctionMessage, FunctionMessageChunk
from langchain_core.messages.human import HumanMessage, HumanMessageChunk
from langchain_core.messages.modifier import RemoveMessage
from langchain_core.messages.system import SystemMessage, SystemMessageChunk
from langchain_core.messages.tool import (
InvalidToolCall,
Expand Down Expand Up @@ -70,6 +71,7 @@
"ToolCallChunk",
"ToolMessage",
"ToolMessageChunk",
"RemoveMessage",
"_message_from_dict",
"convert_to_messages",
"get_buffer_string",
Expand Down
23 changes: 23 additions & 0 deletions libs/core/langchain_core/messages/modifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Any, List, Literal

from langchain_core.messages.base import BaseMessage


class RemoveMessage(BaseMessage):
"""Message responsible for deleting other messages."""

type: Literal["remove"] = "remove"

def __init__(self, id: str, **kwargs: Any) -> None:
if kwargs.pop("content", None):
raise ValueError("RemoveMessage does not support 'content' field.")

return super().__init__("", id=id, **kwargs)

@classmethod
def get_lc_namespace(cls) -> List[str]:
"""Get the namespace of the langchain object."""
return ["langchain", "schema", "messages"]


RemoveMessage.update_forward_refs()
1 change: 1 addition & 0 deletions libs/core/tests/unit_tests/messages/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ToolCallChunk",
"ToolMessage",
"ToolMessageChunk",
"RemoveMessage",
"convert_to_messages",
"get_buffer_string",
"merge_content",
Expand Down

0 comments on commit e8d7700

Please sign in to comment.