{"record":{"id":"1097e16c870bf3d3","repo":"apache/beam","slug":"jsonrowwriter-is-not-readable","errorCode":null,"errorMessage":"JsonRowWriter is not readable","messagePattern":"JsonRowWriter is not readable","errorType":"exception","errorClass":"io.UnsupportedOperation","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/gcp/bigquery_tools.py","lineNumber":1487,"sourceCode":"    \"\"\"\n    if not file_handle.writable():\n      raise ValueError(\"Output stream must be writable\")\n\n    self._file_handle = file_handle\n    self._coder = RowAsDictJsonCoder()\n\n  def close(self):\n    self._file_handle.close()\n\n  @property\n  def closed(self):\n    return self._file_handle.closed\n\n  def flush(self):\n    self._file_handle.flush()\n\n  def read(self, size=-1):\n    raise io.UnsupportedOperation(\"JsonRowWriter is not readable\")\n\n  def tell(self):\n    return self._file_handle.tell()\n\n  def writable(self):\n    return self._file_handle.writable()\n\n  def write(self, row):\n    return self._file_handle.write(self._coder.encode(row) + b'\\n')\n\n\nclass AvroRowWriter(io.IOBase):\n  \"\"\"\n  A writer which provides an IOBase-like interface for writing table rows\n  (represented as dicts) as Avro records.\n  \"\"\"\n  def __init__(self, file_handle, schema):\n    \"\"\"Initialize an AvroRowWriter.","sourceCodeStart":1469,"sourceCodeEnd":1505,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L1469-L1505","documentation":"JsonRowWriter wraps a file opened for writing JSON rows for BigQuery loads; its read() raises io.UnsupportedOperation('JsonRowWriter is not readable') because a write-only writer cannot serve reads. It signals an attempt to read from a stream that only supports writing.","triggerScenarios":"Calling .read() (or passing the writer where a readable file-like object is expected) on a JsonRowWriter instance; e.g. mistakenly handing the writer to code that reads records back.","commonSituations":"Confusing the writer with the reader class; code written for a real file object reused on the writer; debugging code attempting to inspect written contents directly from the writer instead of reopening the file.","solutions":["Reopen the underlying file path with open(path, 'rb') (or the reader class) to read the written JSON records","Use the corresponding reader (e.g. JsonReader) instead of the writer","Access writer._file_handle only if you know it's opened in a read-capable mode","Restructure code so reading happens after closing the writer, on a fresh handle"],"exampleFix":"// before\nrows = writer.read()  # raises UnsupportedOperation\n// after\nwriter.flush()\nwith open(writer._file_handle.name, 'r') as f:\n    rows = [json.loads(line) for line in f]","handlingStrategy":"try-catch","validationCode":"if getattr(obj, 'writable', lambda: False)() and not getattr(obj, 'readable', lambda: True)():\n    raise TypeError('stream is write-only; reopen the 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, 'r') as f:\n        data = f.read()","preventionTips":["Treat *RowWriter objects as write-only by contract","Read back via the matching reader class or by reopening the file path","Check .readable() before calling read() on any file-like object","Close the writer before reading its output"],"tags":["bigquery","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"}