Home / File/ test_chat_models.py — langchain Source File

test_chat_models.py — langchain Source File

Architecture documentation for test_chat_models.py, a python file in the langchain codebase. 17 imports, 0 dependents.

File python CoreAbstractions MessageSchema 17 imports 74 functions 10 classes

Entity Profile

Dependency Diagram

graph LR
  f03d043d_9970_5652_cac7_6bd06018b017["test_chat_models.py"]
  9e98f0a7_ec6e_708f_4f1b_e9428b316e1c["os"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 9e98f0a7_ec6e_708f_4f1b_e9428b316e1c
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  525a7d6f_f455_56e3_854a_c8a7da4a1417["unittest.mock"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 525a7d6f_f455_56e3_854a_c8a7da4a1417
  ad604472_c022_b119_aeba_5ff8893361cd["anthropic"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> ad604472_c022_b119_aeba_5ff8893361cd
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  394795db_0c62_c169_63f8_dfeb39db09a7["anthropic.types"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 394795db_0c62_c169_63f8_dfeb39db09a7
  9eb3be64_a334_606b_b4bd_24e50a8c430d["blockbuster"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 9eb3be64_a334_606b_b4bd_24e50a8c430d
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> d758344f_537f_649e_f467_b9d7442e86df
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 43d88577_548b_2248_b01b_7987bae85dcc
  59d7001f_fb28_1819_31fc_7fb0380a8b32["langchain_core.tracers.base"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 59d7001f_fb28_1819_31fc_7fb0380a8b32
  17e2fb09_6b0f_338f_1319_77bc43602969["langchain_core.tracers.schemas"]
  f03d043d_9970_5652_cac7_6bd06018b017 --> 17e2fb09_6b0f_338f_1319_77bc43602969
  style f03d043d_9970_5652_cac7_6bd06018b017 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test chat model integration."""

from __future__ import annotations

import os
from collections.abc import Callable
from typing import Any, Literal, cast
from unittest.mock import MagicMock, patch

import anthropic
import pytest
from anthropic.types import Message, TextBlock, Usage
from blockbuster import blockbuster_ctx
from langchain_core.exceptions import ContextOverflowError
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
from langchain_core.runnables import RunnableBinding
from langchain_core.tools import BaseTool
from langchain_core.tracers.base import BaseTracer
from langchain_core.tracers.schemas import Run
from pydantic import BaseModel, Field, SecretStr, ValidationError
from pytest import CaptureFixture, MonkeyPatch

from langchain_anthropic import ChatAnthropic
from langchain_anthropic.chat_models import (
    _create_usage_metadata,
    _format_image,
    _format_messages,
    _is_builtin_tool,
    _merge_messages,
    convert_to_anthropic_tool,
)

os.environ["ANTHROPIC_API_KEY"] = "foo"

MODEL_NAME = "claude-sonnet-4-5-20250929"


def test_initialization() -> None:
    """Test chat model initialization."""
    for model in [
        ChatAnthropic(model_name=MODEL_NAME, api_key="xyz", timeout=2),  # type: ignore[arg-type, call-arg]
        ChatAnthropic(  # type: ignore[call-arg, call-arg, call-arg]
            model=MODEL_NAME,
            anthropic_api_key="xyz",
            default_request_timeout=2,
            base_url="https://api.anthropic.com",
        ),
    ]:
        assert model.model == MODEL_NAME
        assert cast("SecretStr", model.anthropic_api_key).get_secret_value() == "xyz"
        assert model.default_request_timeout == 2.0
        assert model.anthropic_api_url == "https://api.anthropic.com"


@pytest.mark.parametrize("async_api", [True, False])
def test_streaming_attribute_should_stream(async_api: bool) -> None:  # noqa: FBT001
    llm = ChatAnthropic(model=MODEL_NAME, streaming=True)
    assert llm._should_stream(async_api=async_api)


// ... (2331 more lines)

Subdomains

Functions

Dependencies

  • anthropic
  • anthropic.types
  • blockbuster
  • collections.abc
  • langchain_anthropic
  • langchain_anthropic.chat_models
  • langchain_core.exceptions
  • langchain_core.messages
  • langchain_core.runnables
  • langchain_core.tools
  • langchain_core.tracers.base
  • langchain_core.tracers.schemas
  • os
  • pydantic
  • pytest
  • typing
  • unittest.mock

Frequently Asked Questions

What does test_chat_models.py do?
test_chat_models.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in test_chat_models.py?
test_chat_models.py defines 74 function(s): dummy_tool, function, json_schema, openai_function, pydantic, test__format_image, test__format_messages_openai_image_format, test__format_messages_trailing_whitespace, test__format_messages_with_cache_control, test__format_messages_with_citations, and 64 more.
What does test_chat_models.py depend on?
test_chat_models.py imports 17 module(s): anthropic, anthropic.types, blockbuster, collections.abc, langchain_anthropic, langchain_anthropic.chat_models, langchain_core.exceptions, langchain_core.messages, and 9 more.
Where is test_chat_models.py in the architecture?
test_chat_models.py is located at libs/partners/anthropic/tests/unit_tests/test_chat_models.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/partners/anthropic/tests/unit_tests).

Analyze Your Own Codebase

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

Try Supermodel Free