{"record":{"id":"085a40f52704572b","repo":"BerriAI/litellm","slug":"empty-image-url-string-is-not-valid","errorCode":null,"errorMessage":"Empty image_url string is not valid.","messagePattern":"Empty image_url string is not valid\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":600,"sourceCode":"\n    Raises:\n        ValueError: If image_url is invalid type or detail value is invalid\n    \"\"\"\n    if isinstance(image_url, dict):\n        detail: Final = image_url.get(\"detail\", \"auto\")\n        if detail not in [\"low\", \"high\", \"auto\"]:\n            raise ValueError(f\"Invalid detail value: {detail}. Expected 'low', 'high', or 'auto'.\")\n        url: Final = image_url.get(\"url\")\n        if not url:\n            raise ValueError(\"Missing required key 'url' in image_url dict.\")\n        return calculate_img_tokens(\n            data=url,\n            mode=detail,\n            use_default_image_token_count=use_default_image_token_count,\n        )\n    elif isinstance(image_url, str):\n        if not image_url.strip():\n            raise ValueError(\"Empty image_url string is not valid.\")\n        return calculate_img_tokens(\n            data=image_url,\n            mode=\"auto\",\n            use_default_image_token_count=use_default_image_token_count,\n        )\n    else:\n        raise ValueError(f\"Invalid image_url type: {type(image_url).__name__}. Expected str or dict with 'url' field.\")\n\n\ndef _validate_anthropic_content(content: Mapping[str, Any]) -> type:\n    \"\"\"\n    Validate and determine which Anthropic TypedDict applies.\n\n    Returns the corresponding TypedDict class if recognized, otherwise raises.\n    \"\"\"\n    content_type: Final = content.get(\"type\")\n    if not content_type:\n        raise ValueError(\"Anthropic content missing required field: 'type'\")","sourceCodeStart":582,"sourceCodeEnd":618,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/token_counter.py#L582-L618","documentation":"An image_url given as a plain string must be non-empty after stripping whitespace - an empty or whitespace-only string has no tokens to count and almost always signals a bug in the caller's data assembly.","triggerScenarios":"image_url set to an empty or whitespace-only string - typically a blank value from a template, an unset variable interpolated via f-string, or a user form submitted without an image.","commonSituations":"Users submitting forms without attaching an image; empty template placeholders; base64-encoding of an empty file producing an empty string upstream.","solutions":["Skip image blocks whose url is blank instead of passing them on.","Fail loudly at your input boundary when an image attachment is required but empty."],"exampleFix":"# before\nblocks = [{\"type\": \"image_url\", \"image_url\": url} for url in urls]  # url may be \"\"\n\n# after\nblocks = [{\"type\": \"image_url\", \"image_url\": url} for url in urls if url and url.strip()]","handlingStrategy":"validation","validationCode":"urls = [u for u in urls if isinstance(u, str) and u.strip()]  # drop blanks before building blocks","typeGuard":"def is_nonempty_image_url(value) -> bool:\n    return isinstance(value, str) and bool(value.strip())","tryCatchPattern":null,"preventionTips":["Reject empty image attachments at form/API validation time.","Truthiness-check url fields before building content blocks."],"tags":["token-counter","image","validation","empty-input"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}