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. 22 imports, 0 dependents.

File python CoreAbstractions MessageSchema 22 imports 61 functions 6 classes

Entity Profile

Dependency Diagram

graph LR
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171["test_chat_models.py"]
  a327e534_84f6_5308_58ca_5727d5eda0cf["asyncio"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> a327e534_84f6_5308_58ca_5727d5eda0cf
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  9e98f0a7_ec6e_708f_4f1b_e9428b316e1c["os"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 9e98f0a7_ec6e_708f_4f1b_e9428b316e1c
  66c6348c_7716_027c_42d7_71449bc64eeb["base64"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 66c6348c_7716_027c_42d7_71449bc64eeb
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  1803c8c1_a347_1256_1454_9f04c3553d93["httpx"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 1803c8c1_a347_1256_1454_9f04c3553d93
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  792c09b7_7372_31d2_e29c_dc98949aa3c2["requests"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 792c09b7_7372_31d2_e29c_dc98949aa3c2
  ad604472_c022_b119_aeba_5ff8893361cd["anthropic"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> ad604472_c022_b119_aeba_5ff8893361cd
  839143dd_e377_b604_96de_3624dbdffeb5["langchain.agents"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 839143dd_e377_b604_96de_3624dbdffeb5
  c57803e6_8295_a18f_018d_3926ecdf1855["langchain.agents.structured_output"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> c57803e6_8295_a18f_018d_3926ecdf1855
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 --> d758344f_537f_649e_f467_b9d7442e86df
  style 8c3b89ce_c9b8_0b2f_93e9_bad2618f6171 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test ChatAnthropic chat model."""

from __future__ import annotations

import asyncio
import json
import os
from base64 import b64encode
from typing import Literal, cast

import httpx
import pytest
import requests
from anthropic import BadRequestError
from langchain.agents import create_agent
from langchain.agents.structured_output import ProviderStrategy
from langchain_core.callbacks import CallbackManager
from langchain_core.exceptions import OutputParserException
from langchain_core.messages import (
    AIMessage,
    AIMessageChunk,
    BaseMessage,
    BaseMessageChunk,
    HumanMessage,
    SystemMessage,
    ToolMessage,
)
from langchain_core.outputs import ChatGeneration, LLMResult
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool
from pydantic import BaseModel, Field
from typing_extensions import TypedDict

from langchain_anthropic import ChatAnthropic
from langchain_anthropic._compat import _convert_from_v1_to_anthropic
from tests.unit_tests._utils import FakeCallbackHandler

MODEL_NAME = "claude-3-5-haiku-20241022"


def test_stream() -> None:
    """Test streaming tokens from Anthropic."""
    llm = ChatAnthropic(model_name=MODEL_NAME)  # type: ignore[call-arg, call-arg]

    full: BaseMessageChunk | None = None
    chunks_with_input_token_counts = 0
    chunks_with_output_token_counts = 0
    chunks_with_model_name = 0
    for token in llm.stream("I'm Pickle Rick"):
        assert isinstance(token.content, str)
        full = cast("BaseMessageChunk", token) if full is None else full + token
        assert isinstance(token, AIMessageChunk)
        if token.usage_metadata is not None:
            if token.usage_metadata.get("input_tokens"):
                chunks_with_input_token_counts += 1
            if token.usage_metadata.get("output_tokens"):
                chunks_with_output_token_counts += 1
        chunks_with_model_name += int("model_name" in token.response_metadata)
    if chunks_with_input_token_counts != 1 or chunks_with_output_token_counts != 1:
        msg = (
// ... (2459 more lines)

Subdomains

Dependencies

  • anthropic
  • asyncio
  • base64
  • httpx
  • json
  • langchain.agents
  • langchain.agents.structured_output
  • langchain_anthropic
  • langchain_anthropic._compat
  • langchain_core.callbacks
  • langchain_core.exceptions
  • langchain_core.messages
  • langchain_core.outputs
  • langchain_core.prompts
  • langchain_core.tools
  • os
  • pydantic
  • pytest
  • requests
  • tests.unit_tests._utils
  • typing
  • typing_extensions

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 61 function(s): test_abatch, test_abatch_tags, test_agent_loop, test_agent_loop_streaming, test_ainvoke, test_anthropic_async_streaming_callback, test_anthropic_bind_tools_tool_choice, test_anthropic_call, test_anthropic_generate, test_anthropic_multimodal, and 51 more.
What does test_chat_models.py depend on?
test_chat_models.py imports 22 module(s): anthropic, asyncio, base64, httpx, json, langchain.agents, langchain.agents.structured_output, langchain_anthropic, and 14 more.
Where is test_chat_models.py in the architecture?
test_chat_models.py is located at libs/partners/anthropic/tests/integration_tests/test_chat_models.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/partners/anthropic/tests/integration_tests).

Analyze Your Own Codebase

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

Try Supermodel Free