Home / Function/ _url_to_size() — langchain Function Reference

_url_to_size() — langchain Function Reference

Architecture documentation for the _url_to_size() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  749c696d_69d8_c3f0_ae59_34c44e681f49["_url_to_size()"]
  2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"]
  749c696d_69d8_c3f0_ae59_34c44e681f49 -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda
  3d7b9c9e_fae0_940a_5e96_75a84e2ad11c["get_num_tokens_from_messages()"]
  3d7b9c9e_fae0_940a_5e96_75a84e2ad11c -->|calls| 749c696d_69d8_c3f0_ae59_34c44e681f49
  fad1cb86_8af7_214e_1f81_508ed797197b["_is_url()"]
  749c696d_69d8_c3f0_ae59_34c44e681f49 -->|calls| fad1cb86_8af7_214e_1f81_508ed797197b
  2df17538_98c7_e9cb_e0a2_dfc6a6ac2c41["_is_b64()"]
  749c696d_69d8_c3f0_ae59_34c44e681f49 -->|calls| 2df17538_98c7_e9cb_e0a2_dfc6a6ac2c41
  style 749c696d_69d8_c3f0_ae59_34c44e681f49 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/chat_models/base.py lines 3518–3597

def _url_to_size(image_source: str) -> tuple[int, int] | None:
    try:
        from PIL import Image  # type: ignore[import]
    except ImportError:
        logger.info(
            "Unable to count image tokens. To count image tokens please install "
            "`pip install -U pillow httpx`."
        )
        return None
    if _is_url(image_source):
        try:
            import httpx
        except ImportError:
            logger.info(
                "Unable to count image tokens. To count image tokens please install "
                "`pip install -U httpx`."
            )
            return None

        # Validate URL for SSRF protection
        try:
            from langchain_core._security._ssrf_protection import validate_safe_url

            validate_safe_url(image_source, allow_private=False, allow_http=True)
        except ImportError:
            logger.warning(
                "SSRF protection not available. "
                "Update langchain-core to get SSRF protection."
            )
        except ValueError as e:
            logger.warning("Image URL failed SSRF validation: %s", e)
            return None

        # Set reasonable limits to prevent resource exhaustion
        # Timeout prevents indefinite hangs on slow/malicious servers
        timeout = 5.0  # seconds
        # Max size matches OpenAI's 50 MB payload limit
        max_size = 50 * 1024 * 1024  # 50 MB

        try:
            response = httpx.get(
                image_source,
                timeout=timeout,
            )
            response.raise_for_status()

            # Check response size before loading into memory
            content_length = response.headers.get("content-length")
            if content_length and int(content_length) > max_size:
                logger.warning(
                    "Image URL exceeds maximum size limit of %d bytes", max_size
                )
                return None

            # Also check actual content size
            if len(response.content) > max_size:
                logger.warning(
                    "Image URL exceeds maximum size limit of %d bytes", max_size
                )
                return None

            # close things (context managers)
            width, height = Image.open(BytesIO(response.content)).size
            return width, height
        except httpx.TimeoutException:
            logger.warning("Image URL request timed out after %s seconds", timeout)
            return None
        except httpx.HTTPStatusError as e:
            logger.warning("Image URL returned HTTP error: %s", e)
            return None
        except Exception as e:
            logger.warning("Failed to fetch or process image from URL: %s", e)
            return None

    if _is_b64(image_source):
        _, encoded = image_source.split(",", 1)
        data = base64.b64decode(encoded)
        width, height = Image.open(BytesIO(data)).size
        return width, height
    return None

Domain

Subdomains

Frequently Asked Questions

What does _url_to_size() do?
_url_to_size() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is _url_to_size() defined?
_url_to_size() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 3518.
What does _url_to_size() call?
_url_to_size() calls 2 function(s): _is_b64, _is_url.
What calls _url_to_size()?
_url_to_size() is called by 1 function(s): get_num_tokens_from_messages.

Analyze Your Own Codebase

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

Try Supermodel Free