{"record":{"id":"82a87bcc1b005258","repo":"langchain-ai/langchain","slug":"mime-type-key-is-required-for-base64-data","errorCode":null,"errorMessage":"mime_type key is required for base64 data.","messagePattern":"mime_type key is required for base64 data\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/block_translators/openai.py","lineNumber":45,"sourceCode":"\n    Raises:\n        ValueError: If required keys are missing.\n        ValueError: If source type is unsupported.\n\n    Returns:\n        The formatted image content block.\n    \"\"\"\n    if \"url\" in block:\n        return {\n            \"type\": \"image_url\",\n            \"image_url\": {\n                \"url\": block[\"url\"],\n            },\n        }\n    if \"base64\" in block or block.get(\"source_type\") == \"base64\":\n        if \"mime_type\" not in block:\n            error_message = \"mime_type key is required for base64 data.\"\n            raise ValueError(error_message)\n        mime_type = block[\"mime_type\"]\n        base64_data = block[\"data\"] if \"data\" in block else block[\"base64\"]\n        return {\n            \"type\": \"image_url\",\n            \"image_url\": {\n                \"url\": f\"data:{mime_type};base64,{base64_data}\",\n            },\n        }\n    error_message = \"Unsupported source type. Only 'url' and 'base64' are supported.\"\n    raise ValueError(error_message)\n\n\ndef convert_to_openai_data_block(\n    block: dict[str, Any],\n    api: Literal[\"chat/completions\", \"responses\"] = \"chat/completions\",\n) -> dict[str, Any]:\n    \"\"\"Format standard data content block to format expected by OpenAI.\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/block_translators/openai.py#L27-L63","documentation":"Raised when converting a standard image content block to the OpenAI format and the block indicates base64 data (`base64` key or `source_type == \"base64\"`) but has no `mime_type` key. OpenAI requires a complete data URI (`data:<mime>;base64,<data>`), which cannot be built without a MIME type.","triggerScenarios":"Passing a block like `{\"type\": \"image\", \"source_type\": \"base64\", \"data\": \"...\"}` (v0 style) or `{\"type\": \"image\", \"base64\": \"...\"}` (v1 style) without a `mime_type` field to an OpenAI model or to `convert_to_openai_data_block`.","commonSituations":"Hand-building image blocks from raw bytes and forgetting the MIME type; blocks produced by another provider's output that never carried `mime_type`; migrating from integrations that inferred the MIME type from context.","solutions":["Add `mime_type` to the block, e.g. `{..., \"mime_type\": \"image/png\"}`","Build blocks through the v1 helper which enforces the pair: `ImageContentBlock(base64=..., mime_type=\"image/png\")`","Infer the MIME type from a file extension or magic bytes before sending: `mimetypes.guess_type(path)[0]`"],"exampleFix":"# before\nblock = {\"type\": \"image\", \"base64\": b64_data}\n\n# after\nblock = {\"type\": \"image\", \"base64\": b64_data, \"mime_type\": \"image/png\"}","handlingStrategy":"validation","validationCode":"def valid_openai_image_block(block: dict) -> bool:\n    if \"url\" in block:\n        return True\n    if \"base64\" in block or block.get(\"source_type\") == \"base64\":\n        return \"mime_type\" in block\n    return False","typeGuard":"def is_b64_image_with_mime(block: dict) -> bool:\n    return (\"base64\" in block or block.get(\"source_type\") == \"base64\") and \"mime_type\" in block","tryCatchPattern":null,"preventionTips":["Always build image blocks with mime_type when using base64","Use ImageContentBlock(base64=..., mime_type=...) constructors instead of raw dicts","Derive mime_type with mimetypes.guess_type() at encode time"],"tags":["openai","image","base64","mime-type","content-blocks"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}