{"record":{"id":"2a86a203f15fcc25","repo":"apache/beam","slug":"cannot-convert-the-micro-seconds-to-s","errorCode":null,"errorMessage":"cannot convert the micro seconds to %s","messagePattern":"cannot convert the micro seconds to (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/utils/proto_utils.py","lineNumber":130,"sourceCode":"  msg = struct_pb2.Struct()\n  for key, value in kwargs.items():\n    msg[key] = value  # pylint: disable=unsubscriptable-object, unsupported-assignment-operation\n  return msg\n\n\ndef from_micros(cls: type[TimeMessageT], micros: int) -> TimeMessageT:\n  result = cls()\n  if isinstance(result, duration_pb2.Duration):\n    result.FromMicroseconds(micros)\n    return result\n  # Protobuf 5.x enforces a maximum timestamp value less than the Beam\n  # maximum allowable timestamp, so we cannot use the built-in conversion.\n  elif isinstance(result, timestamp_pb2.Timestamp):\n    result.seconds = micros // _SECONDS_TO_MICROS\n    result.nanos = (micros % _SECONDS_TO_MICROS) * _MICROS_TO_NANOS\n    return result\n  else:\n    raise RuntimeError('cannot convert the micro seconds to %s' % cls)\n\n\ndef to_micros(value: Union[duration_pb2.Duration, timestamp_pb2.Timestamp]):\n  if isinstance(value, duration_pb2.Duration):\n    return value.ToMicroseconds()\n  # Protobuf 5.x enforces a maximum timestamp value less than the Beam\n  # maximum allowable timestamp, so we cannot use the built-in conversion.\n  elif isinstance(value, timestamp_pb2.Timestamp):\n    micros = value.seconds * _SECONDS_TO_MICROS\n    return micros + (value.nanos // _MICROS_TO_NANOS)\n  else:\n    raise RuntimeError('cannot convert %s to micro seconds' % value)\n\n\ndef to_Timestamp(time: Union[int, float]) -> timestamp_pb2.Timestamp:\n  \"\"\"Convert a float returned by time.time() to a Timestamp.\n  \"\"\"\n  seconds = int(time)","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/utils/proto_utils.py#L112-L148","documentation":"from_micros converts microseconds into a protobuf Duration or Timestamp. If the target class is neither duration_pb2.Duration nor timestamp_pb2.Timestamp, Beam raises RuntimeError because the conversion is undefined for other proto types.","triggerScenarios":"Calling proto_utils.from_micros(micros, SomeClass) where SomeClass is not duration_pb2.Duration or timestamp_pb2.Timestamp (Protobuf 5.x enforces a max timestamp, but the else branch is about the type itself).","commonSituations":"Refactoring code to a custom timestamp proto; passing a Python datetime class or wrong protobuf message type by mistake; API changes swapping the expected proto type.","solutions":["Pass duration_pb2.Duration or timestamp_pb2.Timestamp as the target class","Check that the variable holding the class is not None or shadowed","Convert manually with result.FromMicroseconds(micros) for supported protos"],"exampleFix":"// before\nproto_utils.from_micros(micros, datetime)\n// after\nproto_utils.from_micros(micros, timestamp_pb2.Timestamp)","handlingStrategy":"type-guard","validationCode":"if cls not in (duration_pb2.Duration, timestamp_pb2.Timestamp):\n    raise ValueError(f'unsupported proto class {cls}')","typeGuard":"def is_convertible_proto(cls):\n    return cls in (duration_pb2.Duration, timestamp_pb2.Timestamp)","tryCatchPattern":"try:\n    ts = proto_utils.from_micros(micros, cls)\nexcept RuntimeError as e:\n    logging.error('bad proto class: %s', e)\n    ts = timestamp_pb2.Timestamp()  # or re-raise","preventionTips":["Only pass duration_pb2.Duration or timestamp_pb2.Timestamp","Keep type annotations on helper functions that forward classes","Add a unit test covering the conversion target type"],"tags":["protobuf","timestamp","type-mismatch","python"],"backgroundTag":"type-mismatch","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"}