libs/core/tests/unit_tests/test_messages.py PYTHON 1,473 lines View on github.com → Search inside
1import uuid2from typing import Any, get_args34import pytest56from langchain_core._api import LangChainDeprecationWarning7from langchain_core.documents import Document8from langchain_core.load import dumpd, load9from langchain_core.messages import (10    AIMessage,11    AIMessageChunk,12    BaseMessage,13    BaseMessageChunk,14    ChatMessage,15    ChatMessageChunk,16    FunctionMessage,17    FunctionMessageChunk,18    HumanMessage,19    HumanMessageChunk,20    RemoveMessage,21    SystemMessage,22    ToolMessage,23    convert_to_messages,24    convert_to_openai_image_block,25    get_buffer_string,26    is_data_content_block,27    merge_content,28    message_chunk_to_message,29    message_to_dict,30    messages_from_dict,31    messages_to_dict,32)33from langchain_core.messages.content import KNOWN_BLOCK_TYPES, ContentBlock34from langchain_core.messages.tool import invalid_tool_call as create_invalid_tool_call35from langchain_core.messages.tool import tool_call as create_tool_call36from langchain_core.messages.tool import tool_call_chunk as create_tool_call_chunk37from langchain_core.utils._merge import merge_lists383940def test_message_init() -> None:41    for doc in [42        BaseMessage(type="foo", content="bar"),43        BaseMessage(type="foo", content="bar", id=None),44        BaseMessage(type="foo", content="bar", id="1"),45        BaseMessage(type="foo", content="bar", id=1),46    ]:47        assert isinstance(doc, BaseMessage)484950def test_message_chunks() -> None:51    assert AIMessageChunk(content="I am", id="ai3") + AIMessageChunk(52        content=" indeed."53    ) == AIMessageChunk(content="I am indeed.", id="ai3"), (54        "MessageChunk + MessageChunk should be a MessageChunk"55    )5657    assert AIMessageChunk(content="I am", id="ai2") + HumanMessageChunk(58        content=" indeed.", id="human1"59    ) == AIMessageChunk(content="I am indeed.", id="ai2"), (60        "MessageChunk + MessageChunk should be a MessageChunk "61        "of same class as the left side"62    )6364    assert AIMessageChunk(65        content="", additional_kwargs={"foo": "bar"}66    ) + AIMessageChunk(content="", additional_kwargs={"baz": "foo"}) == AIMessageChunk(67        content="", additional_kwargs={"foo": "bar", "baz": "foo"}68    ), (69        "MessageChunk + MessageChunk should be a MessageChunk "70        "with merged additional_kwargs"71    )7273    assert AIMessageChunk(74        content="", additional_kwargs={"function_call": {"name": "web_search"}}75    ) + AIMessageChunk(76        content="", additional_kwargs={"function_call": {"arguments": None}}77    ) + AIMessageChunk(78        content="", additional_kwargs={"function_call": {"arguments": "{\n"}}79    ) + AIMessageChunk(80        content="",81        additional_kwargs={"function_call": {"arguments": '  "query": "turtles"\n}'}},82    ) == AIMessageChunk(83        content="",84        additional_kwargs={85            "function_call": {86                "name": "web_search",87                "arguments": '{\n  "query": "turtles"\n}',88            }89        },90    ), (91        "MessageChunk + MessageChunk should be a MessageChunk "92        "with merged additional_kwargs"93    )9495    # Test tool calls96    assert (97        AIMessageChunk(98            content="",99            tool_call_chunks=[100                create_tool_call_chunk(name="tool1", args="", id="1", index=0)101            ],102        )103        + AIMessageChunk(104            content="",105            tool_call_chunks=[106                create_tool_call_chunk(107                    name=None, args='{"arg1": "val', id=None, index=0108                )109            ],110        )111        + AIMessageChunk(112            content="",113            tool_call_chunks=[114                create_tool_call_chunk(name=None, args='ue}"', id=None, index=0)115            ],116        )117    ) == AIMessageChunk(118        content="",119        tool_call_chunks=[120            create_tool_call_chunk(121                name="tool1", args='{"arg1": "value}"', id="1", index=0122            )123        ],124    )125126    assert (127        AIMessageChunk(128            content="",129            tool_call_chunks=[130                create_tool_call_chunk(name="tool1", args="", id="1", index=0)131            ],132        )133        + AIMessageChunk(134            content="",135            tool_call_chunks=[136                create_tool_call_chunk(name=None, args='{"arg1": "val', id="", index=0)137            ],138        )139        + AIMessageChunk(140            content="",141            tool_call_chunks=[142                create_tool_call_chunk(name=None, args='ue"}', id="", index=0)143            ],144        )145    ) == AIMessageChunk(146        content="",147        tool_call_chunks=[148            create_tool_call_chunk(149                name="tool1", args='{"arg1": "value"}', id="1", index=0150            )151        ],152    )153154    assert (155        AIMessageChunk(156            content="",157            tool_call_chunks=[158                create_tool_call_chunk(name="tool1", args="", id="1", index=0)159            ],160        )161        + AIMessageChunk(162            content="",163            tool_call_chunks=[164                create_tool_call_chunk(name="tool1", args="a", id=None, index=1)165            ],166        )167        # Don't merge if `index` field does not match.168    ) == AIMessageChunk(169        content="",170        tool_call_chunks=[171            create_tool_call_chunk(name="tool1", args="", id="1", index=0),172            create_tool_call_chunk(name="tool1", args="a", id=None, index=1),173        ],174    )175176    ai_msg_chunk = AIMessageChunk(content="")177    tool_calls_msg_chunk = AIMessageChunk(178        content="",179        tool_call_chunks=[180            create_tool_call_chunk(name="tool1", args="a", id=None, index=1)181        ],182    )183    assert ai_msg_chunk + tool_calls_msg_chunk == tool_calls_msg_chunk184    assert tool_calls_msg_chunk + ai_msg_chunk == tool_calls_msg_chunk185186    ai_msg_chunk = AIMessageChunk(187        content="",188        tool_call_chunks=[189            create_tool_call_chunk(name="tool1", args="", id="1", index=0)190        ],191    )192    assert ai_msg_chunk.tool_calls == [create_tool_call(name="tool1", args={}, id="1")]193194    # Test token usage195    left = AIMessageChunk(196        content="",197        usage_metadata={"input_tokens": 1, "output_tokens": 2, "total_tokens": 3},198    )199    right = AIMessageChunk(200        content="",201        usage_metadata={"input_tokens": 4, "output_tokens": 5, "total_tokens": 9},202    )203    assert left + right == AIMessageChunk(204        content="",205        usage_metadata={"input_tokens": 5, "output_tokens": 7, "total_tokens": 12},206    )207    assert AIMessageChunk(content="") + left == left208    assert right + AIMessageChunk(content="") == right209210    default_id = "lc_run--abc123"211    meaningful_id = "msg_def456"212213    # Test ID order of precedence214    null_id_chunk = AIMessageChunk(content="", id=None)215    default_id_chunk = AIMessageChunk(216        content="", id=default_id217    )  # LangChain-assigned run ID218    provider_chunk = AIMessageChunk(219        content="", id=meaningful_id220    )  # provided ID (either by user or provider)221222    assert (null_id_chunk + default_id_chunk).id == default_id223    assert (null_id_chunk + provider_chunk).id == meaningful_id224225    # Provider assigned IDs have highest precedence226    assert (default_id_chunk + provider_chunk).id == meaningful_id227228229def test_message_chunks_bool_additional_kwargs_raises() -> None:230    """Differing booleans (e.g. `refusal`) must not silently coerce to `int`."""231    a = AIMessageChunk(content="", additional_kwargs={"refusal": True})232    b = AIMessageChunk(content="", additional_kwargs={"refusal": False})233    with pytest.raises(TypeError, match="unsupported type"):234        a + b235236237def test_chat_message_chunks() -> None:238    assert ChatMessageChunk(role="User", content="I am", id="ai4") + ChatMessageChunk(239        role="User", content=" indeed."240    ) == ChatMessageChunk(id="ai4", role="User", content="I am indeed."), (241        "ChatMessageChunk + ChatMessageChunk should be a ChatMessageChunk"242    )243244    with pytest.raises(245        ValueError, match="Cannot concatenate ChatMessageChunks with different roles"246    ):247        ChatMessageChunk(role="User", content="I am") + ChatMessageChunk(248            role="Assistant", content=" indeed."249        )250251    assert ChatMessageChunk(role="User", content="I am") + AIMessageChunk(252        content=" indeed."253    ) == ChatMessageChunk(role="User", content="I am indeed."), (254        "ChatMessageChunk + other MessageChunk should be a ChatMessageChunk "255        "with the left side's role"256    )257258    assert AIMessageChunk(content="I am") + ChatMessageChunk(259        role="User", content=" indeed."260    ) == AIMessageChunk(content="I am indeed."), (261        "Other MessageChunk + ChatMessageChunk should be a MessageChunk "262        "as the left side"263    )264265266def test_complex_ai_message_chunks() -> None:267    assert AIMessageChunk(content=["I am"], id="ai4") + AIMessageChunk(268        content=[" indeed."]269    ) == AIMessageChunk(id="ai4", content=["I am", " indeed."]), (270        "Content concatenation with arrays of strings should naively combine"271    )272273    assert AIMessageChunk(content=[{"index": 0, "text": "I am"}]) + AIMessageChunk(274        content=" indeed."275    ) == AIMessageChunk(content=[{"index": 0, "text": "I am"}, " indeed."]), (276        "Concatenating mixed content arrays should naively combine them"277    )278279    assert AIMessageChunk(content=[{"index": 0, "text": "I am"}]) + AIMessageChunk(280        content=[{"index": 0, "text": " indeed."}]281    ) == AIMessageChunk(content=[{"index": 0, "text": "I am indeed."}]), (282        "Concatenating when both content arrays are dicts with the same index "283        "should merge"284    )285286    assert AIMessageChunk(content=[{"index": 0, "text": "I am"}]) + AIMessageChunk(287        content=[{"text": " indeed."}]288    ) == AIMessageChunk(content=[{"index": 0, "text": "I am"}, {"text": " indeed."}]), (289        "Concatenating when one chunk is missing an index should not merge or throw"290    )291292    assert AIMessageChunk(content=[{"index": 0, "text": "I am"}]) + AIMessageChunk(293        content=[{"index": 2, "text": " indeed."}]294    ) == AIMessageChunk(295        content=[{"index": 0, "text": "I am"}, {"index": 2, "text": " indeed."}]296    ), (297        "Concatenating when both content arrays are dicts with a gap between indexes "298        "should not result in a holey array"299    )300301    assert AIMessageChunk(content=[{"index": 0, "text": "I am"}]) + AIMessageChunk(302        content=[{"index": 1, "text": " indeed."}]303    ) == AIMessageChunk(304        content=[{"index": 0, "text": "I am"}, {"index": 1, "text": " indeed."}]305    ), (306        "Concatenating when both content arrays are dicts with separate indexes "307        "should not merge"308    )309310    assert AIMessageChunk(311        content=[{"index": 0, "text": "I am", "type": "text_block"}]312    ) + AIMessageChunk(313        content=[{"index": 0, "text": " indeed.", "type": "text_block"}]314    ) == AIMessageChunk(315        content=[{"index": 0, "text": "I am indeed.", "type": "text_block"}]316    ), (317        "Concatenating when both content arrays are dicts with the same index and type "318        "should merge"319    )320321    assert AIMessageChunk(322        content=[{"index": 0, "text": "I am", "type": "text_block"}]323    ) + AIMessageChunk(324        content=[{"index": 0, "text": " indeed.", "type": "text_block_delta"}]325    ) == AIMessageChunk(326        content=[{"index": 0, "text": "I am indeed.", "type": "text_block"}]327    ), (328        "Concatenating when both content arrays are dicts with the same index "329        "and different types should merge without updating type"330    )331332    assert AIMessageChunk(333        content=[{"index": 0, "text": "I am", "type": "text_block"}]334    ) + AIMessageChunk(335        content="", response_metadata={"extra": "value"}336    ) == AIMessageChunk(337        content=[{"index": 0, "text": "I am", "type": "text_block"}],338        response_metadata={"extra": "value"},339    ), (340        "Concatenating when one content is an array and one is an empty string "341        "should not add a new item, but should concat other fields"342    )343344345def test_function_message_chunks() -> None:346    assert FunctionMessageChunk(347        name="hello", content="I am", id="ai5"348    ) + FunctionMessageChunk(name="hello", content=" indeed.") == FunctionMessageChunk(349        id="ai5", name="hello", content="I am indeed."350    ), "FunctionMessageChunk + FunctionMessageChunk should be a FunctionMessageChunk"351352    with pytest.raises(353        ValueError,354        match="Cannot concatenate FunctionMessageChunks with different names",355    ):356        FunctionMessageChunk(name="hello", content="I am") + FunctionMessageChunk(357            name="bye", content=" indeed."358        )359360361def test_ai_message_chunks() -> None:362    assert AIMessageChunk(content="I am") + AIMessageChunk(363        content=" indeed."364    ) == AIMessageChunk(content="I am indeed."), (365        "AIMessageChunk + AIMessageChunk should be a AIMessageChunk"366    )367368369class TestGetBufferString:370    _HUMAN_MSG = HumanMessage(content="human")371    _AI_MSG = AIMessage(content="ai")372373    def test_empty_input(self) -> None:374        assert not get_buffer_string([])375376    def test_valid_single_message(self) -> None:377        expected_output = "Human: human"378        assert get_buffer_string([self._HUMAN_MSG]) == expected_output379380    def test_custom_human_prefix(self) -> None:381        expected_output = "H: human"382        assert get_buffer_string([self._HUMAN_MSG], human_prefix="H") == expected_output383384    def test_custom_ai_prefix(self) -> None:385        expected_output = "A: ai"386        assert get_buffer_string([self._AI_MSG], ai_prefix="A") == expected_output387388    def test_multiple_msg(self) -> None:389        msgs = [390            self._HUMAN_MSG,391            self._AI_MSG,392            SystemMessage(content="system"),393            FunctionMessage(name="func", content="function"),394            ToolMessage(tool_call_id="tool_id", content="tool"),395            ChatMessage(role="Chat", content="chat"),396            AIMessage(content="tool"),397        ]398        expected_output = (399            "Human: human\n"400            "AI: ai\n"401            "System: system\n"402            "Function: function\n"403            "Tool: tool\n"404            "Chat: chat\n"405            "AI: tool"406        )407408        assert get_buffer_string(msgs) == expected_output409410    def test_custom_message_separator(self) -> None:411        msgs = [412            self._HUMAN_MSG,413            self._AI_MSG,414        ]415        expected_output = "Human: human\n\nAI: ai"416        assert get_buffer_string(msgs, message_separator="\n\n") == expected_output417418419def test_multiple_msg() -> None:420    human_msg = HumanMessage(content="human", additional_kwargs={"key": "value"})421    ai_msg = AIMessage(content="ai")422    sys_msg = SystemMessage(content="sys")423424    msgs = [425        human_msg,426        ai_msg,427        sys_msg,428    ]429    assert messages_from_dict(messages_to_dict(msgs)) == msgs430431    # Test with tool calls432    msgs = [433        AIMessage(434            content="",435            tool_calls=[create_tool_call(name="a", args={"b": 1}, id=None)],436        ),437        AIMessage(438            content="",439            tool_calls=[create_tool_call(name="c", args={"c": 2}, id=None)],440        ),441    ]442    assert messages_from_dict(messages_to_dict(msgs)) == msgs443444445def test_multiple_msg_with_name() -> None:446    human_msg = HumanMessage(447        content="human", additional_kwargs={"key": "value"}, name="human erick"448    )449    ai_msg = AIMessage(content="ai", name="ai erick")450    sys_msg = SystemMessage(content="sys", name="sys erick")451452    msgs = [453        human_msg,454        ai_msg,455        sys_msg,456    ]457    assert messages_from_dict(messages_to_dict(msgs)) == msgs458459460def test_message_chunk_to_message() -> None:461    assert message_chunk_to_message(462        AIMessageChunk(content="I am", additional_kwargs={"foo": "bar"})463    ) == AIMessage(content="I am", additional_kwargs={"foo": "bar"})464    assert message_chunk_to_message(HumanMessageChunk(content="I am")) == HumanMessage(465        content="I am"466    )467    assert message_chunk_to_message(468        ChatMessageChunk(role="User", content="I am")469    ) == ChatMessage(role="User", content="I am")470    assert message_chunk_to_message(471        FunctionMessageChunk(name="hello", content="I am")472    ) == FunctionMessage(name="hello", content="I am")473474    chunk = AIMessageChunk(475        content="I am",476        tool_call_chunks=[477            create_tool_call_chunk(name="tool1", args='{"a": 1}', id="1", index=0),478            create_tool_call_chunk(name="tool2", args='{"b": ', id="2", index=0),479            create_tool_call_chunk(name="tool3", args=None, id="3", index=0),480            create_tool_call_chunk(name="tool4", args="abc", id="4", index=0),481        ],482    )483    expected = AIMessage(484        content="I am",485        tool_calls=[486            create_tool_call(name="tool1", args={"a": 1}, id="1"),487            create_tool_call(name="tool2", args={}, id="2"),488            create_tool_call(name="tool3", args={}, id="3"),489        ],490        invalid_tool_calls=[491            create_invalid_tool_call(name="tool4", args="abc", id="4", error=None),492        ],493    )494    assert message_chunk_to_message(chunk) == expected495    assert AIMessage(**expected.model_dump()) == expected496    assert AIMessageChunk(**chunk.model_dump()) == chunk497498499def test_tool_calls_merge() -> None:500    chunks: list[dict[str, Any]] = [501        {"content": ""},502        {503            "content": "",504            "additional_kwargs": {505                "tool_calls": [506                    {507                        "index": 0,508                        "id": "call_CwGAsESnXehQEjiAIWzinlva",509                        "function": {"arguments": "", "name": "person"},510                        "type": "function",511                    }512                ]513            },514        },515        {516            "content": "",517            "additional_kwargs": {518                "tool_calls": [519                    {520                        "index": 0,521                        "id": None,522                        "function": {"arguments": '{"na', "name": None},523                        "type": None,524                    }525                ]526            },527        },528        {529            "content": "",530            "additional_kwargs": {531                "tool_calls": [532                    {533                        "index": 0,534                        "id": None,535                        "function": {"arguments": 'me": ', "name": None},536                        "type": None,537                    }538                ]539            },540        },541        {542            "content": "",543            "additional_kwargs": {544                "tool_calls": [545                    {546                        "index": 0,547                        "id": None,548                        "function": {"arguments": '"jane"', "name": None},549                        "type": None,550                    }551                ]552            },553        },554        {555            "content": "",556            "additional_kwargs": {557                "tool_calls": [558                    {559                        "index": 0,560                        "id": None,561                        "function": {"arguments": ', "a', "name": None},562                        "type": None,563                    }564                ]565            },566        },567        {568            "content": "",569            "additional_kwargs": {570                "tool_calls": [571                    {572                        "index": 0,573                        "id": None,574                        "function": {"arguments": 'ge": ', "name": None},575                        "type": None,576                    }577                ]578            },579        },580        {581            "content": "",582            "additional_kwargs": {583                "tool_calls": [584                    {585                        "index": 0,586                        "id": None,587                        "function": {"arguments": "2}", "name": None},588                        "type": None,589                    }590                ]591            },592        },593        {594            "content": "",595            "additional_kwargs": {596                "tool_calls": [597                    {598                        "index": 1,599                        "id": "call_zXSIylHvc5x3JUAPcHZR5GZI",600                        "function": {"arguments": "", "name": "person"},601                        "type": "function",602                    }603                ]604            },605        },606        {607            "content": "",608            "additional_kwargs": {609                "tool_calls": [610                    {611                        "index": 1,612                        "id": None,613                        "function": {"arguments": '{"na', "name": None},614                        "type": None,615                    }616                ]617            },618        },619        {620            "content": "",621            "additional_kwargs": {622                "tool_calls": [623                    {624                        "index": 1,625                        "id": None,626                        "function": {"arguments": 'me": ', "name": None},627                        "type": None,628                    }629                ]630            },631        },632        {633            "content": "",634            "additional_kwargs": {635                "tool_calls": [636                    {637                        "index": 1,638                        "id": None,639                        "function": {"arguments": '"bob",', "name": None},640                        "type": None,641                    }642                ]643            },644        },645        {646            "content": "",647            "additional_kwargs": {648                "tool_calls": [649                    {650                        "index": 1,651                        "id": None,652                        "function": {"arguments": ' "ag', "name": None},653                        "type": None,654                    }655                ]656            },657        },658        {659            "content": "",660            "additional_kwargs": {661                "tool_calls": [662                    {663                        "index": 1,664                        "id": None,665                        "function": {"arguments": 'e": 3', "name": None},666                        "type": None,667                    }668                ]669            },670        },671        {672            "content": "",673            "additional_kwargs": {674                "tool_calls": [675                    {676                        "index": 1,677                        "id": None,678                        "function": {"arguments": "}", "name": None},679                        "type": None,680                    }681                ]682            },683        },684        {"content": ""},685    ]686687    final: BaseMessageChunk | None = None688689    for chunk in chunks:690        msg = AIMessageChunk(**chunk)691        final = msg if final is None else final + msg692693    assert final == AIMessageChunk(694        content="",695        additional_kwargs={696            "tool_calls": [697                {698                    "index": 0,699                    "id": "call_CwGAsESnXehQEjiAIWzinlva",700                    "function": {701                        "arguments": '{"name": "jane", "age": 2}',702                        "name": "person",703                    },704                    "type": "function",705                },706                {707                    "index": 1,708                    "id": "call_zXSIylHvc5x3JUAPcHZR5GZI",709                    "function": {710                        "arguments": '{"name": "bob", "age": 3}',711                        "name": "person",712                    },713                    "type": "function",714                },715            ]716        },717        tool_call_chunks=[718            {719                "name": "person",720                "args": '{"name": "jane", "age": 2}',721                "id": "call_CwGAsESnXehQEjiAIWzinlva",722                "index": 0,723                "type": "tool_call_chunk",724            },725            {726                "name": "person",727                "args": '{"name": "bob", "age": 3}',728                "id": "call_zXSIylHvc5x3JUAPcHZR5GZI",729                "index": 1,730                "type": "tool_call_chunk",731            },732        ],733        tool_calls=[734            {735                "name": "person",736                "args": {"name": "jane", "age": 2},737                "id": "call_CwGAsESnXehQEjiAIWzinlva",738                "type": "tool_call",739            },740            {741                "name": "person",742                "args": {"name": "bob", "age": 3},743                "id": "call_zXSIylHvc5x3JUAPcHZR5GZI",744                "type": "tool_call",745            },746        ],747    )748749750def test_convert_to_messages() -> None:751    # dicts752    actual = convert_to_messages(753        [754            {"role": "system", "content": "You are a helpful assistant."},755            {"role": "user", "content": "Hello!"},756            {"role": "ai", "content": "Hi!", "id": "ai1"},757            {"type": "human", "content": "Hello!", "name": "Jane", "id": "human1"},758            {759                "role": "assistant",760                "content": "Hi!",761                "name": "JaneBot",762                "function_call": {"name": "greet", "arguments": '{"name": "Jane"}'},763            },764            {"role": "function", "name": "greet", "content": "Hi!"},765            {766                "role": "assistant",767                "content": "",768                "tool_calls": [769                    {770                        "name": "greet",771                        "args": {"name": "Jane"},772                        "id": "tool_id",773                        "type": "tool_call",774                    }775                ],776            },777            {"role": "tool", "tool_call_id": "tool_id", "content": "Hi!"},778            {779                "role": "tool",780                "tool_call_id": "tool_id2",781                "content": "Bye!",782                "artifact": {"foo": 123},783                "status": "success",784            },785            {"role": "remove", "id": "message_to_remove", "content": ""},786            {787                "content": "Now the turn for Larry to ask a question about the book!",788                "additional_kwargs": {"metadata": {"speaker_name": "Presenter"}},789                "response_metadata": {},790                "type": "human",791                "name": None,792                "id": "1",793            },794        ]795    )796    expected = [797        SystemMessage(content="You are a helpful assistant."),798        HumanMessage(content="Hello!"),799        AIMessage(content="Hi!", id="ai1"),800        HumanMessage(content="Hello!", name="Jane", id="human1"),801        AIMessage(802            content="Hi!",803            name="JaneBot",804            additional_kwargs={805                "function_call": {"name": "greet", "arguments": '{"name": "Jane"}'}806            },807        ),808        FunctionMessage(name="greet", content="Hi!"),809        AIMessage(810            content="",811            tool_calls=[812                create_tool_call(name="greet", args={"name": "Jane"}, id="tool_id")813            ],814        ),815        ToolMessage(tool_call_id="tool_id", content="Hi!"),816        ToolMessage(817            tool_call_id="tool_id2",818            content="Bye!",819            artifact={"foo": 123},820            status="success",821        ),822        RemoveMessage(id="message_to_remove"),823        HumanMessage(824            content="Now the turn for Larry to ask a question about the book!",825            additional_kwargs={"metadata": {"speaker_name": "Presenter"}},826            response_metadata={},827            id="1",828        ),829    ]830    assert expected == actual831832    # tuples833    assert convert_to_messages(834        [835            ("system", "You are a helpful assistant."),836            "hello!",837            ("ai", "Hi!"),838            ("human", "Hello!"),839            ["assistant", "Hi!"],840        ]841    ) == [842        SystemMessage(content="You are a helpful assistant."),843        HumanMessage(content="hello!"),844        AIMessage(content="Hi!"),845        HumanMessage(content="Hello!"),846        AIMessage(content="Hi!"),847    ]848849850@pytest.mark.parametrize(851    "message_class",852    [853        AIMessage,854        AIMessageChunk,855        HumanMessage,856        HumanMessageChunk,857        SystemMessage,858    ],859)860def test_message_name(message_class: type) -> None:861    msg = message_class(content="foo", name="bar")862    assert msg.name == "bar"863864    msg2 = message_class(content="foo", name=None)865    assert msg2.name is None866867    msg3 = message_class(content="foo")868    assert msg3.name is None869870871@pytest.mark.parametrize(872    "message_class",873    [FunctionMessage, FunctionMessageChunk],874)875def test_message_name_function(message_class: type) -> None:876    # functionmessage doesn't support name=None877    msg = message_class(name="foo", content="bar")878    assert msg.name == "foo"879880881@pytest.mark.parametrize(882    "message_class",883    [ChatMessage, ChatMessageChunk],884)885def test_message_name_chat(message_class: type) -> None:886    msg = message_class(content="foo", role="user", name="bar")887    assert msg.name == "bar"888889    msg2 = message_class(content="foo", role="user", name=None)890    assert msg2.name is None891892    msg3 = message_class(content="foo", role="user")893    assert msg3.name is None894895896def test_merge_tool_calls() -> None:897    tool_call_1 = create_tool_call_chunk(name="tool1", args="", id="1", index=0)898    tool_call_2 = create_tool_call_chunk(899        name=None, args='{"arg1": "val', id=None, index=0900    )901    tool_call_3 = create_tool_call_chunk(name=None, args='ue}"', id=None, index=0)902    merged = merge_lists([tool_call_1], [tool_call_2])903    assert merged is not None904    assert merged == [905        {906            "name": "tool1",907            "args": '{"arg1": "val',908            "id": "1",909            "index": 0,910            "type": "tool_call_chunk",911        }912    ]913    merged = merge_lists(merged, [tool_call_3])914    assert merged is not None915    assert merged == [916        {917            "name": "tool1",918            "args": '{"arg1": "value}"',919            "id": "1",920            "index": 0,921            "type": "tool_call_chunk",922        }923    ]924925    left = create_tool_call_chunk(926        name="tool1", args='{"arg1": "value1"}', id="1", index=None927    )928    right = create_tool_call_chunk(929        name="tool2", args='{"arg2": "value2"}', id="1", index=None930    )931    merged = merge_lists([left], [right])932    assert merged is not None933    assert len(merged) == 2934935    left = create_tool_call_chunk(936        name="tool1", args='{"arg1": "value1"}', id=None, index=None937    )938    right = create_tool_call_chunk(939        name="tool1", args='{"arg2": "value2"}', id=None, index=None940    )941    merged = merge_lists([left], [right])942    assert merged is not None943    assert len(merged) == 2944945    left = create_tool_call_chunk(946        name="tool1", args='{"arg1": "value1"}', id="1", index=0947    )948    right = create_tool_call_chunk(949        name="tool2", args='{"arg2": "value2"}', id=None, index=1950    )951    merged = merge_lists([left], [right])952    assert merged is not None953    assert len(merged) == 2954955956def test_merge_tool_calls_parallel_same_index() -> None:957    """Test parallel tool calls with same index but different IDs."""958    # Two parallel tool calls with the same index but different IDs959    left = create_tool_call_chunk(960        name="read_file", args='{"path": "foo.txt"}', id="tooluse_ABC", index=0961    )962    right = create_tool_call_chunk(963        name="search_text", args='{"query": "bar"}', id="tooluse_DEF", index=0964    )965    merged = merge_lists([left], [right])966    assert merged is not None967    assert len(merged) == 2968    assert merged[0]["name"] == "read_file"969    assert merged[0]["id"] == "tooluse_ABC"970    assert merged[1]["name"] == "search_text"971    assert merged[1]["id"] == "tooluse_DEF"972973    # Streaming continuation: same index, id=None on continuation chunk974    # should still merge correctly with the original chunk975    first = create_tool_call_chunk(name="tool1", args="", id="id1", index=0)976    continuation = create_tool_call_chunk(977        name=None, args='{"key": "value"}', id=None, index=0978    )979    merged = merge_lists([first], [continuation])980    assert merged is not None981    assert len(merged) == 1982    assert merged[0]["name"] == "tool1"983    assert merged[0]["args"] == '{"key": "value"}'984    assert merged[0]["id"] == "id1"985986    # Three parallel tool calls all with the same index987    tc1 = create_tool_call_chunk(name="tool_a", args="{}", id="id_a", index=0)988    tc2 = create_tool_call_chunk(name="tool_b", args="{}", id="id_b", index=0)989    tc3 = create_tool_call_chunk(name="tool_c", args="{}", id="id_c", index=0)990    merged = merge_lists([tc1], [tc2], [tc3])991    assert merged is not None992    assert len(merged) == 3993    assert [m["name"] for m in merged] == ["tool_a", "tool_b", "tool_c"]994    assert [m["id"] for m in merged] == ["id_a", "id_b", "id_c"]995996997def test_tool_message_serdes() -> None:998    message = ToolMessage(999        "foo", artifact={"bar": {"baz": 123}}, tool_call_id="1", status="error"1000    )1001    ser_message = {1002        "lc": 1,1003        "type": "constructor",1004        "id": ["langchain", "schema", "messages", "ToolMessage"],1005        "kwargs": {1006            "content": "foo",1007            "type": "tool",1008            "tool_call_id": "1",1009            "artifact": {"bar": {"baz": 123}},1010            "status": "error",1011        },1012    }1013    assert dumpd(message) == ser_message1014    assert load(dumpd(message), allowed_objects=[ToolMessage]) == message101510161017class BadObject:1018    pass101910201021def test_tool_message_ser_non_serializable() -> None:1022    bad_obj = BadObject()1023    message = ToolMessage("foo", artifact=bad_obj, tool_call_id="1")1024    ser_message = {1025        "lc": 1,1026        "type": "constructor",1027        "id": ["langchain", "schema", "messages", "ToolMessage"],1028        "kwargs": {1029            "content": "foo",1030            "type": "tool",1031            "tool_call_id": "1",1032            "artifact": {1033                "lc": 1,1034                "type": "not_implemented",1035                "id": ["tests", "unit_tests", "test_messages", "BadObject"],1036                "repr": repr(bad_obj),1037            },1038            "status": "success",1039        },1040    }1041    assert dumpd(message) == ser_message1042    with pytest.raises(NotImplementedError):1043        load(dumpd(message), allowed_objects=[ToolMessage])104410451046def test_tool_message_to_dict() -> None:1047    message = ToolMessage("foo", artifact={"bar": {"baz": 123}}, tool_call_id="1")1048    expected = {1049        "type": "tool",1050        "data": {1051            "content": "foo",1052            "additional_kwargs": {},1053            "response_metadata": {},1054            "artifact": {"bar": {"baz": 123}},1055            "type": "tool",1056            "name": None,1057            "id": None,1058            "tool_call_id": "1",1059            "status": "success",1060        },1061    }1062    actual = message_to_dict(message)1063    assert actual == expected106410651066def test_tool_message_repr() -> None:1067    message = ToolMessage("foo", artifact={"bar": {"baz": 123}}, tool_call_id="1")1068    expected = (1069        "ToolMessage(content='foo', tool_call_id='1', artifact={'bar': {'baz': 123}})"1070    )1071    actual = repr(message)1072    assert expected == actual107310741075def test_tool_message_str() -> None:1076    message = ToolMessage("foo", artifact={"bar": {"baz": 123}}, tool_call_id="1")1077    expected = "content='foo' tool_call_id='1' artifact={'bar': {'baz': 123}}"1078    actual = str(message)1079    assert expected == actual108010811082@pytest.mark.parametrize(1083    ("first", "others", "expected"),1084    [1085        ("", [""], ""),1086        ("", [[]], [""]),1087        ([], [""], []),1088        ([], [[]], []),1089        ("foo", [""], "foo"),1090        ("foo", [[]], ["foo"]),1091        (["foo"], [""], ["foo"]),1092        (["foo"], [[]], ["foo"]),1093        ("foo", ["bar"], "foobar"),1094        ("foo", [["bar"]], ["foo", "bar"]),1095        (["foo"], ["bar"], ["foobar"]),1096        (["foo"], [["bar"]], ["foo", "bar"]),1097        (1098            [{"text": "foo"}],1099            [[{"index": 0, "text": "bar"}]],1100            [{"text": "foo"}, {"index": 0, "text": "bar"}],1101        ),1102    ],1103)1104def test_merge_content(1105    first: str | list[str | dict[str, Any]],1106    others: str | list[str | dict[str, Any]],1107    expected: str | list[str | dict[str, Any]],1108) -> None:1109    actual = merge_content(first, *others)1110    assert actual == expected111111121113@pytest.mark.parametrize(1114    "first",1115    [1116        [{"type": "text", "text": "a"}],1117        ["x"],1118    ],1119)1120def test_merge_content_leaves_the_first_list_unchanged(1121    first: list[str | dict[str, Any]],1122) -> None:1123    original = list(first)1124    merge_content(first, "b")1125    assert first == original112611271128def test_tool_message_content() -> None:1129    ToolMessage("foo", tool_call_id="1")1130    ToolMessage(["foo"], tool_call_id="1")1131    ToolMessage([{"foo": "bar"}], tool_call_id="1")11321133    # Ignoring since we're testing that tuples get converted to lists in `coerce_args`1134    assert ToolMessage(("a", "b", "c"), tool_call_id="1").content == ["a", "b", "c"]  # type: ignore[call-overload]1135    assert ToolMessage(5, tool_call_id="1").content == "5"  # type: ignore[call-overload]1136    assert ToolMessage(5.1, tool_call_id="1").content == "5.1"  # type: ignore[call-overload]1137    assert ToolMessage({"foo": "bar"}, tool_call_id="1").content == "{'foo': 'bar'}"  # type: ignore[call-overload]1138    assert (1139        ToolMessage(Document("foo"), tool_call_id="1").content == "page_content='foo'"  # type: ignore[call-overload]1140    )114111421143def test_tool_message_tool_call_id() -> None:1144    ToolMessage("foo", tool_call_id="1")1145    ToolMessage("foo", tool_call_id=uuid.uuid4())1146    ToolMessage("foo", tool_call_id=1)1147    ToolMessage("foo", tool_call_id=1.0)114811491150def test_message_text() -> None:1151    # partitions:1152    # message types: [ai], [human], [system], [tool]1153    # content types: [str], [list[str]], [list[dict]], [list[str | dict]]1154    # content: [empty], [single element], [multiple elements]1155    # content dict types: [text], [not text], [no type]11561157    assert HumanMessage(content="foo").text == "foo"1158    assert AIMessage(content=[]).text == ""1159    assert AIMessage(content=["foo", "bar"]).text == "foobar"1160    assert (1161        AIMessage(1162            content=[1163                {"type": "text", "text": "<thinking>thinking...</thinking>"},1164                {1165                    "type": "tool_use",1166                    "id": "toolu_01A09q90qw90lq917835lq9",1167                    "name": "get_weather",1168                    "input": {"location": "San Francisco, CA"},1169                },1170            ]1171        ).text1172        == "<thinking>thinking...</thinking>"1173    )1174    assert (1175        SystemMessage(content=[{"type": "text", "text": "foo"}, "bar"]).text == "foobar"1176    )1177    assert (1178        ToolMessage(1179            content=[1180                {"type": "text", "text": "15 degrees"},1181                {1182                    "type": "image",1183                    "source": {1184                        "type": "base64",1185                        "media_type": "image/jpeg",1186                        "data": "/9j/4AAQSkZJRg...",1187                    },1188                },1189            ],1190            tool_call_id="1",1191        ).text1192        == "15 degrees"1193    )1194    assert (1195        AIMessage(content=[{"text": "hi there"}, "hi"]).text == "hi"1196    )  # missing type: text1197    assert AIMessage(content=[{"type": "nottext", "text": "hi"}]).text == ""1198    assert AIMessage(content=[]).text == ""1199    assert (1200        AIMessage(1201            content="", tool_calls=[create_tool_call(name="a", args={"b": 1}, id=None)]1202        ).text1203        == ""1204    )120512061207def test_is_data_content_block() -> None:1208    # Test all DataContentBlock types with various data fields12091210    # Image blocks1211    assert is_data_content_block({"type": "image", "url": "https://..."})1212    assert is_data_content_block(1213        {1214            "type": "image",1215            "base64": "<base64 data>",1216            "mime_type": "image/jpeg",1217        }1218    )12191220    # Video blocks1221    assert is_data_content_block({"type": "video", "url": "https://video.mp4"})1222    assert is_data_content_block(1223        {1224            "type": "video",1225            "base64": "<base64 video>",1226            "mime_type": "video/mp4",1227        }1228    )1229    assert is_data_content_block({"type": "video", "file_id": "vid_123"})12301231    # Audio blocks1232    assert is_data_content_block({"type": "audio", "url": "https://audio.mp3"})1233    assert is_data_content_block(1234        {1235            "type": "audio",1236            "base64": "<base64 audio>",1237            "mime_type": "audio/mp3",1238        }1239    )1240    assert is_data_content_block({"type": "audio", "file_id": "aud_123"})12411242    # Plain text blocks1243    assert is_data_content_block({"type": "text-plain", "text": "document content"})1244    assert is_data_content_block({"type": "text-plain", "url": "https://doc.txt"})1245    assert is_data_content_block({"type": "text-plain", "file_id": "txt_123"})12461247    # File blocks1248    assert is_data_content_block({"type": "file", "url": "https://file.pdf"})1249    assert is_data_content_block(1250        {1251            "type": "file",1252            "base64": "<base64 file>",1253            "mime_type": "application/pdf",1254        }1255    )1256    assert is_data_content_block({"type": "file", "file_id": "file_123"})12571258    # Blocks with additional metadata (should still be valid)1259    assert is_data_content_block(1260        {1261            "type": "image",1262            "base64": "<base64 data>",1263            "mime_type": "image/jpeg",1264            "cache_control": {"type": "ephemeral"},1265        }1266    )1267    assert is_data_content_block(1268        {1269            "type": "image",1270            "base64": "<base64 data>",1271            "mime_type": "image/jpeg",1272            "metadata": {"cache_control": {"type": "ephemeral"}},1273        }1274    )1275    assert is_data_content_block(1276        {1277            "type": "image",1278            "base64": "<base64 data>",1279            "mime_type": "image/jpeg",1280            "extras": "hi",1281        }1282    )12831284    # Invalid cases - wrong type1285    assert not is_data_content_block({"type": "text", "text": "foo"})1286    assert not is_data_content_block(1287        {1288            "type": "image_url",1289            "image_url": {"url": "https://..."},1290        }  # This is OpenAI Chat Completions1291    )1292    assert not is_data_content_block({"type": "tool_call", "name": "func", "args": {}})1293    assert not is_data_content_block({"type": "invalid", "url": "something"})12941295    # Invalid cases - valid type but no data or `source_type` fields1296    assert not is_data_content_block({"type": "image"})1297    assert not is_data_content_block({"type": "video", "mime_type": "video/mp4"})1298    assert not is_data_content_block({"type": "audio", "extras": {"key": "value"}})12991300    # Invalid cases - valid type but wrong data field name1301    assert not is_data_content_block({"type": "image", "source": "<base64 data>"})1302    assert not is_data_content_block({"type": "video", "data": "video_data"})13031304    # Edge cases - empty or missing values1305    assert not is_data_content_block({})1306    assert not is_data_content_block({"url": "https://..."})  # missing type130713081309def test_convert_to_openai_image_block() -> None:1310    for input_block in [1311        {1312            "type": "image",1313            "url": "https://...",1314            "cache_control": {"type": "ephemeral"},1315        },1316        {1317            "type": "image",1318            "source_type": "url",1319            "url": "https://...",1320            "cache_control": {"type": "ephemeral"},1321        },1322    ]:1323        expected = {1324            "type": "image_url",1325            "image_url": {"url": "https://..."},1326        }1327        result = convert_to_openai_image_block(input_block)1328        assert result == expected13291330    for input_block in [1331        {1332            "type": "image",1333            "base64": "<base64 data>",1334            "mime_type": "image/jpeg",1335            "cache_control": {"type": "ephemeral"},1336        },1337        {1338            "type": "image",1339            "source_type": "base64",1340            "data": "<base64 data>",1341            "mime_type": "image/jpeg",1342            "cache_control": {"type": "ephemeral"},1343        },1344    ]:1345        expected = {1346            "type": "image_url",1347            "image_url": {1348                "url": "data:image/jpeg;base64,<base64 data>",1349            },1350        }1351        result = convert_to_openai_image_block(input_block)1352        assert result == expected135313541355def test_known_block_types() -> None:1356    expected = {1357        bt1358        for bt in get_args(ContentBlock)1359        for bt in get_args(bt.__annotations__["type"])1360    }1361    # Normalize any Literal[...] types in block types to their string values.1362    # This ensures all entries are plain strings, not Literal objects.1363    expected = {1364        t1365        if isinstance(t, str)1366        else t.__args__[0]1367        if hasattr(t, "__args__") and len(t.__args__) == 11368        else t1369        for t in expected1370    }1371    assert expected == KNOWN_BLOCK_TYPES137213731374def test_typed_init() -> None:1375    ai_message = AIMessage(content_blocks=[{"type": "text", "text": "Hello"}])1376    assert ai_message.content == [{"type": "text", "text": "Hello"}]1377    assert ai_message.content_blocks == ai_message.content13781379    human_message = HumanMessage(content_blocks=[{"type": "text", "text": "Hello"}])1380    assert human_message.content == [{"type": "text", "text": "Hello"}]1381    assert human_message.content_blocks == human_message.content13821383    system_message = SystemMessage(content_blocks=[{"type": "text", "text": "Hello"}])1384    assert system_message.content == [{"type": "text", "text": "Hello"}]1385    assert system_message.content_blocks == system_message.content13861387    tool_message = ToolMessage(1388        content_blocks=[{"type": "text", "text": "Hello"}],1389        tool_call_id="abc123",1390    )1391    assert tool_message.content == [{"type": "text", "text": "Hello"}]1392    assert tool_message.content_blocks == tool_message.content13931394    for message_class in [AIMessage, HumanMessage, SystemMessage]:1395        message = message_class("Hello")1396        assert message.content == "Hello"1397        assert message.content_blocks == [{"type": "text", "text": "Hello"}]13981399        message = message_class(content="Hello")1400        assert message.content == "Hello"1401        assert message.content_blocks == [{"type": "text", "text": "Hello"}]14021403    # Test we get type errors for malformed blocks (type checker will complain if1404    # below type-ignores are unused).1405    _ = AIMessage(content_blocks=[{"type": "text", "bad": "Hello"}])  # type: ignore[list-item]1406    _ = HumanMessage(content_blocks=[{"type": "text", "bad": "Hello"}])  # type: ignore[list-item]1407    _ = SystemMessage(content_blocks=[{"type": "text", "bad": "Hello"}])  # type: ignore[list-item]1408    _ = ToolMessage(1409        content_blocks=[{"type": "text", "bad": "Hello"}],  # type: ignore[list-item]1410        tool_call_id="abc123",1411    )141214131414# Calling `.text()` as a method is the deprecated path under test here, so the1415# warning it emits is expected rather than something to fix.1416@pytest.mark.filterwarnings(1417    r"ignore:Calling \.text\(\) as a method is deprecated:"1418    r"langchain_core._api.deprecation.LangChainDeprecationWarning"1419)1420def test_text_accessor() -> None:1421    """Test that `message.text` property and `.text()` method return the same value."""1422    human_msg = HumanMessage(content="Hello world")1423    assert human_msg.text == "Hello world"1424    assert human_msg.text() == "Hello world"1425    assert str(human_msg.text) == str(human_msg.text())14261427    system_msg = SystemMessage(content="You are a helpful assistant")1428    assert system_msg.text == "You are a helpful assistant"1429    assert system_msg.text() == "You are a helpful assistant"1430    assert str(system_msg.text) == str(system_msg.text())14311432    ai_msg = AIMessage(content="I can help you with that")1433    assert ai_msg.text == "I can help you with that"1434    assert ai_msg.text() == "I can help you with that"1435    assert str(ai_msg.text) == str(ai_msg.text())14361437    tool_msg = ToolMessage(content="Task completed", tool_call_id="tool_1")1438    assert tool_msg.text == "Task completed"1439    assert tool_msg.text() == "Task completed"1440    assert str(tool_msg.text) == str(tool_msg.text())14411442    complex_msg = HumanMessage(1443        content=[{"type": "text", "text": "Hello "}, {"type": "text", "text": "world"}]1444    )1445    assert complex_msg.text == "Hello world"1446    assert complex_msg.text() == "Hello world"1447    assert str(complex_msg.text) == str(complex_msg.text())14481449    mixed_msg = AIMessage(1450        content=[1451            {"type": "text", "text": "The answer is "},1452            {"type": "tool_use", "name": "calculate", "input": {"x": 2}, "id": "1"},1453            {"type": "text", "text": "42"},1454        ]1455    )1456    assert mixed_msg.text == "The answer is 42"1457    assert mixed_msg.text() == "The answer is 42"1458    assert str(mixed_msg.text) == str(mixed_msg.text())14591460    empty_msg = HumanMessage(content=[])1461    assert empty_msg.text == ""1462    assert empty_msg.text() == ""1463    assert str(empty_msg.text) == str(empty_msg.text())146414651466def test_text_accessor_deprecation_warning() -> None:1467    """Test that calling `.text()` as a method emits a deprecation warning."""1468    with pytest.warns(1469        LangChainDeprecationWarning,1470        match=r"Calling \.text\(\) as a method is deprecated",1471    ):1472        HumanMessage(content="Hello world").text()

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.

Generated by searchcode.com · powered by code_get_file + code_get_findings