{"record":{"id":"0407de7e50eab7e4","repo":"apache/beam","slug":"avrorowwriter-is-not-readable","errorCode":null,"errorMessage":"AvroRowWriter is not readable","messagePattern":"AvroRowWriter is not readable","errorType":"exception","errorClass":"io.UnsupportedOperation","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/gcp/bigquery_tools.py","lineNumber":1536,"sourceCode":"\n  def close(self):\n    if not self._file_handle.closed:\n      self.flush()\n      self._file_handle.close()\n\n  @property\n  def closed(self):\n    return self._file_handle.closed\n\n  def flush(self):\n    if self._file_handle.closed:\n      raise ValueError(\"flush on closed file\")\n\n    self._avro_writer.flush()\n    self._file_handle.flush()\n\n  def read(self, size=-1):\n    raise io.UnsupportedOperation(\"AvroRowWriter is not readable\")\n\n  def tell(self):\n    # Flush the fastavro Writer to the underlying stream, otherwise there isn't\n    # a reliable way to determine how many bytes have been written.\n    self._avro_writer.flush()\n    return self._file_handle.tell()\n\n  def writable(self):\n    return self._file_handle.writable()\n\n  def write(self, row):\n    try:\n      self._avro_writer.write(row)\n    except (TypeError, ValueError) as ex:\n      _, _, tb = sys.exc_info()\n      raise ex.__class__(\n          \"Error writing row to Avro: {}\\nSchema: {}\\nRow: {}\".format(\n              ex, self._avro_writer.schema, row)).with_traceback(tb)","sourceCodeStart":1518,"sourceCodeEnd":1554,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L1518-L1554","documentation":"AvroRowWriter wraps a write-only stream feeding a fastavro Writer for BigQuery Avro loads; its read() raises io.UnsupportedOperation('AvroRowWriter is not readable') because the writer cannot serve reads. It signals reading from a write-only writer.","triggerScenarios":"Calling .read() on an AvroRowWriter, or passing it to an API expecting a readable file-like object (e.g. uploading via a read handle).","commonSituations":"Passing the writer to Beam/GCS code that opens its own readable handle; confusing AvroRowWriter with an AvroReader; attempting to verify written Avro bytes by reading from the writer instead of the file.","solutions":["Close/flush the writer and reopen the file (or use the matching Avro reader) to read records back","Pass a separately-opened readable handle to APIs that need to read","Check whether the object is a writer (writable() is True) before calling read()","Restructure to write fully, close, then read in a second phase"],"exampleFix":"// before\nwith writer:\n    write_rows(writer)\n    data = writer.read()  # raises UnsupportedOperation\n// after\nwith writer:\n    write_rows(writer)\nwriter.flush()\nwith open(writer._file_handle.name, 'rb') as f:\n    data = f.read()","handlingStrategy":"type-guard","validationCode":"if not obj.readable():\n    raise TypeError('write-only stream: reopen the underlying file to read')","typeGuard":"def is_readable_stream(o):\n    return hasattr(o, 'read') and callable(getattr(o, 'readable', None)) and o.readable()","tryCatchPattern":"try:\n    data = writer.read()\nexcept io.UnsupportedOperation:\n    writer.flush()\n    with open(writer._file_handle.name, 'rb') as f:\n        data = f.read()","preventionTips":["Never pass AvroRowWriter where a readable handle is required","Flush, close, then reopen the file for verification reads","Use writer.writable() / .readable() checks before I/O calls","Separate write and read phases in load pipelines"],"tags":["bigquery","avro","io","file","python"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}