Home / Function/ _convert_to_v1_from_chat_completions_input() — langchain Function Reference

_convert_to_v1_from_chat_completions_input() — langchain Function Reference

Architecture documentation for the _convert_to_v1_from_chat_completions_input() function in openai.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e06c3a3b_b539_e71c_f165_d7355563ba61["_convert_to_v1_from_chat_completions_input()"]
  3f58992c_878c_57d1_7412_5e64743aa7ba["openai.py"]
  e06c3a3b_b539_e71c_f165_d7355563ba61 -->|defined in| 3f58992c_878c_57d1_7412_5e64743aa7ba
  f017275d_db65_5cb1_527d_53d0c724527f["_convert_openai_format_to_data_block()"]
  e06c3a3b_b539_e71c_f165_d7355563ba61 -->|calls| f017275d_db65_5cb1_527d_53d0c724527f
  style e06c3a3b_b539_e71c_f165_d7355563ba61 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/openai.py lines 178–223

def _convert_to_v1_from_chat_completions_input(
    content: list[types.ContentBlock],
) -> list[types.ContentBlock]:
    """Convert OpenAI Chat Completions format blocks to v1 format.

    During the `content_blocks` parsing process, we wrap blocks not recognized as a v1
    block as a `'non_standard'` block with the original block stored in the `value`
    field. This function attempts to unpack those blocks and convert any blocks that
    might be OpenAI format to v1 ContentBlocks.

    If conversion fails, the block is left as a `'non_standard'` block.

    Args:
        content: List of content blocks to process.

    Returns:
        Updated list with OpenAI blocks converted to v1 format.
    """
    converted_blocks = []
    unpacked_blocks: list[dict[str, Any]] = [
        cast("dict[str, Any]", block)
        if block.get("type") != "non_standard"
        else block["value"]  # type: ignore[typeddict-item]  # this is only non-standard blocks
        for block in content
    ]
    for block in unpacked_blocks:
        if block.get("type") in {
            "image_url",
            "input_audio",
            "file",
        } and is_openai_data_block(block):
            converted_block = _convert_openai_format_to_data_block(block)
            # If conversion succeeded, use it; otherwise keep as non_standard
            if (
                isinstance(converted_block, dict)
                and converted_block.get("type") in types.KNOWN_BLOCK_TYPES
            ):
                converted_blocks.append(cast("types.ContentBlock", converted_block))
            else:
                converted_blocks.append({"type": "non_standard", "value": block})
        elif block.get("type") in types.KNOWN_BLOCK_TYPES:
            converted_blocks.append(cast("types.ContentBlock", block))
        else:
            converted_blocks.append({"type": "non_standard", "value": block})

    return converted_blocks

Domain

Subdomains

Frequently Asked Questions

What does _convert_to_v1_from_chat_completions_input() do?
_convert_to_v1_from_chat_completions_input() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/block_translators/openai.py.
Where is _convert_to_v1_from_chat_completions_input() defined?
_convert_to_v1_from_chat_completions_input() is defined in libs/core/langchain_core/messages/block_translators/openai.py at line 178.
What does _convert_to_v1_from_chat_completions_input() call?
_convert_to_v1_from_chat_completions_input() calls 1 function(s): _convert_openai_format_to_data_block.

Analyze Your Own Codebase

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

Try Supermodel Free