test_utils.py — langchain Source File
Architecture documentation for test_utils.py, a python file in the langchain codebase. 12 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR ea3f8d89_f44b_6738_3cb9_a740a73cfca4["test_utils.py"] 66c6348c_7716_027c_42d7_71449bc64eeb["base64"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 66c6348c_7716_027c_42d7_71449bc64eeb 7025b240_fdc3_cf68_b72f_f41dac94566b["json"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 7025b240_fdc3_cf68_b72f_f41dac94566b 6d7cdba5_8e52_34b5_6742_57caf6500c80["math"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 6d7cdba5_8e52_34b5_6742_57caf6500c80 67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95 cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 120e2591_3e15_b895_72b6_cb26195e40a6["pytest"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 120e2591_3e15_b895_72b6_cb26195e40a6 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 91721f45_4909_e489_8c1f_084f8bd87145 833aeadc_c3e9_bfcf_db07_ecb37ad3ba24["langchain_core.language_models.fake_chat_models"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 833aeadc_c3e9_bfcf_db07_ecb37ad3ba24 d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> d758344f_537f_649e_f467_b9d7442e86df 5563046c_094e_e4ab_c007_ae813975fd4d["langchain_core.messages.utils"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 5563046c_094e_e4ab_c007_ae813975fd4d 43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"] ea3f8d89_f44b_6738_3cb9_a740a73cfca4 --> 43d88577_548b_2248_b01b_7987bae85dcc style ea3f8d89_f44b_6738_3cb9_a740a73cfca4 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import base64
import json
import math
import re
from collections.abc import Callable, Sequence
from typing import Any, TypedDict
import pytest
from typing_extensions import NotRequired, override
from langchain_core.language_models.fake_chat_models import FakeChatModel
from langchain_core.messages import (
AIMessage,
BaseMessage,
ChatMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
ToolCall,
ToolMessage,
)
from langchain_core.messages.utils import (
MessageLikeRepresentation,
convert_to_messages,
convert_to_openai_messages,
count_tokens_approximately,
filter_messages,
get_buffer_string,
merge_message_runs,
trim_messages,
)
from langchain_core.tools import BaseTool, tool
@pytest.mark.parametrize("msg_cls", [HumanMessage, AIMessage, SystemMessage])
def test_merge_message_runs_str(msg_cls: type[BaseMessage]) -> None:
messages = [msg_cls("foo"), msg_cls("bar"), msg_cls("baz")]
messages_model_copy = [m.model_copy(deep=True) for m in messages]
expected = [msg_cls("foo\nbar\nbaz")]
actual = merge_message_runs(messages)
assert actual == expected
assert messages == messages_model_copy
@pytest.mark.parametrize("msg_cls", [HumanMessage, AIMessage, SystemMessage])
def test_merge_message_runs_str_with_specified_separator(
msg_cls: type[BaseMessage],
) -> None:
messages = [msg_cls("foo"), msg_cls("bar"), msg_cls("baz")]
messages_model_copy = [m.model_copy(deep=True) for m in messages]
expected = [msg_cls("foo<sep>bar<sep>baz")]
actual = merge_message_runs(messages, chunk_separator="<sep>")
assert actual == expected
assert messages == messages_model_copy
@pytest.mark.parametrize("msg_cls", [HumanMessage, AIMessage, SystemMessage])
def test_merge_message_runs_str_without_separator(
msg_cls: type[BaseMessage],
) -> None:
// ... (2901 more lines)
Domain
Subdomains
Functions
- create_base64_image()
- create_image_data()
- dummy_token_counter()
- test_convert_to_messages()
- test_convert_to_messages_openai_refusal()
- test_convert_to_openai_messages_anthropic()
- test_convert_to_openai_messages_bedrock_converse_image()
- test_convert_to_openai_messages_developer()
- test_convert_to_openai_messages_empty_list()
- test_convert_to_openai_messages_empty_message()
- test_convert_to_openai_messages_guard_content()
- test_convert_to_openai_messages_invalid_block()
- test_convert_to_openai_messages_invalid_format()
- test_convert_to_openai_messages_json()
- test_convert_to_openai_messages_mixed_content_types()
- test_convert_to_openai_messages_multimodal()
- test_convert_to_openai_messages_multiple_messages()
- test_convert_to_openai_messages_openai_block()
- test_convert_to_openai_messages_openai_image()
- test_convert_to_openai_messages_openai_string()
- test_convert_to_openai_messages_reasoning_content()
- test_convert_to_openai_messages_single_message()
- test_convert_to_openai_messages_string()
- test_convert_to_openai_messages_tool_message()
- test_convert_to_openai_messages_tool_use()
- test_convert_to_openai_messages_tool_use_unicode()
- test_convert_to_openai_messages_vertexai_image()
- test_count_tokens_approximately_ai_tool_calls_skipped_for_list_content()
- test_count_tokens_approximately_custom_token_length()
- test_count_tokens_approximately_empty_messages()
- test_count_tokens_approximately_large_message_content()
- test_count_tokens_approximately_large_number_of_messages()
- test_count_tokens_approximately_list_content()
- test_count_tokens_approximately_mixed_content_types()
- test_count_tokens_approximately_openai_format()
- test_count_tokens_approximately_respects_count_name_flag()
- test_count_tokens_approximately_string_content()
- test_count_tokens_approximately_text_only_backward_compatible()
- test_count_tokens_approximately_tool_calls()
- test_count_tokens_approximately_usage_metadata_scaling()
- test_count_tokens_approximately_usage_metadata_scaling_floor_at_one()
- test_count_tokens_approximately_usage_metadata_scaling_model_provider()
- test_count_tokens_approximately_usage_metadata_scaling_total_tokens()
- test_count_tokens_approximately_with_custom_image_penalty()
- test_count_tokens_approximately_with_image_content()
- test_count_tokens_approximately_with_image_only_message()
- test_count_tokens_approximately_with_multiple_images()
- test_count_tokens_approximately_with_names()
- test_count_tokens_approximately_with_tools()
- test_count_tokens_approximately_with_unknown_block_type()
- test_filter_message()
- test_filter_message_exclude_tool_calls()
- test_filter_message_exclude_tool_calls_content_blocks()
- test_get_buffer_string_all_custom_prefixes()
- test_get_buffer_string_custom_function_prefix()
- test_get_buffer_string_custom_system_prefix()
- test_get_buffer_string_custom_tool_prefix()
- test_get_buffer_string_invalid_format()
- test_get_buffer_string_prefix_custom_separator()
- test_get_buffer_string_tool_calls_preferred_over_function_call()
- test_get_buffer_string_with_empty_content()
- test_get_buffer_string_with_function_call()
- test_get_buffer_string_with_mixed_content()
- test_get_buffer_string_with_structured_content()
- test_get_buffer_string_with_tool_calls()
- test_get_buffer_string_with_tool_calls_empty_content()
- test_get_buffer_string_xml_all_custom_prefixes()
- test_get_buffer_string_xml_audio_base64_skipped()
- test_get_buffer_string_xml_audio_url_block()
- test_get_buffer_string_xml_basic()
- test_get_buffer_string_xml_chat_message_valid_role()
- test_get_buffer_string_xml_custom_function_prefix()
- test_get_buffer_string_xml_custom_prefixes()
- test_get_buffer_string_xml_custom_separator()
- test_get_buffer_string_xml_custom_system_prefix()
- test_get_buffer_string_xml_custom_tool_prefix()
- test_get_buffer_string_xml_empty_content()
- test_get_buffer_string_xml_empty_messages_list()
- test_get_buffer_string_xml_escaping()
- test_get_buffer_string_xml_escaping_in_content_blocks()
- test_get_buffer_string_xml_function_call_legacy()
- test_get_buffer_string_xml_function_call_special_chars_in_name()
- test_get_buffer_string_xml_image_base64_skipped()
- test_get_buffer_string_xml_image_data_url_skipped()
- test_get_buffer_string_xml_image_file_id_block()
- test_get_buffer_string_xml_image_url_block()
- test_get_buffer_string_xml_mixed_content_blocks()
- test_get_buffer_string_xml_multiline_content()
- test_get_buffer_string_xml_multiple_tool_calls()
- test_get_buffer_string_xml_no_truncation_under_limit()
- test_get_buffer_string_xml_openai_image_url_block()
- test_get_buffer_string_xml_openai_image_url_data_skipped()
- test_get_buffer_string_xml_reasoning_block()
- test_get_buffer_string_xml_server_tool_call_args_truncation()
- test_get_buffer_string_xml_server_tool_call_block()
- test_get_buffer_string_xml_server_tool_result_block()
- test_get_buffer_string_xml_server_tool_result_output_truncation()
- test_get_buffer_string_xml_structured_content()
- test_get_buffer_string_xml_text_plain_block()
- test_get_buffer_string_xml_text_plain_truncation()
- test_get_buffer_string_xml_tool_call_none_id()
- test_get_buffer_string_xml_tool_call_special_chars_in_attrs()
- test_get_buffer_string_xml_tool_calls_empty_content()
- test_get_buffer_string_xml_tool_calls_escaping()
- test_get_buffer_string_xml_tool_calls_preferred_over_function_call()
- test_get_buffer_string_xml_tool_calls_with_content()
- test_get_buffer_string_xml_unicode_content()
- test_get_buffer_string_xml_unknown_block_type_skipped()
- test_get_buffer_string_xml_url_with_special_chars()
- test_get_buffer_string_xml_video_base64_skipped()
- test_get_buffer_string_xml_video_url_block()
- test_handle_openai_responses_blocks()
- test_merge_message_runs_content()
- test_merge_message_runs_response_metadata()
- test_merge_message_runs_str()
- test_merge_message_runs_str_with_specified_separator()
- test_merge_message_runs_str_without_separator()
- test_merge_messages_tool_messages()
- test_trim_messages_allow_partial_one_message()
- test_trim_messages_allow_partial_text_splitter()
- test_trim_messages_bad_token_counter()
- test_trim_messages_bound_model_token_counter()
- test_trim_messages_exact_token_boundary()
- test_trim_messages_first_30()
- test_trim_messages_first_30_allow_partial()
- test_trim_messages_first_30_allow_partial_end_on_human()
- test_trim_messages_include_system_strategy_last_empty_messages()
- test_trim_messages_invoke()
- test_trim_messages_last_30_include_system()
- test_trim_messages_last_30_include_system_allow_partial_end_on_human()
- test_trim_messages_last_40_include_system_allow_partial()
- test_trim_messages_last_40_include_system_allow_partial_start_on_human()
- test_trim_messages_last_allow_partial_one_message()
- test_trim_messages_mixed_content_with_partial()
- test_trim_messages_partial_text_splitting()
- test_trim_messages_start_on_with_allow_partial()
- test_trim_messages_token_counter_shortcut_approximate()
- test_trim_messages_token_counter_shortcut_invalid()
- test_trim_messages_token_counter_shortcut_with_options()
Dependencies
- base64
- collections.abc
- json
- langchain_core.language_models.fake_chat_models
- langchain_core.messages
- langchain_core.messages.utils
- langchain_core.tools
- math
- pytest
- re
- typing
- typing_extensions
Source
Frequently Asked Questions
What does test_utils.py do?
test_utils.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_utils.py?
test_utils.py defines 139 function(s): create_base64_image, create_image_data, dummy_token_counter, test_convert_to_messages, test_convert_to_messages_openai_refusal, test_convert_to_openai_messages_anthropic, test_convert_to_openai_messages_bedrock_converse_image, test_convert_to_openai_messages_developer, test_convert_to_openai_messages_empty_list, test_convert_to_openai_messages_empty_message, and 129 more.
What does test_utils.py depend on?
test_utils.py imports 12 module(s): base64, collections.abc, json, langchain_core.language_models.fake_chat_models, langchain_core.messages, langchain_core.messages.utils, langchain_core.tools, math, and 4 more.
Where is test_utils.py in the architecture?
test_utils.py is located at libs/core/tests/unit_tests/messages/test_utils.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/tests/unit_tests/messages).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free