Fix convert_to_parquet crash on empty struct fields#1
Merged
iarata merged 1 commit intocarp-dk:mainfrom Mar 26, 2026
Merged
Conversation
Flatten nested dicts via pd.json_normalize before writing to Parquet.
This avoids PyArrow inferring empty struct types (e.g. "metadata": {})
which Parquet cannot represent. Also fixes schema drift across batches
by using pa.unify_schemas instead of silent cast-and-pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
convert_to_parquet()crashes when the JSON data contains empty dict fields (e.g."metadata": {}).PyArrow infers
{}asstruct<>(a struct with zero child fields), which Parquet cannot represent. This affects data types likecompletedapptaskandimagethat contain emptymetadatadicts.Error log and stacktrace
Root cause
Some items in the JSON (e.g.
completedapptask,image) contain fields like:pa.Table.from_pylist()infers this asmetadata: struct<>, whichpq.ParquetWriterrejects because Parquet has no representation for an empty struct.Additionally, the previous schema handling silently swallowed cast failures (
except: pass), which could lead to silent data loss when schemas drifted across batches (e.g.locationtype with varying fields).Fix
pa.Table.from_pylist(buffer)withpd.json_normalize(buffer)+pa.Table.from_pandas(df). This flattens nested dicts into dot-separated columns (e.g.measurement.data.steps), avoiding empty struct inference entirely.pa.unify_schemas(), which merges schemas across batches so new columns get nulls in earlier rows instead of being silently dropped.