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_v2.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  ad3667ea_37f5_1191_d9e1_6ff2602f1259["test_runnable_with_message_history()"]
  33c02978_2077_5819_7048_bc2a81e80625["test_runnable_events_v2.py"]
  ad3667ea_37f5_1191_d9e1_6ff2602f1259 -->|defined in| 33c02978_2077_5819_7048_bc2a81e80625
  a35a2b06_d230_78c7_e382_a3e274acf675["add_message()"]
  ad3667ea_37f5_1191_d9e1_6ff2602f1259 -->|calls| a35a2b06_d230_78c7_e382_a3e274acf675
  c5bb3014_45c4_cc75_d40b_0453d8678a92["clear()"]
  ad3667ea_37f5_1191_d9e1_6ff2602f1259 -->|calls| c5bb3014_45c4_cc75_d40b_0453d8678a92
  style ad3667ea_37f5_1191_d9e1_6ff2602f1259 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py lines 1905–2000

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",
    )

    # patch with_message_history._get_output_messages to listen for errors
    # so we can raise them in this main thread
    raised_errors = []

    def collect_errors(fn: Callable[..., Any]) -> Callable[..., Any]:
        nonlocal raised_errors

        def _get_output_messages(*args: Any, **kwargs: Any) -> Any:
            try:
                return fn(*args, **kwargs)
            except Exception as e:
                raised_errors.append(e)
                raise

        return _get_output_messages

    old_ref = with_message_history._get_output_messages
    with_message_history.__dict__["_get_output_messages"] = collect_errors(old_ref)
    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"),
        ]
    }

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_v2.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_v2.py at line 1905.
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