1"""Test chat model integration."""23from __future__ import annotations45import copy6import json7import os8import warnings9from collections.abc import Callable10from types import SimpleNamespace11from typing import Any, Literal, cast12from unittest.mock import MagicMock, patch1314import anthropic15import pytest16from anthropic.types import Message, TextBlock, Usage17from blockbuster import blockbuster_ctx18from langchain_core.exceptions import (19 ContextOverflowError,20 ModelAPIError,21 ModelAuthenticationError,22 ModelConnectionError,23 ModelError,24 ModelInvalidRequestError,25 ModelNotFoundError,26 ModelPermissionDeniedError,27 ModelRateLimitError,28 ModelRefusalError,29 ModelTimeoutError,30)31from langchain_core.load import dumpd32from langchain_core.messages import (33 AIMessage,34 AIMessageChunk,35 BaseMessage,36 HumanMessage,37 SystemMessage,38 ToolMessage,39)40from langchain_core.messages.content import create_text_block41from langchain_core.outputs import ChatGenerationChunk42from langchain_core.runnables import RunnableBinding43from langchain_core.tools import BaseTool, tool44from langchain_core.tracers._streaming import _V2StreamingCallbackHandler45from langchain_core.tracers.base import BaseTracer46from langchain_core.tracers.schemas import Run47from langchain_core.utils._gateway import (48 GATEWAY_METADATA_RESPONSE_KEY,49 langsmith_gateway,50)51from pydantic import BaseModel, Field, RootModel, SecretStr, ValidationError52from pytest import CaptureFixture, MonkeyPatch5354from langchain_anthropic import ChatAnthropic55from langchain_anthropic._sdk_compat import _unsupported_sampling_params56from langchain_anthropic._version import __version__57from langchain_anthropic.chat_models import (58 _TOOL_CALL_ID_PATTERN,59 _create_usage_metadata,60 _drop_unsupported_root_composition_tools,61 _format_image,62 _format_messages,63 _is_builtin_tool,64 _merge_messages,65 _normalize_tool_call_id,66 _thinking_in_params,67 convert_to_anthropic_tool,68)69from tests.unit_tests._httpx_compat import httpx7071os.environ["ANTHROPIC_API_KEY"] = "foo"7273MODEL_NAME = "claude-sonnet-4-5-20250929"7475MID_CONVERSATION_SYSTEM_MODEL = "claude-opus-5"767778class _GatewayMetadataTracer(BaseTracer):79 """Captures gateway metadata promoted onto completed LLM runs."""8081 def __init__(self) -> None:82 super().__init__()83 self.gateway_metadata: dict[str, Any] | None = None8485 def _persist_run(self, run: Run) -> None:86 """No-op; runs are inspected as they complete."""8788 def _on_llm_end(self, run: Run) -> None:89 metadata = run.extra.get("metadata", {})90 gateway_metadata = metadata.get("ls_gateway_info")91 if isinstance(gateway_metadata, dict):92 self.gateway_metadata = gateway_metadata939495class _TokenEventTracer(BaseTracer, _V2StreamingCallbackHandler):96 """Records LLM run events with a v2 handler attached, as LangGraph does."""9798 def __init__(self) -> None:99 super().__init__()100 self.token_events: list[dict[str, Any]] = []101102 def _persist_run(self, run: Run) -> None:103 """No-op; runs are inspected as they complete."""104105 def _on_llm_end(self, run: Run) -> None:106 self.token_events = [107 event["kwargs"] for event in run.events if event["name"] == "new_token"108 ]109110111_GATEWAY_METADATA = {"provider": "anthropic"}112_MESSAGE_RESPONSE = {113 "id": "msg_123",114 "content": [{"type": "text", "text": "Bar Baz", "citations": None}],115 "model": MODEL_NAME,116 "role": "assistant",117 "stop_reason": "end_turn",118 "stop_sequence": None,119 "usage": {"input_tokens": 2, "output_tokens": 1},120 "type": "message",121}122_STREAM_EVENTS: list[dict[str, Any]] = [123 {124 "type": "message_start",125 "message": {126 **_MESSAGE_RESPONSE,127 "content": [],128 "stop_reason": None,129 "usage": {"input_tokens": 2, "output_tokens": 0},130 },131 },132 {133 "type": "content_block_delta",134 "index": 0,135 "delta": {"type": "text_delta", "text": "Bar Baz"},136 },137 {138 "type": "message_delta",139 "delta": {"stop_reason": "end_turn", "stop_sequence": None},140 "usage": {"input_tokens": 2, "output_tokens": 1},141 },142 {"type": "message_stop"},143]144145146def _gateway_handler(147 expected_beta: str | None,148) -> Callable[[Any], Any]:149 # Annotated `Any`: the concrete request/response classes come from `httpx`150 # or `httpx2` depending on the installed anthropic SDK.151 def handler(request: Any) -> Any:152 assert request.headers.get("anthropic-beta") == expected_beta153 headers = {"x-langsmith-gateway-metadata": json.dumps(_GATEWAY_METADATA)}154 if json.loads(request.content).get("stream"):155 stream = "".join(156 f"event: {event['type']}\ndata: {json.dumps(event)}\n\n"157 for event in _STREAM_EVENTS158 )159 return httpx.Response(160 200,161 text=stream,162 headers={**headers, "content-type": "text/event-stream"},163 )164 return httpx.Response(200, json=_MESSAGE_RESPONSE, headers=headers)165166 return handler167168169def _sync_gateway_client(betas: list[str] | None) -> anthropic.Client:170 return anthropic.Client(171 api_key="lsv2_pt_example",172 http_client=httpx.Client(173 transport=httpx.MockTransport(174 _gateway_handler(",".join(betas) if betas else None)175 )176 ),177 )178179180def _async_gateway_client(betas: list[str] | None) -> anthropic.AsyncClient:181 return anthropic.AsyncClient(182 api_key="lsv2_pt_example",183 http_client=httpx.AsyncClient(184 transport=httpx.MockTransport(185 _gateway_handler(",".join(betas) if betas else None)186 )187 ),188 )189190191@pytest.mark.parametrize("betas", [None, ["test-beta"]])192def test_anthropic_invoke_surfaces_gateway_metadata(193 betas: list[str] | None,194) -> None:195 """Gateway metadata header is surfaced on `generation_info`, not the message."""196 llm = ChatAnthropic(197 model=MODEL_NAME,198 api_key="lsv2_pt_example",199 betas=betas,200 max_tokens=10,201 )202 client = _sync_gateway_client(betas)203 tracer = _GatewayMetadataTracer()204 try:205 with patch.object(llm, "_client", client):206 result = llm.invoke("bar", config={"callbacks": [tracer]})207 finally:208 client.close()209210 assert tracer.gateway_metadata == _GATEWAY_METADATA211 assert GATEWAY_METADATA_RESPONSE_KEY not in result.response_metadata212213214@pytest.mark.parametrize("betas", [None, ["test-beta"]])215async def test_anthropic_ainvoke_surfaces_gateway_metadata(216 betas: list[str] | None,217) -> None:218 """Async gateway responses surface metadata on `generation_info`."""219 llm = ChatAnthropic(220 model=MODEL_NAME,221 api_key="lsv2_pt_example",222 betas=betas,223 max_tokens=10,224 )225 client = _async_gateway_client(betas)226 tracer = _GatewayMetadataTracer()227 try:228 with patch.object(llm, "_async_client", client):229 result = await llm.ainvoke("bar", config={"callbacks": [tracer]})230 finally:231 await client.close()232233 assert tracer.gateway_metadata == _GATEWAY_METADATA234 assert GATEWAY_METADATA_RESPONSE_KEY not in result.response_metadata235236237@pytest.mark.parametrize("betas", [None, ["test-beta"]])238def test_anthropic_stream_surfaces_gateway_metadata(239 betas: list[str] | None,240) -> None:241 """Gateway metadata is attached to the first streaming generation chunk."""242 llm = ChatAnthropic(243 model=MODEL_NAME,244 api_key="lsv2_pt_example",245 betas=betas,246 max_tokens=10,247 )248 client = _sync_gateway_client(betas)249 try:250 with patch.object(llm, "_client", client):251 chunks = list(llm._stream([HumanMessage("bar")]))252 finally:253 client.close()254255 assert [chunk.generation_info for chunk in chunks] == [256 {GATEWAY_METADATA_RESPONSE_KEY: _GATEWAY_METADATA},257 None,258 None,259 ]260261262@pytest.mark.parametrize("betas", [None, ["test-beta"]])263async def test_anthropic_astream_surfaces_gateway_metadata(264 betas: list[str] | None,265) -> None:266 """Async gateway metadata is attached to the first streaming chunk."""267 llm = ChatAnthropic(268 model=MODEL_NAME,269 api_key="lsv2_pt_example",270 betas=betas,271 max_tokens=10,272 )273 client = _async_gateway_client(betas)274 try:275 with patch.object(llm, "_async_client", client):276 chunks = [chunk async for chunk in llm._astream([HumanMessage("bar")])]277 finally:278 await client.close()279280 assert [chunk.generation_info for chunk in chunks] == [281 {GATEWAY_METADATA_RESPONSE_KEY: _GATEWAY_METADATA},282 None,283 None,284 ]285286287def test_stream_with_tools_reports_tokens_to_v2_handlers(dummy_tool: BaseTool) -> None:288 """Block-list chunks still fire `on_llm_new_token`, so tracers see TTFT."""289 llm = ChatAnthropic(model=MODEL_NAME, max_tokens=10)290 tracer = _TokenEventTracer()291 with _sync_gateway_client(None) as client, patch.object(llm, "_client", client):292 llm.bind_tools([dummy_tool]).invoke("bar", config={"callbacks": [tracer]})293294 assert [event["token"] for event in tracer.token_events] == ["", "Bar Baz", ""]295 assert tracer.token_events[1]["chunk"].message.content == [296 {"type": "text", "text": "Bar Baz", "index": 0}297 ]298299300async def test_astream_with_tools_reports_tokens_to_v2_handlers(301 dummy_tool: BaseTool,302) -> None:303 """Async block-list chunks still fire `on_llm_new_token`."""304 llm = ChatAnthropic(model=MODEL_NAME, max_tokens=10)305 tracer = _TokenEventTracer()306 async with _async_gateway_client(None) as client:307 with patch.object(llm, "_async_client", client):308 await llm.bind_tools([dummy_tool]).ainvoke(309 "bar", config={"callbacks": [tracer]}310 )311312 assert [event["token"] for event in tracer.token_events] == ["", "Bar Baz", ""]313 assert tracer.token_events[1]["chunk"].message.content == [314 {"type": "text", "text": "Bar Baz", "index": 0}315 ]316317318def test_initialization() -> None:319 """Test chat model initialization."""320 with patch.dict(os.environ, {"ANTHROPIC_API_URL": "https://api.anthropic.com"}):321 for model in [322 ChatAnthropic(model_name=MODEL_NAME, api_key="xyz", timeout=2), # type: ignore[arg-type, call-arg]323 ChatAnthropic( # type: ignore[call-arg, call-arg, call-arg]324 model=MODEL_NAME,325 anthropic_api_key="xyz",326 default_request_timeout=2,327 base_url="https://api.anthropic.com",328 ),329 ]:330 assert model.model == MODEL_NAME331 assert (332 cast("SecretStr", model.anthropic_api_key).get_secret_value() == "xyz"333 )334 assert model.default_request_timeout == 2.0335 assert model.anthropic_api_url == "https://api.anthropic.com"336337338def test_user_agent_header_in_client_params() -> None:339 """Test that _client_params includes a User-Agent header."""340 llm = ChatAnthropic(model=MODEL_NAME, api_key="test-key") # type: ignore[arg-type]341 params = llm._client_params342 assert "default_headers" in params343 assert "User-Agent" in params["default_headers"]344 assert params["default_headers"]["User-Agent"].startswith("langchain-anthropic/")345346347@pytest.mark.parametrize("async_api", [True, False])348def test_streaming_attribute_should_stream(async_api: bool) -> None: # noqa: FBT001349 llm = ChatAnthropic(model=MODEL_NAME, streaming=True)350 assert llm._should_stream(async_api=async_api)351352353def test_anthropic_client_caching() -> None:354 """Test that the OpenAI client is cached."""355 llm1 = ChatAnthropic(model=MODEL_NAME)356 llm2 = ChatAnthropic(model=MODEL_NAME)357 assert llm1._client._client is llm2._client._client358359 llm3 = ChatAnthropic(model=MODEL_NAME, base_url="foo")360 assert llm1._client._client is not llm3._client._client361362 llm4 = ChatAnthropic(model=MODEL_NAME, timeout=None)363 assert llm1._client._client is llm4._client._client364365 llm5 = ChatAnthropic(model=MODEL_NAME, timeout=3)366 assert llm1._client._client is not llm5._client._client367368369def test_anthropic_proxy_support() -> None:370 """Test that both sync and async clients support proxy configuration."""371 proxy_url = "http://proxy.example.com:8080"372373 # Test sync client with proxy374 llm_sync = ChatAnthropic(model=MODEL_NAME, anthropic_proxy=proxy_url)375 sync_client = llm_sync._client376 assert sync_client is not None377378 # Test async client with proxy - this should not raise TypeError379 async_client = llm_sync._async_client380 assert async_client is not None381382 # Test that clients with different proxy settings are not cached together383 llm_no_proxy = ChatAnthropic(model=MODEL_NAME)384 llm_with_proxy = ChatAnthropic(model=MODEL_NAME, anthropic_proxy=proxy_url)385386 # Different proxy settings should result in different cached clients387 assert llm_no_proxy._client._client is not llm_with_proxy._client._client388389390def test_anthropic_proxy_from_environment() -> None:391 """Test that proxy can be set from ANTHROPIC_PROXY environment variable."""392 proxy_url = "http://env-proxy.example.com:8080"393394 # Test with environment variable set395 with patch.dict(os.environ, {"ANTHROPIC_PROXY": proxy_url}):396 llm = ChatAnthropic(model=MODEL_NAME)397 assert llm.anthropic_proxy == proxy_url398399 # Should be able to create clients successfully400 sync_client = llm._client401 async_client = llm._async_client402 assert sync_client is not None403 assert async_client is not None404405 # Test that explicit parameter overrides environment variable406 with patch.dict(os.environ, {"ANTHROPIC_PROXY": "http://env-proxy.com"}):407 explicit_proxy = "http://explicit-proxy.com"408 llm = ChatAnthropic(model=MODEL_NAME, anthropic_proxy=explicit_proxy)409 assert llm.anthropic_proxy == explicit_proxy410411412def test_set_default_max_tokens() -> None:413 """Test the set_default_max_tokens function."""414 # Test claude-sonnet-4-5 models415 llm = ChatAnthropic(model="claude-sonnet-4-5-20250929", anthropic_api_key="test")416 assert llm.max_tokens == 64000417418 # Test claude-haiku-4-5 models419 llm = ChatAnthropic(model="claude-haiku-4-5-20251001", anthropic_api_key="test")420 assert llm.max_tokens == 64000421422 # Test claude-3-5-haiku models (profile removed, should fall back to 4096)423 llm = ChatAnthropic(model="claude-3-5-haiku-20241022", anthropic_api_key="test")424 assert llm.max_tokens == 4096425426 # Test claude-3-haiku models (should default to 4096)427 llm = ChatAnthropic(model="claude-3-haiku-20240307", anthropic_api_key="test")428 assert llm.max_tokens == 4096429430 # Test that existing max_tokens values are preserved431 llm = ChatAnthropic(model=MODEL_NAME, max_tokens=2048, anthropic_api_key="test")432 assert llm.max_tokens == 2048433434 # Test that explicitly set max_tokens values are preserved435 llm = ChatAnthropic(model=MODEL_NAME, max_tokens=4096, anthropic_api_key="test")436 assert llm.max_tokens == 4096437438439@pytest.mark.requires("anthropic")440def test_anthropic_model_name_param() -> None:441 llm = ChatAnthropic(model_name=MODEL_NAME) # type: ignore[call-arg, call-arg]442 assert llm.model == MODEL_NAME443444445@pytest.mark.requires("anthropic")446def test_anthropic_model_param() -> None:447 llm = ChatAnthropic(model=MODEL_NAME) # type: ignore[call-arg]448 assert llm.model == MODEL_NAME449450451@pytest.mark.requires("anthropic")452def test_anthropic_model_kwargs() -> None:453 llm = ChatAnthropic(model_name=MODEL_NAME, model_kwargs={"foo": "bar"}) # type: ignore[call-arg, call-arg]454 assert llm.model_kwargs == {"foo": "bar"}455456457@pytest.mark.requires("anthropic")458def test_anthropic_fields_in_model_kwargs() -> None:459 """Test that for backwards compatibility fields can be passed in as model_kwargs."""460 with pytest.warns(461 UserWarning,462 match=(463 "Parameters {'max_tokens_to_sample'} should be specified explicitly. "464 "Instead they were passed in as part of `model_kwargs` parameter."465 ),466 ):467 llm = ChatAnthropic(model=MODEL_NAME, model_kwargs={"max_tokens_to_sample": 5}) # type: ignore[call-arg]468 assert llm.max_tokens == 5469 with pytest.warns(470 UserWarning,471 match=(472 "Parameters {'max_tokens'} should be specified explicitly. Instead they "473 "were passed in as part of `model_kwargs` parameter."474 ),475 ):476 llm = ChatAnthropic(model=MODEL_NAME, model_kwargs={"max_tokens": 5}) # type: ignore[call-arg]477 assert llm.max_tokens == 5478479480@pytest.mark.requires("anthropic")481def test_anthropic_incorrect_field() -> None:482 with pytest.warns(match="not default parameter"):483 llm = ChatAnthropic(model=MODEL_NAME, foo="bar") # type: ignore[call-arg, call-arg]484 assert llm.model_kwargs == {"foo": "bar"}485486487@pytest.mark.requires("anthropic")488def test_anthropic_initialization() -> None:489 """Test anthropic initialization."""490 # Verify that chat anthropic can be initialized using a secret key provided491 # as a parameter rather than an environment variable.492 ChatAnthropic(model=MODEL_NAME, anthropic_api_key="test") # type: ignore[call-arg, call-arg]493494495def test__format_output() -> None:496 anthropic_msg = Message(497 id="foo",498 content=[TextBlock(type="text", text="bar")],499 model="baz",500 role="assistant",501 stop_reason=None,502 stop_sequence=None,503 usage=Usage(input_tokens=2, output_tokens=1),504 type="message",505 )506 expected = AIMessage( # type: ignore[misc]507 "bar",508 usage_metadata={509 "input_tokens": 2,510 "output_tokens": 1,511 "total_tokens": 3,512 "input_token_details": {},513 },514 response_metadata={"model_provider": "anthropic"},515 )516 llm = ChatAnthropic(model=MODEL_NAME, anthropic_api_key="test") # type: ignore[call-arg, call-arg]517 actual = llm._format_output(anthropic_msg)518 assert actual.generations[0].message == expected519520521def test__format_output_cached() -> None:522 anthropic_msg = Message(523 id="foo",524 content=[TextBlock(type="text", text="bar")],525 model="baz",526 role="assistant",527 stop_reason=None,528 stop_sequence=None,529 usage=Usage(530 input_tokens=2,531 output_tokens=1,532 cache_creation_input_tokens=3,533 cache_read_input_tokens=4,534 ),535 type="message",536 )537 expected = AIMessage( # type: ignore[misc]538 "bar",539 usage_metadata={540 "input_tokens": 9,541 "output_tokens": 1,542 "total_tokens": 10,543 "input_token_details": {"cache_creation": 3, "cache_read": 4},544 },545 response_metadata={"model_provider": "anthropic"},546 )547548 llm = ChatAnthropic(model=MODEL_NAME, anthropic_api_key="test") # type: ignore[call-arg, call-arg]549 actual = llm._format_output(anthropic_msg)550 assert actual.generations[0].message == expected551552553def test__merge_messages() -> None:554 messages = [555 SystemMessage("foo"), # type: ignore[misc]556 HumanMessage("bar"), # type: ignore[misc]557 AIMessage( # type: ignore[misc]558 [559 {"text": "baz", "type": "text"},560 {561 "tool_input": {"a": "b"},562 "type": "tool_use",563 "id": "1",564 "text": None,565 "name": "buz",566 },567 {"text": "baz", "type": "text"},568 {569 "tool_input": {"a": "c"},570 "type": "tool_use",571 "id": "2",572 "text": None,573 "name": "blah",574 },575 {576 "tool_input": {"a": "c"},577 "type": "tool_use",578 "id": "3",579 "text": None,580 "name": "blah",581 },582 ],583 ),584 ToolMessage("buz output", tool_call_id="1", status="error"), # type: ignore[misc]585 ToolMessage(586 content=[587 {588 "type": "image",589 "source": {590 "type": "base64",591 "media_type": "image/jpeg",592 "data": "fake_image_data",593 },594 },595 ],596 tool_call_id="2",597 ), # type: ignore[misc]598 ToolMessage([], tool_call_id="3"), # type: ignore[misc]599 HumanMessage("next thing"), # type: ignore[misc]600 ]601 expected = [602 SystemMessage("foo"), # type: ignore[misc]603 HumanMessage("bar"), # type: ignore[misc]604 AIMessage( # type: ignore[misc]605 [606 {"text": "baz", "type": "text"},607 {608 "tool_input": {"a": "b"},609 "type": "tool_use",610 "id": "1",611 "text": None,612 "name": "buz",613 },614 {"text": "baz", "type": "text"},615 {616 "tool_input": {"a": "c"},617 "type": "tool_use",618 "id": "2",619 "text": None,620 "name": "blah",621 },622 {623 "tool_input": {"a": "c"},624 "type": "tool_use",625 "id": "3",626 "text": None,627 "name": "blah",628 },629 ],630 ),631 HumanMessage( # type: ignore[misc]632 [633 {634 "type": "tool_result",635 "content": "buz output",636 "tool_use_id": "1",637 "is_error": True,638 },639 {640 "type": "tool_result",641 "content": [642 {643 "type": "image",644 "source": {645 "type": "base64",646 "media_type": "image/jpeg",647 "data": "fake_image_data",648 },649 },650 ],651 "tool_use_id": "2",652 "is_error": False,653 },654 {655 "type": "tool_result",656 "content": [],657 "tool_use_id": "3",658 "is_error": False,659 },660 {"type": "text", "text": "next thing"},661 ],662 ),663 ]664 actual = _merge_messages(messages)665 assert expected == actual666667 # Test tool message case668 messages = [669 ToolMessage("buz output", tool_call_id="1"), # type: ignore[misc]670 ToolMessage( # type: ignore[misc]671 content=[672 {"type": "tool_result", "content": "blah output", "tool_use_id": "2"},673 ],674 tool_call_id="2",675 ),676 ]677 expected = [678 HumanMessage( # type: ignore[misc]679 [680 {681 "type": "tool_result",682 "content": "buz output",683 "tool_use_id": "1",684 "is_error": False,685 },686 {"type": "tool_result", "content": "blah output", "tool_use_id": "2"},687 ],688 ),689 ]690 actual = _merge_messages(messages)691 assert expected == actual692693694def test__merge_messages_coalesces_adjacent_system_messages() -> None:695 """Test adjacent system messages are merged."""696 messages = [697 SystemMessage("bar"), # type: ignore[misc]698 SystemMessage("baz"), # type: ignore[misc]699 SystemMessage( # type: ignore[misc]700 [701 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},702 ],703 ),704 HumanMessage("hi"), # type: ignore[misc]705 ]706 expected = [707 SystemMessage( # type: ignore[misc]708 [709 {"type": "text", "text": "bar"},710 {"type": "text", "text": "baz"},711 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},712 ],713 ),714 HumanMessage("hi"), # type: ignore[misc]715 ]716 assert _merge_messages(messages) == expected717718719def test__merge_messages_mutation() -> None:720 original_messages = [721 HumanMessage([{"type": "text", "text": "bar"}]), # type: ignore[misc]722 HumanMessage("next thing"), # type: ignore[misc]723 ]724 messages = [725 HumanMessage([{"type": "text", "text": "bar"}]), # type: ignore[misc]726 HumanMessage("next thing"), # type: ignore[misc]727 ]728 expected = [729 HumanMessage( # type: ignore[misc]730 [{"type": "text", "text": "bar"}, {"type": "text", "text": "next thing"}],731 ),732 ]733 actual = _merge_messages(messages)734 assert expected == actual735 assert messages == original_messages736737738def test__merge_messages_tool_message_cache_control() -> None:739 """Test that cache_control is hoisted from content blocks to tool_result level."""740 # Test with cache_control in content block741 messages = [742 ToolMessage(743 content=[744 {745 "type": "text",746 "text": "tool output",747 "cache_control": {"type": "ephemeral"},748 }749 ],750 tool_call_id="1",751 )752 ]753 original_messages = [copy.deepcopy(m) for m in messages]754 expected = [755 HumanMessage(756 [757 {758 "type": "tool_result",759 "content": [{"type": "text", "text": "tool output"}],760 "tool_use_id": "1",761 "is_error": False,762 "cache_control": {"type": "ephemeral"},763 }764 ]765 )766 ]767 actual = _merge_messages(messages)768 assert expected == actual769 # Verify no mutation770 assert messages == original_messages771772 # Test with multiple content blocks, cache_control on last one773 messages = [774 ToolMessage(775 content=[776 {"type": "text", "text": "first output"},777 {778 "type": "text",779 "text": "second output",780 "cache_control": {"type": "ephemeral"},781 },782 ],783 tool_call_id="2",784 )785 ]786 expected = [787 HumanMessage(788 [789 {790 "type": "tool_result",791 "content": [792 {"type": "text", "text": "first output"},793 {"type": "text", "text": "second output"},794 ],795 "tool_use_id": "2",796 "is_error": False,797 "cache_control": {"type": "ephemeral"},798 }799 ]800 )801 ]802 actual = _merge_messages(messages)803 assert expected == actual804805 # Test without cache_control806 messages = [ToolMessage(content="simple output", tool_call_id="3")]807 expected = [808 HumanMessage(809 [810 {811 "type": "tool_result",812 "content": "simple output",813 "tool_use_id": "3",814 "is_error": False,815 }816 ]817 )818 ]819 actual = _merge_messages(messages)820 assert expected == actual821822823def test__format_image() -> None:824 url = "dummyimage.com/600x400/000/fff"825 with pytest.raises(ValueError):826 _format_image(url)827828829@pytest.fixture830def pydantic() -> type[BaseModel]:831 class dummy_function(BaseModel): # noqa: N801832 """Dummy function."""833834 arg1: int = Field(..., description="foo")835 arg2: Literal["bar", "baz"] = Field(..., description="one of 'bar', 'baz'")836837 return dummy_function838839840@pytest.fixture841def function() -> Callable:842 def dummy_function(arg1: int, arg2: Literal["bar", "baz"]) -> None:843 """Dummy function.844845 Args:846 arg1: foo847 arg2: one of 'bar', 'baz'848849 """850851 return dummy_function852853854@pytest.fixture855def dummy_tool() -> BaseTool:856 class Schema(BaseModel):857 arg1: int = Field(..., description="foo")858 arg2: Literal["bar", "baz"] = Field(..., description="one of 'bar', 'baz'")859860 class DummyFunction(BaseTool): # type: ignore[override]861 args_schema: type[BaseModel] = Schema862 name: str = "dummy_function"863 description: str = "Dummy function."864865 def _run(self, *args: Any, **kwargs: Any) -> Any:866 pass867868 return DummyFunction()869870871@pytest.fixture872def json_schema() -> dict:873 return {874 "title": "dummy_function",875 "description": "Dummy function.",876 "type": "object",877 "properties": {878 "arg1": {"description": "foo", "type": "integer"},879 "arg2": {880 "description": "one of 'bar', 'baz'",881 "enum": ["bar", "baz"],882 "type": "string",883 },884 },885 "required": ["arg1", "arg2"],886 }887888889@pytest.fixture890def openai_function() -> dict:891 return {892 "name": "dummy_function",893 "description": "Dummy function.",894 "parameters": {895 "type": "object",896 "properties": {897 "arg1": {"description": "foo", "type": "integer"},898 "arg2": {899 "description": "one of 'bar', 'baz'",900 "enum": ["bar", "baz"],901 "type": "string",902 },903 },904 "required": ["arg1", "arg2"],905 },906 }907908909def test_convert_to_anthropic_tool(910 pydantic: type[BaseModel],911 function: Callable,912 dummy_tool: BaseTool,913 json_schema: dict,914 openai_function: dict,915) -> None:916 expected = {917 "name": "dummy_function",918 "description": "Dummy function.",919 "input_schema": {920 "type": "object",921 "properties": {922 "arg1": {"description": "foo", "type": "integer"},923 "arg2": {924 "description": "one of 'bar', 'baz'",925 "enum": ["bar", "baz"],926 "type": "string",927 },928 },929 "required": ["arg1", "arg2"],930 },931 }932933 for fn in (pydantic, function, dummy_tool, json_schema, expected, openai_function):934 actual = convert_to_anthropic_tool(fn)935 assert actual == expected936937938def test__format_messages_with_tool_calls() -> None:939 system = SystemMessage("fuzz") # type: ignore[misc]940 human = HumanMessage("foo") # type: ignore[misc]941 ai = AIMessage(942 "", # with empty string943 tool_calls=[{"name": "bar", "id": "1", "args": {"baz": "buzz"}}],944 )945 ai2 = AIMessage(946 [], # with empty list947 tool_calls=[{"name": "bar", "id": "2", "args": {"baz": "buzz"}}],948 )949 tool = ToolMessage(950 "blurb",951 tool_call_id="1",952 )953 tool_image_url = ToolMessage(954 [{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,...."}}],955 tool_call_id="2",956 )957 tool_image = ToolMessage(958 [959 {960 "type": "image",961 "source": {962 "data": "....",963 "type": "base64",964 "media_type": "image/jpeg",965 },966 },967 ],968 tool_call_id="3",969 )970 messages = [system, human, ai, tool, ai2, tool_image_url, tool_image]971 expected = (972 "fuzz",973 [974 {"role": "user", "content": "foo"},975 {976 "role": "assistant",977 "content": [978 {979 "type": "tool_use",980 "name": "bar",981 "id": "1",982 "input": {"baz": "buzz"},983 },984 ],985 },986 {987 "role": "user",988 "content": [989 {990 "type": "tool_result",991 "content": "blurb",992 "tool_use_id": "1",993 "is_error": False,994 },995 ],996 },997 {998 "role": "assistant",999 "content": [1000 {1001 "type": "tool_use",1002 "name": "bar",1003 "id": "2",1004 "input": {"baz": "buzz"},1005 },1006 ],1007 },1008 {1009 "role": "user",1010 "content": [1011 {1012 "type": "tool_result",1013 "content": [1014 {1015 "type": "image",1016 "source": {1017 "data": "....",1018 "type": "base64",1019 "media_type": "image/jpeg",1020 },1021 },1022 ],1023 "tool_use_id": "2",1024 "is_error": False,1025 },1026 {1027 "type": "tool_result",1028 "content": [1029 {1030 "type": "image",1031 "source": {1032 "data": "....",1033 "type": "base64",1034 "media_type": "image/jpeg",1035 },1036 },1037 ],1038 "tool_use_id": "3",1039 "is_error": False,1040 },1041 ],1042 },1043 ],1044 )1045 actual = _format_messages(messages, model=MODEL_NAME)1046 assert expected == actual10471048 # Check handling of empty AIMessage1049 empty_contents: list[str | list[str | dict[str, Any]]] = ["", []]1050 for empty_content in empty_contents:1051 ## Permit message in final position1052 _, anthropic_messages = _format_messages(1053 [human, AIMessage(empty_content)], model=MODEL_NAME1054 )1055 expected_messages = [1056 {"role": "user", "content": "foo"},1057 {"role": "assistant", "content": empty_content},1058 ]1059 assert expected_messages == anthropic_messages10601061 ## Remove message otherwise1062 _, anthropic_messages = _format_messages(1063 [human, AIMessage(empty_content), human], model=MODEL_NAME1064 )1065 expected_messages = [1066 {"role": "user", "content": "foo"},1067 {"role": "user", "content": "foo"},1068 ]1069 assert expected_messages == anthropic_messages10701071 actual = _format_messages(1072 [system, human, ai, tool, AIMessage(empty_content), human], model=MODEL_NAME1073 )1074 assert actual[0] == "fuzz"1075 assert [message["role"] for message in actual[1]] == [1076 "user",1077 "assistant",1078 "user",1079 "user",1080 ]108110821083def test__normalize_tool_call_id() -> None:1084 # Already-valid IDs (including native Anthropic and OpenAI styles) pass1085 # through unchanged.1086 for valid in ("1", "toolu_01abcDEF-_", "call_Ao02pnFYXD6GN1yzc0uXPsvF"):1087 assert _normalize_tool_call_id(valid) == valid10881089 # Empty and None IDs pass through so a malformed request surfaces a clear1090 # error from Anthropic rather than a synthesized ID.1091 assert _normalize_tool_call_id("") == ""1092 assert _normalize_tool_call_id(None) is None10931094 # Foreign IDs with characters Anthropic rejects (e.g. Fireworks/Kimi's1095 # `functions.write_todos:0`) are rewritten to a compatible form.1096 invalid = "functions.write_todos:0"1097 normalized = _normalize_tool_call_id(invalid)1098 assert normalized is not None1099 assert normalized != invalid1100 assert _TOOL_CALL_ID_PATTERN.match(normalized)11011102 # Deterministic + idempotent: same input always maps to the same output.1103 assert _normalize_tool_call_id(invalid) == normalized1104 assert _normalize_tool_call_id(normalized) == normalized11051106 # Distinct invalid IDs map to distinct replacements (no collision that1107 # would break multi-tool turns).1108 other = _normalize_tool_call_id("functions.read_file:1")1109 assert other != normalized111011111112def test__format_messages_normalizes_cross_provider_tool_call_ids() -> None:1113 """A `tool_use.id` and its paired `tool_use_id` must normalize identically.11141115 Reproduces the Fireworks/Kimi -> Anthropic 400 from replaying a thread whose1116 tool-call IDs were minted by another provider.1117 """1118 bad_id = "functions.write_todos:0"1119 ai = AIMessage(1120 "",1121 tool_calls=[{"name": "write_todos", "id": bad_id, "args": {"todos": []}}],1122 )1123 tool = ToolMessage("done", tool_call_id=bad_id)11241125 _, formatted = _format_messages([HumanMessage("hi"), ai, tool], model=MODEL_NAME)11261127 tool_use = formatted[1]["content"][0]1128 tool_result = formatted[2]["content"][0]1129 assert tool_use["type"] == "tool_use"1130 assert tool_result["type"] == "tool_result"11311132 # The rewritten IDs are valid and still reference each other.1133 assert _TOOL_CALL_ID_PATTERN.match(tool_use["id"])1134 assert tool_use["id"] == tool_result["tool_use_id"]1135 assert tool_use["id"] == _normalize_tool_call_id(bad_id)113611371138def test__format_messages_normalizes_prestructured_tool_result_id() -> None:1139 """A `ToolMessage` whose content is already `tool_result` blocks is covered.11401141 This shape bypasses the `tool_call_id` normalization in `_merge_messages` and1142 flows through the `tool_result` content branch, so its `tool_use_id` must1143 still be normalized to match the paired `tool_use.id`.1144 """1145 bad_id = "functions.write_todos:0"1146 ai = AIMessage(1147 "",1148 tool_calls=[{"name": "write_todos", "id": bad_id, "args": {"todos": []}}],1149 )1150 tool = ToolMessage(1151 [{"type": "tool_result", "tool_use_id": bad_id, "content": "done"}],1152 tool_call_id=bad_id,1153 )11541155 _, formatted = _format_messages([HumanMessage("hi"), ai, tool], model=MODEL_NAME)11561157 tool_use = formatted[1]["content"][0]1158 tool_result = formatted[2]["content"][0]1159 assert tool_use["id"] == tool_result["tool_use_id"]1160 assert tool_use["id"] == _normalize_tool_call_id(bad_id)116111621163def test__format_messages_normalizes_inline_tool_use_block() -> None:1164 """An invalid ID on an inline `tool_use` content block is normalized.11651166 Covers the v1-compat destination where tool calls are stored as content1167 blocks rather than the `tool_calls` attribute, paired with a `ToolMessage`.1168 """1169 bad_id = "functions.search:2"1170 ai = AIMessage(1171 [{"type": "tool_use", "name": "search", "id": bad_id, "input": {"q": "x"}}],1172 )1173 tool = ToolMessage("result", tool_call_id=bad_id)11741175 _, formatted = _format_messages([HumanMessage("hi"), ai, tool], model=MODEL_NAME)11761177 tool_use = formatted[1]["content"][0]1178 tool_result = formatted[2]["content"][0]1179 assert _TOOL_CALL_ID_PATTERN.match(tool_use["id"])1180 assert tool_use["id"] == tool_result["tool_use_id"]118111821183def test__format_messages_dedupes_overlapping_normalized_tool_use() -> None:1184 """An invalid ID shared by a `tool_use` block and `tool_calls` yields one block.11851186 Guards the dedup branch: `tool_use_ids` are normalized, so the comparison1187 against the (also normalized) tool-call ID must not re-emit a duplicate block.1188 """1189 bad_id = "functions.write_todos:0"1190 ai = AIMessage(1191 [{"type": "tool_use", "name": "write_todos", "id": bad_id, "input": {"a": 1}}],1192 tool_calls=[{"name": "write_todos", "id": bad_id, "args": {"a": 1}}],1193 )11941195 _, formatted = _format_messages([HumanMessage("hi"), ai], model=MODEL_NAME)11961197 tool_use_blocks = [b for b in formatted[1]["content"] if b["type"] == "tool_use"]1198 assert len(tool_use_blocks) == 11199 assert _TOOL_CALL_ID_PATTERN.match(tool_use_blocks[0]["id"])120012011202def test__format_messages_normalizes_distinct_ids_independently() -> None:1203 """Multiple distinct invalid IDs in one turn stay distinct and correctly paired."""1204 id_a = "functions.write_todos:0"1205 id_b = "functions.read_file:1"1206 ai = AIMessage(1207 "",1208 tool_calls=[1209 {"name": "write_todos", "id": id_a, "args": {}},1210 {"name": "read_file", "id": id_b, "args": {}},1211 ],1212 )1213 tool_a = ToolMessage("a", tool_call_id=id_a)1214 tool_b = ToolMessage("b", tool_call_id=id_b)12151216 _, formatted = _format_messages(1217 [HumanMessage("hi"), ai, tool_a, tool_b], model=MODEL_NAME1218 )12191220 tool_uses = formatted[1]["content"]1221 results = formatted[2]["content"]1222 assert tool_uses[0]["id"] == _normalize_tool_call_id(id_a)1223 assert tool_uses[1]["id"] == _normalize_tool_call_id(id_b)1224 assert tool_uses[0]["id"] != tool_uses[1]["id"]1225 # Each result still pairs with its own tool_use.1226 assert {r["tool_use_id"] for r in results} == {1227 tool_uses[0]["id"],1228 tool_uses[1]["id"],1229 }123012311232def test__format_tool_use_block() -> None:1233 # Test we correctly format tool_use blocks when there is no corresponding tool_call.1234 message = AIMessage(1235 [1236 {1237 "type": "tool_use",1238 "name": "foo_1",1239 "id": "1",1240 "input": {"bar_1": "baz_1"},1241 },1242 {1243 "type": "tool_use",1244 "name": "foo_2",1245 "id": "2",1246 "input": {},1247 "partial_json": '{"bar_2": "baz_2"}',1248 "index": 1,1249 },1250 ]1251 )1252 result = _format_messages([message], model=MODEL_NAME)1253 expected = {1254 "role": "assistant",1255 "content": [1256 {1257 "type": "tool_use",1258 "name": "foo_1",1259 "id": "1",1260 "input": {"bar_1": "baz_1"},1261 },1262 {1263 "type": "tool_use",1264 "name": "foo_2",1265 "id": "2",1266 "input": {"bar_2": "baz_2"},1267 },1268 ],1269 }1270 assert result == (None, [expected])127112721273def test__format_messages_with_str_content_and_tool_calls() -> None:1274 system = SystemMessage("fuzz") # type: ignore[misc]1275 human = HumanMessage("foo") # type: ignore[misc]1276 # If content and tool_calls are specified and content is a string, then both are1277 # included with content first.1278 ai = AIMessage( # type: ignore[misc]1279 "thought",1280 tool_calls=[{"name": "bar", "id": "1", "args": {"baz": "buzz"}}],1281 )1282 tool = ToolMessage("blurb", tool_call_id="1") # type: ignore[misc]1283 messages = [system, human, ai, tool]1284 expected = (1285 "fuzz",1286 [1287 {"role": "user", "content": "foo"},1288 {1289 "role": "assistant",1290 "content": [1291 {"type": "text", "text": "thought"},1292 {1293 "type": "tool_use",1294 "name": "bar",1295 "id": "1",1296 "input": {"baz": "buzz"},1297 },1298 ],1299 },1300 {1301 "role": "user",1302 "content": [1303 {1304 "type": "tool_result",1305 "content": "blurb",1306 "tool_use_id": "1",1307 "is_error": False,1308 },1309 ],1310 },1311 ],1312 )1313 actual = _format_messages(messages, model=MODEL_NAME)1314 assert expected == actual131513161317def test__format_messages_with_list_content_and_tool_calls() -> None:1318 system = SystemMessage("fuzz") # type: ignore[misc]1319 human = HumanMessage("foo") # type: ignore[misc]1320 ai = AIMessage( # type: ignore[misc]1321 [{"type": "text", "text": "thought"}],1322 tool_calls=[{"name": "bar", "id": "1", "args": {"baz": "buzz"}}],1323 )1324 tool = ToolMessage( # type: ignore[misc]1325 "blurb",1326 tool_call_id="1",1327 )1328 messages = [system, human, ai, tool]1329 expected = (1330 "fuzz",1331 [1332 {"role": "user", "content": "foo"},1333 {1334 "role": "assistant",1335 "content": [1336 {"type": "text", "text": "thought"},1337 {1338 "type": "tool_use",1339 "name": "bar",1340 "id": "1",1341 "input": {"baz": "buzz"},1342 },1343 ],1344 },1345 {1346 "role": "user",1347 "content": [1348 {1349 "type": "tool_result",1350 "content": "blurb",1351 "tool_use_id": "1",1352 "is_error": False,1353 },1354 ],1355 },1356 ],1357 )1358 actual = _format_messages(messages, model=MODEL_NAME)1359 assert expected == actual136013611362def test__format_messages_with_tool_use_blocks_and_tool_calls() -> None:1363 """Show that tool_calls are preferred to tool_use blocks when both have same id."""1364 system = SystemMessage("fuzz") # type: ignore[misc]1365 human = HumanMessage("foo") # type: ignore[misc]1366 # NOTE: tool_use block in contents and tool_calls have different arguments.1367 ai = AIMessage( # type: ignore[misc]1368 [1369 {"type": "text", "text": "thought"},1370 {1371 "type": "tool_use",1372 "name": "bar",1373 "id": "1",1374 "input": {"baz": "NOT_BUZZ"},1375 },1376 ],1377 tool_calls=[{"name": "bar", "id": "1", "args": {"baz": "BUZZ"}}],1378 )1379 tool = ToolMessage("blurb", tool_call_id="1") # type: ignore[misc]1380 messages = [system, human, ai, tool]1381 expected = (1382 "fuzz",1383 [1384 {"role": "user", "content": "foo"},1385 {1386 "role": "assistant",1387 "content": [1388 {"type": "text", "text": "thought"},1389 {1390 "type": "tool_use",1391 "name": "bar",1392 "id": "1",1393 "input": {"baz": "BUZZ"}, # tool_calls value preferred.1394 },1395 ],1396 },1397 {1398 "role": "user",1399 "content": [1400 {1401 "type": "tool_result",1402 "content": "blurb",1403 "tool_use_id": "1",1404 "is_error": False,1405 },1406 ],1407 },1408 ],1409 )1410 actual = _format_messages(messages, model=MODEL_NAME)1411 assert expected == actual141214131414def test__format_messages_with_cache_control() -> None:1415 messages = [1416 SystemMessage(1417 [1418 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1419 ],1420 ),1421 HumanMessage(1422 [1423 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1424 {1425 "type": "text",1426 "text": "foo",1427 },1428 ],1429 ),1430 ]1431 expected_system = [1432 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1433 ]1434 expected_messages = [1435 {1436 "role": "user",1437 "content": [1438 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1439 {"type": "text", "text": "foo"},1440 ],1441 },1442 ]1443 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1444 assert expected_system == actual_system1445 assert expected_messages == actual_messages14461447 # Test standard multi-modal format (v0)1448 messages = [1449 HumanMessage(1450 [1451 {1452 "type": "text",1453 "text": "Summarize this document:",1454 },1455 {1456 "type": "file",1457 "source_type": "base64",1458 "mime_type": "application/pdf",1459 "data": "<base64 data>",1460 "cache_control": {"type": "ephemeral"},1461 },1462 ],1463 ),1464 ]1465 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1466 assert actual_system is None1467 expected_messages = [1468 {1469 "role": "user",1470 "content": [1471 {1472 "type": "text",1473 "text": "Summarize this document:",1474 },1475 {1476 "type": "document",1477 "source": {1478 "type": "base64",1479 "media_type": "application/pdf",1480 "data": "<base64 data>",1481 },1482 "cache_control": {"type": "ephemeral"},1483 },1484 ],1485 },1486 ]1487 assert actual_messages == expected_messages14881489 # Test standard multi-modal format (v1)1490 messages = [1491 HumanMessage(1492 [1493 {1494 "type": "text",1495 "text": "Summarize this document:",1496 },1497 {1498 "type": "file",1499 "mime_type": "application/pdf",1500 "base64": "<base64 data>",1501 "extras": {"cache_control": {"type": "ephemeral"}},1502 },1503 ],1504 ),1505 ]1506 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1507 assert actual_system is None1508 expected_messages = [1509 {1510 "role": "user",1511 "content": [1512 {1513 "type": "text",1514 "text": "Summarize this document:",1515 },1516 {1517 "type": "document",1518 "source": {1519 "type": "base64",1520 "media_type": "application/pdf",1521 "data": "<base64 data>",1522 },1523 "cache_control": {"type": "ephemeral"},1524 },1525 ],1526 },1527 ]1528 assert actual_messages == expected_messages15291530 # Test standard multi-modal format (v1, unpacked extras)1531 messages = [1532 HumanMessage(1533 [1534 {1535 "type": "text",1536 "text": "Summarize this document:",1537 },1538 {1539 "type": "file",1540 "mime_type": "application/pdf",1541 "base64": "<base64 data>",1542 "cache_control": {"type": "ephemeral"},1543 },1544 ],1545 ),1546 ]1547 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1548 assert actual_system is None1549 expected_messages = [1550 {1551 "role": "user",1552 "content": [1553 {1554 "type": "text",1555 "text": "Summarize this document:",1556 },1557 {1558 "type": "document",1559 "source": {1560 "type": "base64",1561 "media_type": "application/pdf",1562 "data": "<base64 data>",1563 },1564 "cache_control": {"type": "ephemeral"},1565 },1566 ],1567 },1568 ]1569 assert actual_messages == expected_messages15701571 # Also test file inputs1572 ## Images1573 for block in [1574 # v11575 {1576 "type": "image",1577 "file_id": "abc123",1578 },1579 # v01580 {1581 "type": "image",1582 "source_type": "id",1583 "id": "abc123",1584 },1585 ]:1586 messages = [1587 HumanMessage(1588 [1589 {1590 "type": "text",1591 "text": "Summarize this image:",1592 },1593 block,1594 ],1595 ),1596 ]1597 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1598 assert actual_system is None1599 expected_messages = [1600 {1601 "role": "user",1602 "content": [1603 {1604 "type": "text",1605 "text": "Summarize this image:",1606 },1607 {1608 "type": "image",1609 "source": {1610 "type": "file",1611 "file_id": "abc123",1612 },1613 },1614 ],1615 },1616 ]1617 assert actual_messages == expected_messages16181619 ## Documents1620 for block in [1621 # v11622 {1623 "type": "file",1624 "file_id": "abc123",1625 },1626 # v01627 {1628 "type": "file",1629 "source_type": "id",1630 "id": "abc123",1631 },1632 ]:1633 messages = [1634 HumanMessage(1635 [1636 {1637 "type": "text",1638 "text": "Summarize this document:",1639 },1640 block,1641 ],1642 ),1643 ]1644 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1645 assert actual_system is None1646 expected_messages = [1647 {1648 "role": "user",1649 "content": [1650 {1651 "type": "text",1652 "text": "Summarize this document:",1653 },1654 {1655 "type": "document",1656 "source": {1657 "type": "file",1658 "file_id": "abc123",1659 },1660 },1661 ],1662 },1663 ]1664 assert actual_messages == expected_messages166516661667def test__format_messages_with_citations() -> None:1668 input_messages = [1669 HumanMessage(1670 content=[1671 {1672 "type": "file",1673 "source_type": "text",1674 "text": "The grass is green. The sky is blue.",1675 "mime_type": "text/plain",1676 "citations": {"enabled": True},1677 },1678 {"type": "text", "text": "What color is the grass and sky?"},1679 ],1680 ),1681 ]1682 expected_messages = [1683 {1684 "role": "user",1685 "content": [1686 {1687 "type": "document",1688 "source": {1689 "type": "text",1690 "media_type": "text/plain",1691 "data": "The grass is green. The sky is blue.",1692 },1693 "citations": {"enabled": True},1694 },1695 {"type": "text", "text": "What color is the grass and sky?"},1696 ],1697 },1698 ]1699 actual_system, actual_messages = _format_messages(input_messages, model=MODEL_NAME)1700 assert actual_system is None1701 assert actual_messages == expected_messages170217031704def test__format_messages_openai_image_format() -> None:1705 message = HumanMessage(1706 content=[1707 {1708 "type": "text",1709 "text": "Can you highlight the differences between these two images?",1710 },1711 {1712 "type": "image_url",1713 "image_url": {"url": "data:image/jpeg;base64,<base64 data>"},1714 },1715 {1716 "type": "image_url",1717 "image_url": {"url": "https://<image url>"},1718 },1719 ],1720 )1721 actual_system, actual_messages = _format_messages([message], model=MODEL_NAME)1722 assert actual_system is None1723 expected_messages = [1724 {1725 "role": "user",1726 "content": [1727 {1728 "type": "text",1729 "text": (1730 "Can you highlight the differences between these two images?"1731 ),1732 },1733 {1734 "type": "image",1735 "source": {1736 "type": "base64",1737 "media_type": "image/jpeg",1738 "data": "<base64 data>",1739 },1740 },1741 {1742 "type": "image",1743 "source": {1744 "type": "url",1745 "url": "https://<image url>",1746 },1747 },1748 ],1749 },1750 ]1751 assert actual_messages == expected_messages175217531754def test__format_messages_with_multiple_system() -> None:1755 """Test a trailing run of system messages is sent in place."""1756 messages = [1757 HumanMessage("baz"),1758 SystemMessage("bar"),1759 SystemMessage("baz"),1760 SystemMessage(1761 [1762 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1763 ],1764 ),1765 ]1766 expected_messages = [1767 {"role": "user", "content": "baz"},1768 {1769 "role": "system",1770 "content": [1771 {"type": "text", "text": "bar"},1772 {"type": "text", "text": "baz"},1773 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1774 ],1775 },1776 ]1777 actual_system, actual_messages = _format_messages(1778 messages,1779 model=MID_CONVERSATION_SYSTEM_MODEL,1780 mid_conversation_system_messages=True,1781 )1782 assert actual_system is None1783 assert expected_messages == actual_messages178417851786def test__format_messages_with_multiple_leading_system() -> None:1787 """A leading run of system messages is hoisted as one block array."""1788 messages = [1789 SystemMessage("bar"),1790 SystemMessage("baz"),1791 SystemMessage(1792 [1793 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1794 ],1795 ),1796 HumanMessage("baz"),1797 ]1798 expected_system = [1799 {"type": "text", "text": "bar"},1800 {"type": "text", "text": "baz"},1801 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1802 ]1803 expected_messages = [{"role": "user", "content": "baz"}]1804 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1805 assert expected_system == actual_system1806 assert expected_messages == actual_messages180718081809def test__format_messages_system_v1_content_blocks_drop_id() -> None:1810 """System text blocks from `create_text_block` must not leak the `id` field.18111812 See https://github.com/langchain-ai/langchain/issues/391001813 """1814 messages = [1815 SystemMessage(content_blocks=[create_text_block("You are helpful.")]),1816 HumanMessage("hi"),1817 ]1818 actual_system, actual_messages = _format_messages(messages, model=MODEL_NAME)1819 assert actual_system == [{"type": "text", "text": "You are helpful."}]1820 assert actual_messages == [{"role": "user", "content": "hi"}]182118221823def test__format_messages_system_text_block_preserves_supported_fields() -> None:1824 """Sanitizing system text blocks keeps Anthropic-supported fields."""1825 messages = [1826 SystemMessage(1827 [1828 {1829 "type": "text",1830 "text": "foo",1831 "id": "lc_abc123",1832 "cache_control": {"type": "ephemeral"},1833 },1834 ],1835 ),1836 HumanMessage("hi"),1837 ]1838 actual_system, _ = _format_messages(messages, model=MODEL_NAME)1839 assert actual_system == [1840 {"type": "text", "text": "foo", "cache_control": {"type": "ephemeral"}},1841 ]184218431844_HOIST_WARNING = "A non-leading `SystemMessage` was moved"184518461847def test__format_messages_leading_system_string_content_unchanged() -> None:1848 """Test leading string system content stays a string."""1849 messages = [1850 SystemMessage("You are a code reviewer."),1851 HumanMessage("Review foo()"),1852 ]1853 actual_system, actual_messages = _format_messages(1854 messages,1855 model=MID_CONVERSATION_SYSTEM_MODEL,1856 mid_conversation_system_messages=True,1857 )1858 assert actual_system == "You are a code reviewer."1859 assert actual_messages == [{"role": "user", "content": "Review foo()"}]186018611862def test__format_messages_trailing_system_sent_in_place() -> None:1863 """A trailing system message on a supporting model is sent at its position."""1864 messages = [1865 SystemMessage("You are a code reviewer."),1866 HumanMessage("Review foo()"),1867 AIMessage("Looks fine."),1868 HumanMessage("Review bar()"),1869 SystemMessage("Every suggestion must include type annotations."),1870 ]1871 actual_system, actual_messages = _format_messages(1872 messages,1873 model=MID_CONVERSATION_SYSTEM_MODEL,1874 mid_conversation_system_messages=True,1875 )1876 assert actual_system == "You are a code reviewer."1877 assert actual_messages == [1878 {"role": "user", "content": "Review foo()"},1879 {"role": "assistant", "content": "Looks fine."},1880 {"role": "user", "content": "Review bar()"},1881 {1882 "role": "system",1883 "content": "Every suggestion must include type annotations.",1884 },1885 ]188618871888def test__format_messages_non_leading_system_only_run_sent_in_place() -> None:1889 """With nothing hoisted, a legal non-leading run still goes in place."""1890 messages = [1891 HumanMessage("Review foo()"),1892 SystemMessage("Be concise."),1893 ]1894 actual_system, actual_messages = _format_messages(1895 messages,1896 model=MID_CONVERSATION_SYSTEM_MODEL,1897 mid_conversation_system_messages=True,1898 )1899 assert actual_system is None1900 assert actual_messages == [1901 {"role": "user", "content": "Review foo()"},1902 {"role": "system", "content": "Be concise."},1903 ]190419051906def test__format_messages_system_between_user_and_ai_sent_in_place() -> None:1907 """A system message followed by an assistant turn is a legal position."""1908 messages = [1909 HumanMessage("Review foo()"),1910 SystemMessage("Be concise."),1911 AIMessage("Looks fine."),1912 ]1913 actual_system, actual_messages = _format_messages(1914 messages,1915 model=MID_CONVERSATION_SYSTEM_MODEL,1916 mid_conversation_system_messages=True,1917 )1918 assert actual_system is None1919 assert actual_messages == [1920 {"role": "user", "content": "Review foo()"},1921 {"role": "system", "content": "Be concise."},1922 {"role": "assistant", "content": "Looks fine."},1923 ]192419251926def test__format_messages_system_after_tool_message_sent_in_place() -> None:1927 """Test a system message follows the folded tool-result turn."""1928 ai = AIMessage(1929 "",1930 tool_calls=[{"name": "search", "args": {"q": "foo"}, "id": "toolu_1"}],1931 )1932 messages = [1933 HumanMessage("Review foo()"),1934 ai,1935 ToolMessage("results", tool_call_id="toolu_1"),1936 SystemMessage("Be concise."),1937 ]1938 actual_system, actual_messages = _format_messages(1939 messages,1940 model=MID_CONVERSATION_SYSTEM_MODEL,1941 mid_conversation_system_messages=True,1942 )1943 assert actual_system is None1944 assert [message["role"] for message in actual_messages] == [1945 "user",1946 "assistant",1947 "user",1948 "system",1949 ]1950 assert actual_messages[-1]["content"] == "Be concise."195119521953@pytest.mark.parametrize(1954 "block_type",1955 [1956 "web_search_tool_result",1957 "code_execution_tool_result",1958 "mcp_tool_result",1959 "future_server_tool_result",1960 ],1961)1962def test__format_messages_system_after_server_tool_result_sent_in_place(1963 block_type: str,1964) -> None:1965 """An assistant turn ending in a server tool result is a legal predecessor."""1966 block = {1967 "type": block_type,1968 "tool_use_id": "srvtoolu_1",1969 "content": [{"type": "text", "text": "results"}],1970 }1971 ai = AIMessage([block], response_metadata={"model_provider": "anthropic"})1972 messages = [1973 HumanMessage("Review foo()"),1974 ai,1975 SystemMessage("Be concise."),1976 ]1977 actual_system, actual_messages = _format_messages(1978 messages,1979 model=MID_CONVERSATION_SYSTEM_MODEL,1980 mid_conversation_system_messages=True,1981 )1982 assert actual_system is None1983 assert [message["role"] for message in actual_messages] == [1984 "user",1985 "assistant",1986 "system",1987 ]1988 # Forward compatibility: an unrecognized server tool result block reaches the1989 # wire untouched, which is what makes it a legal predecessor in the first place.1990 assert actual_messages[1]["content"] == [block]199119921993@pytest.mark.parametrize("output_version", ["v0", "v1"])1994def test__format_messages_server_tool_use_preserves_caller(output_version: str) -> None:1995 """Nested server tool calls keep `caller` so the API can link them to parents."""1996 caller = {"type": "code_execution_20260120", "tool_id": "srvtoolu_ce"}1997 blocks: list[Any] = [1998 {1999 "type": "server_tool_use",2000 "id": "srvtoolu_ce",
Findings
✓ No findings reported for this file.