Home / Function/ is_openai_data_block() — langchain Function Reference

is_openai_data_block() — langchain Function Reference

Architecture documentation for the is_openai_data_block() function in _utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  8b851a24_8640_e308_679c_b4265f6177ef["is_openai_data_block()"]
  52504dd3_f4d4_21a4_b28c_0c152227d20d["_utils.py"]
  8b851a24_8640_e308_679c_b4265f6177ef -->|defined in| 52504dd3_f4d4_21a4_b28c_0c152227d20d
  030ac02c_2931_4d20_1689_430fac9b6609["_normalize_messages()"]
  030ac02c_2931_4d20_1689_430fac9b6609 -->|calls| 8b851a24_8640_e308_679c_b4265f6177ef
  style 8b851a24_8640_e308_679c_b4265f6177ef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/language_models/_utils.py lines 17–80

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")
            audio_format = audio.get("format")
            # Both required per OpenAI spec
            if isinstance(audio_data, str) and isinstance(audio_format, str):
                return True

    elif block.get("type") == "file":
        if filter_ is not None and filter_ != "file":
            return False
        if (file := block.get("file")) and isinstance(file, dict):
            file_data = file.get("file_data")
            file_id = file.get("file_id")
            # Files can be either base64-encoded or pre-uploaded with an ID
            if isinstance(file_data, str) or isinstance(file_id, str):
                return True

    else:
        return False

    # Has no `'type'` key
    return False

Subdomains

Frequently Asked Questions

What does is_openai_data_block() do?
is_openai_data_block() is a function in the langchain codebase, defined in libs/core/langchain_core/language_models/_utils.py.
Where is is_openai_data_block() defined?
is_openai_data_block() is defined in libs/core/langchain_core/language_models/_utils.py at line 17.
What calls is_openai_data_block()?
is_openai_data_block() is called by 1 function(s): _normalize_messages.

Analyze Your Own Codebase

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

Try Supermodel Free