def test_document_block_conversion():
    """Test converting a ChatMessage with DocumentBlock to OpenAI format."""
    # Create a DocumentBlock with mock PDF data
    mock_pdf_data = b"fake_pdf_data"
    document_block = DocumentBlock(
        data=mock_pdf_data,
        document_mimetype="application/pdf",
        title="test_document.pdf",
    )
    message = ChatMessage(role=MessageRole.USER, content=[document_block])

    # Convert to OpenAI format
    openai_message = to_openailike_message_dict(message)

    # Verify correct format for document block
    assert openai_message["role"] == "user"
    assert isinstance(openai_message["content"], list)
    assert len(openai_message["content"]) == 1

    document_content = openai_message["content"][0]
    assert document_content["type"] == "file"
    assert "file" in document_content
    assert "file_data" in document_content["file"]
    assert document_content["file"]["file_data"].startswith(
        "data:application/pdf;base64,"
    )

    # Verify the base64 encoded data
    file_data = document_content["file"]["file_data"]
    header, encoded_data = file_data.split(",", 1)
    assert header == "data:application/pdf;base64"

    # Verify the encoded data can be decoded back to original
    decoded_data = base64.b64decode(encoded_data.encode("utf-8"))
    assert decoded_data == mock_pdf_data
Test converting a ChatMessage with DocumentBlock to OpenAI format.
llama-index-integrations/llms/llama-index-llms-litellm/tests/test_utils.py