Home / Function/ _format_image() — langchain Function Reference

_format_image() — langchain Function Reference

Architecture documentation for the _format_image() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  4db283f3_f14d_8856_90b5_c06620f07118["_format_image()"]
  a85819c7_917d_4c71_2864_a19e68947340["chat_models.py"]
  4db283f3_f14d_8856_90b5_c06620f07118 -->|defined in| a85819c7_917d_4c71_2864_a19e68947340
  7042d659_4e83_4ce8_9e4c_bfd32dddc9aa["_format_data_content_block()"]
  7042d659_4e83_4ce8_9e4c_bfd32dddc9aa -->|calls| 4db283f3_f14d_8856_90b5_c06620f07118
  a1122c23_c68d_f096_a90e_42f0e4d39d61["_format_messages()"]
  a1122c23_c68d_f096_a90e_42f0e4d39d61 -->|calls| 4db283f3_f14d_8856_90b5_c06620f07118
  style 4db283f3_f14d_8856_90b5_c06620f07118 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/chat_models.py lines 189–233

def _format_image(url: str) -> dict:
    """Convert part["image_url"]["url"] strings (OpenAI format) to Anthropic format.

    {
        "type": "base64",
        "media_type": "image/jpeg",
        "data": "/9j/4AAQSkZJRg...",
    }

    Or

    {
        "type": "url",
        "url": "https://example.com/image.jpg",
    }
    """
    # Base64 encoded image
    base64_regex = r"^data:(?P<media_type>image/.+);base64,(?P<data>.+)$"
    base64_match = re.match(base64_regex, url)

    if base64_match:
        return {
            "type": "base64",
            "media_type": base64_match.group("media_type"),
            "data": base64_match.group("data"),
        }

    # Url
    url_regex = r"^https?://.*$"
    url_match = re.match(url_regex, url)

    if url_match:
        return {
            "type": "url",
            "url": url,
        }

    msg = (
        "Malformed url parameter."
        " Must be either an image URL (https://example.com/image.jpg)"
        " or base64 encoded string (data:image/png;base64,'/9j/4AAQSk'...)"
    )
    raise ValueError(
        msg,
    )

Domain

Subdomains

Frequently Asked Questions

What does _format_image() do?
_format_image() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is _format_image() defined?
_format_image() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 189.
What calls _format_image()?
_format_image() is called by 2 function(s): _format_data_content_block, _format_messages.

Analyze Your Own Codebase

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

Try Supermodel Free