Python3 error: initial_value must be str or None

While porting code from python2 to 3, I get this error when reading from a URL

TypeError: initial_value must be str or None, not bytes.

    import urllib
    import json
    import gzip
    from urllib.parse import urlencode
    from urllib.request import Request

    service_url = ''
    text = 'BabelNet is both a multilingual encyclopedic dictionary and a semantic network'
    lang = 'EN'
    Key  = 'KEY'

        params = {
            'text' : text,
            'key'  : Key,
            'lang' :'EN'


    url = service_url + '?' + urllib.urlencode(params)
    request = Request(url)
    request.add_header('Accept-encoding', 'gzip')
    response = urllib.request.urlopen(request)
    if'Content-Encoding') == 'gzip':
                buf = StringIO(
                f = gzip.GzipFile(fileobj=buf)
                data = json.loads(

The exception is thrown at this line

    buf = StringIO(

If I use python2, it works fine. returns an instance of bytes while StringIO is an in-memory stream for text only. Use BytesIO instead.

From What's new in Python 3.0 - Text Vs. Data Instead Of Unicode Vs. 8-bit

The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.