Home / Function/ _get_last_messages() — langchain Function Reference

_get_last_messages() — langchain Function Reference

Architecture documentation for the _get_last_messages() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  213359c4_25e5_9966_edeb_c4951e45c529["_get_last_messages()"]
  2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"]
  213359c4_25e5_9966_edeb_c4951e45c529 -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda
  36b15b48_0822_029c_4a53_8243405e5a5e["_get_request_payload()"]
  36b15b48_0822_029c_4a53_8243405e5a5e -->|calls| 213359c4_25e5_9966_edeb_c4951e45c529
  style 213359c4_25e5_9966_edeb_c4951e45c529 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/chat_models/base.py lines 3829–3851

def _get_last_messages(
    messages: Sequence[BaseMessage],
) -> tuple[Sequence[BaseMessage], str | None]:
    """Get the last part of the conversation after the last `AIMessage` with an `id`.

    Will return:

    1. Every message after the most-recent `AIMessage` that has a non-empty
        `response_metadata["id"]` (may be an empty list),
    2. That `id`.

    If the most-recent `AIMessage` does not have an `id` (or there is no
    `AIMessage` at all) the entire conversation is returned together with `None`.
    """
    for i in range(len(messages) - 1, -1, -1):
        msg = messages[i]
        if isinstance(msg, AIMessage):
            response_id = msg.response_metadata.get("id")
            if response_id and response_id.startswith("resp_"):
                return messages[i + 1 :], response_id
            # Continue searching for an AIMessage with a valid response_id

    return messages, None

Domain

Subdomains

Frequently Asked Questions

What does _get_last_messages() do?
_get_last_messages() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is _get_last_messages() defined?
_get_last_messages() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 3829.
What calls _get_last_messages()?
_get_last_messages() is called by 1 function(s): _get_request_payload.

Analyze Your Own Codebase

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

Try Supermodel Free