Home / Function/ _convert_message_to_mistral_chat_message() — langchain Function Reference

_convert_message_to_mistral_chat_message() — langchain Function Reference

Architecture documentation for the _convert_message_to_mistral_chat_message() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  0040f7d2_abde_821f_7713_ae3da549425c["_convert_message_to_mistral_chat_message()"]
  cfb937d2_ce7f_5338_2b62_6452043ac78a["chat_models.py"]
  0040f7d2_abde_821f_7713_ae3da549425c -->|defined in| cfb937d2_ce7f_5338_2b62_6452043ac78a
  ef431007_0264_4d4c_0b4c_b5b637d2ca3e["_create_message_dicts()"]
  ef431007_0264_4d4c_0b4c_b5b637d2ca3e -->|calls| 0040f7d2_abde_821f_7713_ae3da549425c
  8a6a7033_9f65_cb08_2b61_2a09610d6827["_format_tool_call_for_mistral()"]
  0040f7d2_abde_821f_7713_ae3da549425c -->|calls| 8a6a7033_9f65_cb08_2b61_2a09610d6827
  34daee73_1fe3_5f5c_3bef_81e1aa38b91f["_format_invalid_tool_call_for_mistral()"]
  0040f7d2_abde_821f_7713_ae3da549425c -->|calls| 34daee73_1fe3_5f5c_3bef_81e1aa38b91f
  30456995_7e1c_8201_cd0a_0ab4e2ee2d00["_clean_block()"]
  0040f7d2_abde_821f_7713_ae3da549425c -->|calls| 30456995_7e1c_8201_cd0a_0ab4e2ee2d00
  17458072_b7f3_6bde_1cc2_60c8f77a0cbc["_convert_tool_call_id_to_mistral_compatible()"]
  0040f7d2_abde_821f_7713_ae3da549425c -->|calls| 17458072_b7f3_6bde_1cc2_60c8f77a0cbc
  style 0040f7d2_abde_821f_7713_ae3da549425c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/mistralai/langchain_mistralai/chat_models.py lines 372–457

def _convert_message_to_mistral_chat_message(
    message: BaseMessage,
) -> dict:
    if isinstance(message, ChatMessage):
        return {"role": message.role, "content": message.content}
    if isinstance(message, HumanMessage):
        return {"role": "user", "content": message.content}
    if isinstance(message, AIMessage):
        message_dict: dict[str, Any] = {"role": "assistant"}
        tool_calls: list = []
        if message.tool_calls or message.invalid_tool_calls:
            if message.tool_calls:
                tool_calls.extend(
                    _format_tool_call_for_mistral(tool_call)
                    for tool_call in message.tool_calls
                )
            if message.invalid_tool_calls:
                tool_calls.extend(
                    _format_invalid_tool_call_for_mistral(invalid_tool_call)
                    for invalid_tool_call in message.invalid_tool_calls
                )
        elif "tool_calls" in message.additional_kwargs:
            for tc in message.additional_kwargs["tool_calls"]:
                chunk = {
                    "function": {
                        "name": tc["function"]["name"],
                        "arguments": tc["function"]["arguments"],
                    }
                }
                if _id := tc.get("id"):
                    chunk["id"] = _id
                tool_calls.append(chunk)
        else:
            pass
        if tool_calls:  # do not populate empty list tool_calls
            message_dict["tool_calls"] = tool_calls

        # Message content
        # Translate v1 content
        if message.response_metadata.get("output_version") == "v1":
            content = _convert_from_v1_to_mistral(
                message.content_blocks, message.response_metadata.get("model_provider")
            )
        else:
            content = message.content

        if tool_calls and content:
            # Assistant message must have either content or tool_calls, but not both.
            # Some providers may not support tool_calls in the same message as content.
            # This is done to ensure compatibility with messages from other providers.
            content = ""

        elif isinstance(content, list):
            content = [
                _clean_block(block)
                if isinstance(block, dict) and "index" in block
                else block
                for block in content
            ]
        else:
            content = message.content

        # if any blocks are dicts, cast strings to text blocks
        if any(isinstance(block, dict) for block in content):
            content = [
                block if isinstance(block, dict) else {"type": "text", "text": block}
                for block in content
            ]
        message_dict["content"] = content

        if "prefix" in message.additional_kwargs:
            message_dict["prefix"] = message.additional_kwargs["prefix"]
        return message_dict
    if isinstance(message, SystemMessage):
        return {"role": "system", "content": message.content}
    if isinstance(message, ToolMessage):
        return {
            "role": "tool",
            "content": message.content,
            "name": message.name,
            "tool_call_id": _convert_tool_call_id_to_mistral_compatible(

Domain

Subdomains

Frequently Asked Questions

What does _convert_message_to_mistral_chat_message() do?
_convert_message_to_mistral_chat_message() is a function in the langchain codebase, defined in libs/partners/mistralai/langchain_mistralai/chat_models.py.
Where is _convert_message_to_mistral_chat_message() defined?
_convert_message_to_mistral_chat_message() is defined in libs/partners/mistralai/langchain_mistralai/chat_models.py at line 372.
What does _convert_message_to_mistral_chat_message() call?
_convert_message_to_mistral_chat_message() calls 4 function(s): _clean_block, _convert_tool_call_id_to_mistral_compatible, _format_invalid_tool_call_for_mistral, _format_tool_call_for_mistral.
What calls _convert_message_to_mistral_chat_message()?
_convert_message_to_mistral_chat_message() is called by 1 function(s): _create_message_dicts.

Analyze Your Own Codebase

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

Try Supermodel Free