apache/beam · error · ValueError

Expected 0 or 1, got %s

Error message

Expected 0 or 1, got %s

What it means

BoolCoder's decode_from_stream expects the wire byte for a boolean to be exactly 0x00 (False) or 0x01 (True). Any other byte means the stream is not a valid boolean encoding — usually corrupted, truncated, or misaligned data — so it raises ValueError('Expected 0 or 1, got %s').

Source

Thrown at sdks/python/apache_beam/coders/coder_impl.py:726

  def decode(self, encoded):
    return encoded


class BooleanCoderImpl(CoderImpl):
  """For internal use only; no backwards-compatibility guarantees.

  A coder for bool objects."""
  def encode_to_stream(self, value, out, nested):
    out.write_byte(1 if value else 0)

  def decode_from_stream(self, in_stream, nested):
    value = in_stream.read_byte()
    if value == 0:
      return False
    elif value == 1:
      return True
    raise ValueError("Expected 0 or 1, got %s" % value)

  def encode(self, value):
    return b'\x01' if value else b'\x00'

  def decode(self, encoded):
    value = ord(encoded)
    if value == 0:
      return False
    elif value == 1:
      return True
    raise ValueError("Expected 0 or 1, got %s" % value)

  def estimate_size(self, unused_value, nested=False):
    # Note that booleans are encoded the same way regardless of nesting.
    return 1


class MapCoderImpl(StreamCoderImpl):

View on GitHub (pinned to 12126d8942)

Solutions

  1. Regenerate the data with the same Beam version rather than decoding stale/cross-version payloads.
  2. Check for a custom coder consuming too few/many bytes upstream, desynchronizing the stream.
  3. Clear cached/staged state directories and rerun the pipeline.
  4. Validate the source data file/stream integrity before decoding.
Defensive patterns

Strategy: try-catch

Validate before calling

def is_valid_bool_payload(b: bytes) -> bool:
    return isinstance(b, (bytes, bytearray)) and len(b) == 1 and b[0] in (0, 1)

Try / catch

try:
    flag = coder.decode_from_stream(in_stream, nested=True)
except ValueError as e:
    logger.error("Stream desync while reading bool: %s", e)
    raise DataCorruptionError from e

Prevention

When it happens

Trigger: decode_from_stream reads a byte from the input stream whose value is neither 0 nor 1: reading a stream produced by a different coder/version, reading past a record boundary (desync), or decoding corrupted files.

Common situations: Resuming pipelines from staged data written by another Beam version; custom coders that mis-encode lengths causing the boolean decoder to land mid-record; manually hand-crafted or edited encoded blobs.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/118e375a6e5472e2. Report an issue: GitHub.