title: How do I decode an Avro message in Python?
body: <p>I am having trouble decoding an Avro message in Python (3.6.11). I have tried both the <code>avro</code> and <code>fastavro</code> packages. So I think that the problem may be that I'm providing the bytes incorrectly.</p>
  <p>Using <a href="https://repl.it/repls/HarshBelovedDebugger#main.py" rel="nofollow noreferrer">avro</a>:</p>
  <pre><code>from avro.io import DatumReader, BinaryDecoder
  import avro.schema
  from io import BytesIO
  
  schema = avro.schema.parse(&quot;&quot;&quot;
      {
          &quot;type&quot;: &quot;record&quot;,
          &quot;name&quot;: &quot;User&quot;,
          &quot;namespace&quot;: &quot;example.avro&quot;,
          &quot;fields&quot;: [
              {
                  &quot;name&quot;: &quot;name&quot;,
                  &quot;type&quot;: &quot;string&quot;
              },
              {
                  &quot;name&quot;: &quot;favorite_number&quot;,
                  &quot;type&quot;: [
                      &quot;int&quot;,
                      &quot;null&quot;
                  ]
              },
              {
                  &quot;name&quot;: &quot;favorite_color&quot;,
                  &quot;type&quot;: [
                      &quot;string&quot;,
                      &quot;null&quot;
                  ]
              }
          ]
      }
  &quot;&quot;&quot;)
  
  rb = BytesIO(b'{&quot;name&quot;: &quot;Alyssa&quot;, &quot;favorite_number&quot;: 256}')
  decoder = BinaryDecoder(rb)
  reader = DatumReader(schema)
  msg = reader.read(decoder)
  print(msg)
  
  Traceback (most recent call last):
    File &quot;main.py&quot;, line 36, in &lt;module&gt;
      msg = reader.read(decoder)
    File &quot;/opt/virtualenvs/python3/lib/python3.8/site-packages/avro/io.py&quot;, line 626, in read
      return self.read_data(self.writers_schema, self.readers_schema, decoder)
    File &quot;/opt/virtualenvs/python3/lib/python3.8/site-packages/avro/io.py&quot;, line 698, in read_data
      return self.read_record(writers_schema, readers_schema, decoder)
    File &quot;/opt/virtualenvs/python3/lib/python3.8/site-packages/avro/io.py&quot;, line 898, in read_record
      field_val = self.read_data(field.type, readers_field.type, decoder)
    File &quot;/opt/virtualenvs/python3/lib/python3.8/site-packages/avro/io.py&quot;, line 638, in read_data
      return self.read_union(writers_schema, readers_schema, decoder)
    File &quot;/opt/virtualenvs/python3/lib/python3.8/site-packages/avro/io.py&quot;, line 854, in read_union
      index_of_schema = int(decoder.read_long())
    File &quot;/opt/virtualenvs/python3/lib/python3.8/site-packages/avro/io.py&quot;, line 240, in read_long
      b = ord(self.read(1))
  TypeError: ord() expected a character, but string of length 0 found
  </code></pre>
  <p>Use <a href="https://repl.it/repls/EnchantedRapidNamebinding#main.py" rel="nofollow noreferrer">fastavro</a>:</p>
  <pre><code>from fastavro import schemaless_reader, parse_schema
  from io import BytesIO
  
  schema = parse_schema(
      {
          &quot;type&quot;: &quot;record&quot;,
          &quot;name&quot;: &quot;User&quot;,
          &quot;namespace&quot;: &quot;example.avro&quot;,
          &quot;fields&quot;: [
              {
                  &quot;name&quot;: &quot;name&quot;,
                  &quot;type&quot;: &quot;string&quot;
              },
              {
                  &quot;name&quot;: &quot;favorite_number&quot;,
                  &quot;type&quot;: [
                      &quot;int&quot;,
                      &quot;null&quot;
                  ]
              },
              {
                  &quot;name&quot;: &quot;favorite_color&quot;,
                  &quot;type&quot;: [
                      &quot;string&quot;,
                      &quot;null&quot;
                  ]
              }
          ]
      }
  )
  
  rb = BytesIO(b'{&quot;name&quot;: &quot;Alyssa&quot;, &quot;favorite_number&quot;: 256}')
  msg = schemaless_reader(rb, schema)
  print(msg)
  
  Traceback (most recent call last):
    File &quot;main.py&quot;, line 33, in &lt;module&gt;
      msg = schemaless_reader(rb, schema)
    File &quot;fastavro/_read.pyx&quot;, line 969, in fastavro._read.schemaless_reader
    File &quot;fastavro/_read.pyx&quot;, line 981, in fastavro._read.schemaless_reader
    File &quot;fastavro/_read.pyx&quot;, line 652, in fastavro._read._read_data
    File &quot;fastavro/_read.pyx&quot;, line 510, in fastavro._read.read_record
    File &quot;fastavro/_read.pyx&quot;, line 644, in fastavro._read._read_data
    File &quot;fastavro/_read.pyx&quot;, line 429, in fastavro._read.read_union
    File &quot;fastavro/_read.pyx&quot;, line 200, in fastavro._read.read_long
  StopIteration
  </code></pre>
  <p>I don't know if the message that I'm encoding is malformed or if the problem is with the encoding itself. Any suggestions?</p>