_utils.py — langchain Source File
Architecture documentation for _utils.py, a python file in the langchain codebase. 7 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 52504dd3_f4d4_21a4_b28c_0c152227d20d["_utils.py"] 67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"] 52504dd3_f4d4_21a4_b28c_0c152227d20d --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95 cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"] 52504dd3_f4d4_21a4_b28c_0c152227d20d --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] 52504dd3_f4d4_21a4_b28c_0c152227d20d --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"] 52504dd3_f4d4_21a4_b28c_0c152227d20d --> d758344f_537f_649e_f467_b9d7442e86df 67d9204c_49ef_a623_53ad_d78199dd66bc["langchain_core.messages.content"] 52504dd3_f4d4_21a4_b28c_0c152227d20d --> 67d9204c_49ef_a623_53ad_d78199dd66bc 5d424099_b75b_144f_f438_6c504b51b923["langchain_core.messages.block_translators.langchain_v0"] 52504dd3_f4d4_21a4_b28c_0c152227d20d --> 5d424099_b75b_144f_f438_6c504b51b923 5bb05482_5f6d_f5c2_1460_a2ec977e38f7["langchain_core.messages.block_translators.openai"] 52504dd3_f4d4_21a4_b28c_0c152227d20d --> 5bb05482_5f6d_f5c2_1460_a2ec977e38f7 style 52504dd3_f4d4_21a4_b28c_0c152227d20d fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import re
from collections.abc import Sequence
from typing import (
TYPE_CHECKING,
Literal,
TypedDict,
TypeVar,
)
if TYPE_CHECKING:
from langchain_core.messages import BaseMessage
from langchain_core.messages.content import (
ContentBlock,
)
def is_openai_data_block(
block: dict, filter_: Literal["image", "audio", "file"] | None = None
) -> bool:
"""Check whether a block contains multimodal data in OpenAI Chat Completions format.
Supports both data and ID-style blocks (e.g. `'file_data'` and `'file_id'`)
If additional keys are present, they are ignored / will not affect outcome as long
as the required keys are present and valid.
Args:
block: The content block to check.
filter_: If provided, only return True for blocks matching this specific type.
- "image": Only match image_url blocks
- "audio": Only match input_audio blocks
- "file": Only match file blocks
If `None`, match any valid OpenAI data block type. Note that this means that
if the block has a valid OpenAI data type but the filter_ is set to a
different type, this function will return False.
Returns:
`True` if the block is a valid OpenAI data block and matches the filter_
(if provided).
"""
if block.get("type") == "image_url":
if filter_ is not None and filter_ != "image":
return False
if (
(set(block.keys()) <= {"type", "image_url", "detail"})
and (image_url := block.get("image_url"))
and isinstance(image_url, dict)
):
url = image_url.get("url")
if isinstance(url, str):
# Required per OpenAI spec
return True
# Ignore `'detail'` since it's optional and specific to OpenAI
elif block.get("type") == "input_audio":
if filter_ is not None and filter_ != "audio":
return False
if (audio := block.get("input_audio")) and isinstance(audio, dict):
audio_data = audio.get("data")
// ... (268 more lines)
Domain
Subdomains
Functions
Classes
Dependencies
- collections.abc
- langchain_core.messages
- langchain_core.messages.block_translators.langchain_v0
- langchain_core.messages.block_translators.openai
- langchain_core.messages.content
- re
- typing
Source
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, Serialization subdomain.
What functions are defined in _utils.py?
_utils.py defines 7 function(s): _ensure_message_copy, _normalize_messages, _parse_data_uri, _update_content_block, _update_message_content_to_blocks, is_openai_data_block, langchain_core.
What does _utils.py depend on?
_utils.py imports 7 module(s): collections.abc, langchain_core.messages, langchain_core.messages.block_translators.langchain_v0, langchain_core.messages.block_translators.openai, langchain_core.messages.content, re, typing.
Where is _utils.py in the architecture?
_utils.py is located at libs/core/langchain_core/language_models/_utils.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core/language_models).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free