Home / File/ utils.py — langchain Source File

utils.py — langchain Source File

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

File python CoreAbstractions MessageSchema 27 imports 31 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  0b528c80_0ce7_1c74_8932_bc433bcb03c6["utils.py"]
  66c6348c_7716_027c_42d7_71449bc64eeb["base64"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 66c6348c_7716_027c_42d7_71449bc64eeb
  614e7b9f_ed51_0780_749c_ff40b74963fc["inspect"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 614e7b9f_ed51_0780_749c_ff40b74963fc
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  2a7f66a7_8738_3d47_375b_70fcaa6ac169["logging"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 2a7f66a7_8738_3d47_375b_70fcaa6ac169
  6d7cdba5_8e52_34b5_6742_57caf6500c80["math"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 6d7cdba5_8e52_34b5_6742_57caf6500c80
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  c990f2d7_9509_7cea_ca95_51ad57dbe5c6["functools"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> c990f2d7_9509_7cea_ca95_51ad57dbe5c6
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  c808b74e_8469_46a3_8d31_099920e5198b["xml.sax.saxutils"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> c808b74e_8469_46a3_8d31_099920e5198b
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  4eb42b7a_5c64_04cb_fcec_1401d5c10628["langchain_core.messages.ai"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 4eb42b7a_5c64_04cb_fcec_1401d5c10628
  a1369c93_b21f_2edb_d15c_ec3e09ac1e42["langchain_core.messages.base"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> a1369c93_b21f_2edb_d15c_ec3e09ac1e42
  5bb05482_5f6d_f5c2_1460_a2ec977e38f7["langchain_core.messages.block_translators.openai"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6 --> 5bb05482_5f6d_f5c2_1460_a2ec977e38f7
  style 0b528c80_0ce7_1c74_8932_bc433bcb03c6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Module contains utility functions for working with messages.

Some examples of what you can do with these functions include:

* Convert messages to strings (serialization)
* Convert messages from dicts to Message objects (deserialization)
* Filter messages from a list of messages based on name, type or id etc.
"""

from __future__ import annotations

import base64
import inspect
import json
import logging
import math
from collections.abc import Callable, Iterable, Sequence
from functools import partial, wraps
from typing import (
    TYPE_CHECKING,
    Annotated,
    Any,
    Concatenate,
    Literal,
    ParamSpec,
    Protocol,
    TypeVar,
    cast,
    overload,
)
from xml.sax.saxutils import escape, quoteattr

from pydantic import Discriminator, Field, Tag

from langchain_core.exceptions import ErrorCode, create_message
from langchain_core.messages.ai import AIMessage, AIMessageChunk
from langchain_core.messages.base import BaseMessage, BaseMessageChunk
from langchain_core.messages.block_translators.openai import (
    convert_to_openai_data_block,
)
from langchain_core.messages.chat import ChatMessage, ChatMessageChunk
from langchain_core.messages.content import (
    is_data_content_block,
)
from langchain_core.messages.function import FunctionMessage, FunctionMessageChunk
from langchain_core.messages.human import HumanMessage, HumanMessageChunk
from langchain_core.messages.modifier import RemoveMessage
from langchain_core.messages.system import SystemMessage, SystemMessageChunk
from langchain_core.messages.tool import ToolCall, ToolMessage, ToolMessageChunk
from langchain_core.utils.function_calling import convert_to_openai_tool

if TYPE_CHECKING:
    from langchain_core.language_models import BaseLanguageModel
    from langchain_core.prompt_values import PromptValue
    from langchain_core.runnables.base import Runnable
    from langchain_core.tools import BaseTool

try:
    from langchain_text_splitters import TextSplitter

// ... (2294 more lines)

Subdomains

Dependencies

  • base64
  • collections.abc
  • functools
  • inspect
  • json
  • langchain_core.exceptions
  • langchain_core.language_models
  • langchain_core.messages.ai
  • langchain_core.messages.base
  • langchain_core.messages.block_translators.openai
  • langchain_core.messages.chat
  • langchain_core.messages.content
  • langchain_core.messages.function
  • langchain_core.messages.human
  • langchain_core.messages.modifier
  • langchain_core.messages.system
  • langchain_core.messages.tool
  • langchain_core.prompt_values
  • langchain_core.runnables.base
  • langchain_core.tools
  • langchain_core.utils.function_calling
  • langchain_text_splitters
  • logging
  • math
  • pydantic
  • typing
  • xml.sax.saxutils

Frequently Asked Questions

What does utils.py do?
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 utils.py?
utils.py defines 31 function(s): _HAS_LANGCHAIN_TEXT_SPLITTERS, _approximate_token_counter, _bytes_to_b64_str, _chunk_to_msg, _convert_to_message, _convert_to_openai_tool_calls, _create_message_from_message_type, _default_text_splitter, _first_max_tokens, _format_content_block_xml, and 21 more.
What does utils.py depend on?
utils.py imports 27 module(s): base64, collections.abc, functools, inspect, json, langchain_core.exceptions, langchain_core.language_models, langchain_core.messages.ai, and 19 more.
Where is utils.py in the architecture?
utils.py is located at libs/core/langchain_core/messages/utils.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/langchain_core/messages).

Analyze Your Own Codebase

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

Try Supermodel Free