Home / Function/ test_runnable_with_message_history() — langchain Function Reference

test_runnable_with_message_history() — langchain Function Reference

Architecture documentation for the test_runnable_with_message_history() function in test_runnable_events_v1.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  9a3699e5_6c2b_e065_33ab_67e600b59a43["test_runnable_with_message_history()"]
  8ff41f3c_f250_f8de_8094_4f24860a10e0["test_runnable_events_v1.py"]
  9a3699e5_6c2b_e065_33ab_67e600b59a43 -->|defined in| 8ff41f3c_f250_f8de_8094_4f24860a10e0
  e513bf9b_8331_0da8_3ae8_4496cbe89bb8["add_message()"]
  9a3699e5_6c2b_e065_33ab_67e600b59a43 -->|calls| e513bf9b_8331_0da8_3ae8_4496cbe89bb8
  59618b17_1870_c6b7_c31d_2f0d1c32f83a["clear()"]
  9a3699e5_6c2b_e065_33ab_67e600b59a43 -->|calls| 59618b17_1870_c6b7_c31d_2f0d1c32f83a
  style 9a3699e5_6c2b_e065_33ab_67e600b59a43 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/runnables/test_runnable_events_v1.py lines 1957–2032

async def test_runnable_with_message_history() -> None:
    class InMemoryHistory(BaseChatMessageHistory, BaseModel):
        """In memory implementation of chat message history."""

        # Attention: for the tests use an Any type to work-around a pydantic issue
        # where it re-instantiates a list, so mutating the list doesn't end up mutating
        # the content in the store!

        # Using Any type here rather than list[BaseMessage] due to pydantic issue!
        messages: Any

        def add_message(self, message: BaseMessage) -> None:
            """Add a self-created message to the store."""
            self.messages.append(message)

        def clear(self) -> None:
            self.messages = []

    # Here we use a global variable to store the chat message history.
    # This will make it easier to inspect it to see the underlying results.
    store: dict[str, list[BaseMessage]] = {}

    def get_by_session_id(session_id: str) -> BaseChatMessageHistory:
        """Get a chat message history."""
        if session_id not in store:
            store[session_id] = []
        return InMemoryHistory(messages=store[session_id])

    infinite_cycle = cycle(
        [
            AIMessage(content="hello", id="ai3"),
            AIMessage(content="world", id="ai4"),
        ]
    )

    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", "You are a cat"),
            MessagesPlaceholder(variable_name="history"),
            ("human", "{question}"),
        ]
    )
    model = GenericFakeChatModel(messages=infinite_cycle)

    chain = prompt | model
    with_message_history = RunnableWithMessageHistory(
        chain,
        get_session_history=get_by_session_id,
        input_messages_key="question",
        history_messages_key="history",
    )
    await with_message_history.with_config(
        {"configurable": {"session_id": "session-123"}}
    ).ainvoke({"question": "hello"})

    assert store == {
        "session-123": [
            HumanMessage(content="hello"),
            AIMessage(content="hello", id="ai3"),
        ]
    }

    await asyncio.to_thread(
        with_message_history.with_config(
            {"configurable": {"session_id": "session-123"}}
        ).invoke,
        {"question": "meow"},
    )
    assert store == {
        "session-123": [
            HumanMessage(content="hello"),
            AIMessage(content="hello", id="ai3"),
            HumanMessage(content="meow"),
            AIMessage(content="world", id="ai4"),
        ]
    }

Domain

Subdomains

Frequently Asked Questions

What does test_runnable_with_message_history() do?
test_runnable_with_message_history() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/runnables/test_runnable_events_v1.py.
Where is test_runnable_with_message_history() defined?
test_runnable_with_message_history() is defined in libs/core/tests/unit_tests/runnables/test_runnable_events_v1.py at line 1957.
What does test_runnable_with_message_history() call?
test_runnable_with_message_history() calls 2 function(s): add_message, clear.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free