langchain-ai/langchain · error · ValueError
mime_type key is required for base64 data.
Error message
mime_type key is required for base64 data.
What it means
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.
Source
Thrown at libs/core/langchain_core/messages/block_translators/openai.py:45
Raises:
ValueError: If required keys are missing.
ValueError: If source type is unsupported.
Returns:
The formatted image content block.
"""
if "url" in block:
return {
"type": "image_url",
"image_url": {
"url": block["url"],
},
}
if "base64" in block or block.get("source_type") == "base64":
if "mime_type" not in block:
error_message = "mime_type key is required for base64 data."
raise ValueError(error_message)
mime_type = block["mime_type"]
base64_data = block["data"] if "data" in block else block["base64"]
return {
"type": "image_url",
"image_url": {
"url": f"data:{mime_type};base64,{base64_data}",
},
}
error_message = "Unsupported source type. Only 'url' and 'base64' are supported."
raise ValueError(error_message)
def convert_to_openai_data_block(
block: dict[str, Any],
api: Literal["chat/completions", "responses"] = "chat/completions",
) -> dict[str, Any]:
"""Format standard data content block to format expected by OpenAI.
View on GitHub (pinned to e32fa9a52e)
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]`
Example fix
# before
block = {"type": "image", "base64": b64_data}
# after
block = {"type": "image", "base64": b64_data, "mime_type": "image/png"} Defensive patterns
Strategy: validation
Validate before calling
def valid_openai_image_block(block: dict) -> bool:
if "url" in block:
return True
if "base64" in block or block.get("source_type") == "base64":
return "mime_type" in block
return False Type guard
def is_b64_image_with_mime(block: dict) -> bool:
return ("base64" in block or block.get("source_type") == "base64") and "mime_type" in block Prevention
- 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
When it happens
Trigger: 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`.
Common situations: 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.
Related errors
- Unsupported source type. Only 'url' and 'base64' are support
- Key base64 is required for audio blocks.
- mime_type is required when using base64 data
- Image URL not a data URI; appending as non-standard block.
- Keys base64, url, or file_id required for file blocks.
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/82a87bcc1b005258.
Report an issue: GitHub.