def to_openailike_message_dict(message: ChatMessage) -> dict:
    """Convert a ChatMessage to an OpenAI-like message dict."""
    content = []
    content_txt = ""
    for block in message.blocks:
        if isinstance(block, TextBlock):
            content.append({"type": "text", "text": block.text})
            content_txt += block.text
        elif isinstance(block, ImageBlock):
            if block.url:
                content.append(
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": str(block.url),
                            "detail": block.detail or "auto",
                        },
                    }
                )
            else:
                img_bytes = block.resolve_image(as_base64=True).read()
                img_str = img_bytes.decode("utf-8")
                content.append(
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:{block.image_mimetype};base64,{img_str}",
                            "detail": block.detail or "auto",
                        },
                    }
                )
        elif isinstance(block, AudioBlock):
            audio_bytes = block.resolve_audio(as_base64=True).read()
            audio_str = audio_bytes.decode("utf-8")
            content.append(
                {
                    "type": "input_audio",
                    "input_audio": {
                        "data": audio_str,
                        "format": block.format,
                    },
                }
            )
        elif isinstance(block, DocumentBlock):
            if not block.data:
                file_buffer = block.resolve_document()
                b64_string = block._get_b64_string(file_buffer)
                mimetype = block.document_mimetype or block._guess_mimetype()
            else:
                b64_string = block.data.decode("utf-8")
                mimetype = block.document_mimetype or block._guess_mimetype()
            content.append(
                {
                    "type": "file",
                    "file": {
                        "file_data": f"data:{mimetype};base64,{b64_string}",
                    },
                }
            )
        else:
            msg = f"Unsupported content block type: {type(block).__name__}"
            raise ValueError(msg)

    # use content as text instead of blocks if all blocks are text. Safer for backwards compatibility with older openai-like APIs
    message_dict = {
        "role": message.role.value,
        "content": (
            content_txt
            if all(isinstance(block, TextBlock) for block in message.blocks)
            else content
        ),
    }

    message_dict.update(message.additional_kwargs)

    return message_dict
Convert a ChatMessage to an OpenAI-like message dict.
llama-index-integrations/llms/llama-index-llms-litellm/llama_index/llms/litellm/utils.py