{"record":{"id":"efed037bb6836dfe","repo":"apache/beam","slug":"file-object-must-be-at-position-0-but-was-d","errorCode":null,"errorMessage":"File object must be at position 0 but was %d","messagePattern":"File object must be at position 0 but was (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/filesystem.py","lineNumber":165,"sourceCode":"      compression_type=CompressionTypes.GZIP,\n      read_size=DEFAULT_READ_BUFFER_SIZE):\n    if not fileobj:\n      raise ValueError('File object must not be None')\n\n    if not CompressionTypes.is_valid_compression_type(compression_type):\n      raise TypeError(\n          'compression_type must be CompressionType object but '\n          'was %s' % type(compression_type))\n    if compression_type in (CompressionTypes.AUTO,\n                            CompressionTypes.UNCOMPRESSED):\n      raise ValueError(\n          'Cannot create object with unspecified or no compression')\n\n    self._file = fileobj\n    self._compression_type = compression_type\n\n    if self._file.tell() != 0:\n      raise ValueError(\n          'File object must be at position 0 but was %d' % self._file.tell())\n    self._uncompressed_position = 0\n    self._uncompressed_size: Optional[int] = None\n\n    if self.readable():\n      self._read_size = read_size\n      self._read_buffer = io.BytesIO()\n      self._read_position = 0\n      self._read_eof = False\n\n      self._initialize_decompressor()\n    else:\n      self._decompressor = None\n\n    if self.writeable():\n      self._initialize_compressor()\n    else:\n      self._compressor = None","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/filesystem.py#L147-L183","documentation":"CompressedFile assumes it starts reading/writing a fresh compressed stream, so the wrapped file object must be positioned at byte 0. If file.tell() reports any other offset, the compressed stream would be corrupted/misread, so __init__ raises ValueError with the offending position.","triggerScenarios":"Opening a file, calling fh.seek(n) or fh.read(...) and then wrapping it in CompressedFile; reusing a partially consumed file handle.","commonSituations":"Reusing a file object after an earlier read pass, retry logic that rewinds improperly, or pipes/stream objects whose position advanced before construction.","solutions":["Call fh.seek(0) before constructing CompressedFile.","Open a fresh file handle for the CompressedFile instead of reusing one.","If resuming mid-stream is required, use a random-access reader (e.g. FileSystems.open with seek support) rather than CompressedFile."],"exampleFix":"// before\nfh.read(10)\nreader = CompressedFile(fh, compression_type=CompressionTypes.GZIP)\n// after\nfh.seek(0)\nreader = CompressedFile(fh, compression_type=CompressionTypes.GZIP)","handlingStrategy":"validation","validationCode":"if fh.tell() != 0:\n    fh.seek(0)\nreader = CompressedFile(fh, compression_type=CompressionTypes.GZIP)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always seek(0) or open a fresh handle before wrapping in CompressedFile.","Avoid reusing partially consumed file handles across pipeline stages."],"tags":["python","apache-beam","io","file-position"],"backgroundTag":"invalid-state-transition","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"}