Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-imports): decode arrow field metadata before writing schema.json (#73251)

Tom Owersaf1d940a328d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/s3/test_writer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/s3/test_writer.py
new file mode 100644
index 000000000000..f7dbb56f49f5
--- /dev/null
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/s3/test_writer.py
@@ -0,0 +1,21 @@
+import json
+
+import pyarrow as pa
+
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.s3.writer import build_schema_dict
+
+
+class TestBuildSchemaDict:
+ def test_field_metadata_is_json_serializable(self) -> None:
+ schema = pa.schema([pa.field("id", pa.int64(), metadata={"comment": "primary key"})])
+
+ schema_dict = build_schema_dict(schema)
+
+ # Would raise "keys must be str ... not bytes" if the bytes metadata wasn't decoded.
+ json.dumps(schema_dict)
+ assert schema_dict["fields"][0]["metadata"] == {"comment": "primary key"}
+
+ def test_field_without_metadata_stays_none(self) -> None:
+ schema = pa.schema([pa.field("id", pa.int64())])
+
+ assert build_schema_dict(schema)["fields"][0]["metadata"] is None
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/s3/writer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/s3/writer.py
index 5134916e1b39..b33465eb2c8f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/s3/writer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/s3/writer.py
@@ -28,6 +28,31 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
ParquetCompression = Literal["gzip", "bz2", "brotli", "lz4", "zstd", "snappy", "none"]
+def _decode_field_metadata(metadata: dict | None) -> dict[str, str] | None:
+ # PyArrow stores field metadata as bytes keys/values; JSON keys can't be bytes.
+ if not metadata:
+ return None
+ return {
+ (key.decode() if isinstance(key, bytes) else key): (value.decode() if isinstance(value, bytes) else value)
+ for key, value in metadata.items()
+ }
+
+
+def build_schema_dict(schema: pa.Schema) -> dict:
+ return {
+ "fields": [
+ {
+ "name": field.name,
+ "type": str(field.type),
+ "nullable": field.nullable,
+ "metadata": _decode_field_metadata(field.metadata),
+ }
+ for field in schema
+ ],
+ "pandas_metadata": schema.pandas_metadata,
+ }
+
+
class S3BatchWriter:
_job: ExternalDataJob
_schema_id: str
@@ -120,18 +145,7 @@ class S3BatchWriter:
schema_path = f"{self._base_folder}/schema.json"
s3_path_without_protocol = strip_s3_protocol(schema_path)
- schema_dict = {
- "fields": [
- {
- "name": field.name,
- "type": str(field.type),
- "nullable": field.nullable,
- "metadata": dict(field.metadata) if field.metadata else None,
- }
- for field in self._schema
- ],
- "pandas_metadata": self._schema.pandas_metadata,
- }
+ schema_dict = build_schema_dict(self._schema)
self._logger.debug(f"Writing schema to {schema_path}")