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.
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)
Domain
Subdomains
Functions
- 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()
- test__format_messages_with_list_content_and_tool_calls()
- test__format_messages_with_multiple_system()
- test__format_messages_with_str_content_and_tool_calls()
- test__format_messages_with_tool_calls()
- test__format_messages_with_tool_use_blocks_and_tool_calls()
- test__format_output()
- test__format_output_cached()
- test__format_tool_use_block()
- test__merge_messages()
- test__merge_messages_mutation()
- test_anthropic_api_key_is_secret_string()
- test_anthropic_api_key_masked_when_passed_from_env()
- test_anthropic_api_key_masked_when_passed_via_constructor()
- test_anthropic_bind_tools_tool_choice()
- test_anthropic_client_caching()
- test_anthropic_fields_in_model_kwargs()
- test_anthropic_incorrect_field()
- test_anthropic_initialization()
- test_anthropic_model_kwargs()
- test_anthropic_model_name_param()
- test_anthropic_model_param()
- test_anthropic_model_params()
- test_anthropic_proxy_from_environment()
- test_anthropic_proxy_support()
- test_anthropic_uses_actual_secret_value_from_secretstr()
- test_auto_append_betas_for_mcp_servers()
- test_auto_append_betas_for_tool_types()
- test_cache_control_kwarg()
- test_cache_control_kwarg_skips_empty_messages()
- test_context_management_in_payload()
- test_context_overflow_error_backwards_compatibility()
- test_context_overflow_error_invoke_async()
- test_context_overflow_error_invoke_sync()
- test_context_overflow_error_stream_async()
- test_context_overflow_error_stream_sync()
- test_convert_to_anthropic_tool()
- test_effort_in_output_config()
- test_effort_in_output_config_payload()
- test_effort_parameter_validation()
- test_effort_priority()
- test_extras_with_cache_control()
- test_extras_with_defer_loading()
- test_extras_with_input_examples()
- test_extras_with_multiple_fields()
- test_fine_grained_tool_streaming_beta()
- test_get_num_tokens_from_messages_passes_kwargs()
- test_inference_geo_in_payload()
- test_initialization()
- test_mcp_tracing()
- test_model_profile_not_blocking()
- test_optional_description()
- test_output_config_without_effort()
- test_profile()
- test_response_format_with_output_config()
- test_set_default_max_tokens()
- test_streaming_attribute_should_stream()
- test_streaming_cache_token_reporting()
- test_strict_tool_use()
- test_strict_tool_use_payload()
- test_tool_search_beta_headers()
- test_tool_search_is_builtin_tool()
- test_tool_search_result_formatting()
- test_tool_search_with_deferred_tools()
- test_usage_metadata_standardization()
Classes
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
Source
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