{"record":{"id":"0c67a009c0d9a25b","repo":"langchain-ai/langchain","slug":"key-base64-is-required-for-audio-blocks","errorCode":null,"errorMessage":"Key base64 is required for audio blocks.","messagePattern":"Key base64 is required for audio blocks\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/block_translators/openai.py","lineNumber":148,"sourceCode":"            # Only supported by Responses API; return in that format\n            formatted_block = {\"type\": \"input_file\", \"file_url\": block[\"url\"]}\n        else:\n            error_msg = \"Keys base64, url, or file_id required for file blocks.\"\n            raise ValueError(error_msg)\n\n    elif block[\"type\"] == \"audio\":\n        if \"base64\" in block or block.get(\"source_type\") == \"base64\":\n            # Handle v0 format: {\"source_type\": \"base64\", \"data\": \"...\", ...}\n            # Handle v1 format: {\"base64\": \"...\", ...}\n            base64_data = block[\"data\"] if \"source_type\" in block else block[\"base64\"]\n            audio_format = block[\"mime_type\"].split(\"/\")[-1]\n            formatted_block = {\n                \"type\": \"input_audio\",\n                \"input_audio\": {\"data\": base64_data, \"format\": audio_format},\n            }\n        else:\n            error_msg = \"Key base64 is required for audio blocks.\"\n            raise ValueError(error_msg)\n    else:\n        error_msg = f\"Block of type {block['type']} is not supported.\"\n        raise ValueError(error_msg)\n\n    return formatted_block\n\n\n# v1 / Chat Completions\ndef _convert_to_v1_from_chat_completions(\n    message: AIMessage,\n) -> list[types.ContentBlock]:\n    \"\"\"Mutate a Chat Completions message to v1 format.\"\"\"\n    content_blocks: list[types.ContentBlock] = []\n    if isinstance(message.content, str):\n        if message.content:\n            content_blocks = [{\"type\": \"text\", \"text\": message.content}]\n        else:\n            content_blocks = []","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/block_translators/openai.py#L130-L166","documentation":"Raised when an audio content block sent to the OpenAI converter has neither a `base64` key nor `source_type == \"base64\"`. OpenAI's audio input format (`input_audio`) requires inline base64 data plus a format derived from `mime_type`; audio supplied by URL or file ID is not supported on this path.","triggerScenarios":"Passing `{\"type\": \"audio\", \"url\": ...}` or `{\"type\": \"audio\", \"file_id\": ...}` (neither base64 key nor base64 source_type) to an OpenAI model or `convert_to_openai_data_block`.","commonSituations":"Assuming audio blocks mirror image blocks and accept URLs; passing audio recorded elsewhere as a link; blocks converted from other providers that store audio as a reference rather than inline bytes.","solutions":["Inline the audio: fetch/encode the bytes and pass `base64` plus `mime_type` (e.g. `audio/wav`)","If the audio only exists as a URL, download it first (`requests.get(url).content`) and base64-encode it","Use `AudioContentBlock(base64=..., mime_type=\"audio/wav\")` to build valid blocks"],"exampleFix":"# before\nblock = {\"type\": \"audio\", \"url\": \"https://example.com/clip.wav\"}\n\n# after\nimport base64, requests\nb64 = base64.b64encode(requests.get(\"https://example.com/clip.wav\").content).decode()\nblock = {\"type\": \"audio\", \"base64\": b64, \"mime_type\": \"audio/wav\"}","handlingStrategy":"validation","validationCode":"def has_audio_b64(block: dict) -> bool:\n    return \"base64\" in block or block.get(\"source_type\") == \"base64\"","typeGuard":"def is_inline_audio_block(block: dict) -> bool:\n    return block.get(\"type\") == \"audio\" and (\"base64\" in block or block.get(\"source_type\") == \"base64\")","tryCatchPattern":null,"preventionTips":["Always inline audio as base64 + mime_type for OpenAI","Don't reuse URL-based patterns from image blocks for audio","Encode audio at capture time and keep the mime type with the bytes"],"tags":["openai","audio","base64","content-blocks"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}