From 68263fcf5079f57d4a8d95f8099d34a9a8a2a918 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Mon, 30 Sep 2024 15:05:32 -0700 Subject: [PATCH] Require bytes or strings. --- sdks/python/apache_beam/yaml/yaml_io.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sdks/python/apache_beam/yaml/yaml_io.py b/sdks/python/apache_beam/yaml/yaml_io.py index e8931878d402..82288841fe1a 100644 --- a/sdks/python/apache_beam/yaml/yaml_io.py +++ b/sdks/python/apache_beam/yaml/yaml_io.py @@ -264,12 +264,13 @@ def convert_to_bytes(row): output = getattr(row, field_names[0]) if isinstance(output, bytes): return output - try: - return str(output).encode('utf-8') - except Exception as e: + elif isinstance(output, str): + return output.encode('utf-8') + else: raise ValueError( f"Cannot encode payload for WriteToPubSub. " - f"Must be valid string or bytes object. {e}") + f"Expected be valid string or bytes object, " + f"got {repr(output)} of type {type(output}.") return convert_to_bytes elif format == 'JSON':