beam icon indicating copy to clipboard operation
beam copied to clipboard

[Bug]: [Python] WriteToJson silently rounds floats to 10 decimal places

Open jk-shah opened this issue 3 weeks ago • 4 comments

What happened?

beam.io.WriteToJson (and Beam YAML WriteToJson) silently loses float precision: values are rounded to 10 decimal places.

input written
0.1234567890123 0.123456789
3.3333333333333335 3.3333333333
12345678901.5 12345678901.5

There is no warning, so a JSON export doesn't round-trip and differs from the same data written with WriteToParquet, WriteToCsv or json.dumps.

Minimal reproducer (Beam YAML, Beam 2.75.0; WriteToJson on master @ 728ef1b4 has the same code path):

pipeline:
  type: chain
  transforms:
  - type: Create
    config:
      elements:
      - {id: 1, x: 0.1234567890123}
      - {id: 2, x: 3.3333333333333335}
  - type: WriteToJson
    config:
      path: /tmp/c5

python -m apache_beam.yaml.main --yaml_pipeline_file=c5.yaml writes:

{"id":1,"x":0.123456789}
{"id":2,"x":3.3333333333}

Cause

WriteToJson (sdks/python/apache_beam/io/textio.py) writes through the DataFrame API, apache_beam.dataframe.io.to_json -> pandas.DataFrame.to_json, and only forwards the caller's **kwargs. pandas' to_json defaults to double_precision=10, so every float is rounded to 10 decimal places.

Options:

  1. Default double_precision=15 (pandas' maximum) when the caller doesn't pass it. That fixes most cases but still isn't exact for 16-17 significant digits.
  2. Write JSON lines with json.dumps for orient='records', lines=True (the YAML default), which round-trips floats exactly.

At minimum the docstring should mention the rounding and the double_precision kwarg. Happy to send a PR for whichever approach maintainers prefer.

Issue Priority

Priority: 2 (default / most bugs should be filed as P2)

Issue Components

  • [x] Component: Python SDK
  • [ ] Component: Java SDK
  • [ ] Component: Go SDK
  • [ ] Component: Typescript SDK
  • [x] Component: IO connector
  • [x] Component: Beam YAML
  • [ ] Component: Beam examples
  • [ ] Component: Beam playground
  • [ ] Component: Beam katas
  • [ ] Component: Website
  • [ ] Component: Infrastructure
  • [ ] Component: Spark Runner
  • [ ] Component: Flink Runner
  • [ ] Component: Prism Runner
  • [ ] Component: Twister2 Runner
  • [ ] Component: Hazelcast Jet Runner
  • [ ] Component: Google Cloud Dataflow Runner

jk-shah avatar Nov 09 '26 06:11 jk-shah

I’d like to work on this. Between defaulting double_precision=15 and switching to json.dumps for orient='records', lines=True, which would maintainers prefer? @jk-shah, are you planning to send a PR, or is it okay if I take it?

Abhi2939 avatar Nov 09 '26 11:11 Abhi2939

Please take it! Thanks!

jk-shah avatar Nov 10 '26 05:11 jk-shah

Thanks @jk-shah, I'll pick this up.

My plan, unless maintainers prefer otherwise:

  1. Reproduce on master with the YAML example from the description, plus a plain Python beam.io.WriteToJson repro.
  2. For orient='records', lines=True (the YAML default), write each row with json.dumps so floats round-trip exactly, instead of going through DataFrame.to_json. I'll check that output for NaN/None, timestamps and nested values matches what the pandas path produces today, so this doesn't change anything besides float precision.
  3. For the other orients that still go through pandas, default double_precision=15 when the caller doesn't pass it, and note the remaining limit for 16-17 significant digits in the docstring.
  4. Add tests that round-trip values like 0.1234567890123 and 3.3333333333333335, and document the double_precision kwarg.

@jk-shah or any maintainer: if you'd rather have only the double_precision=15 default (smaller change) or only the json.dumps path, let me know and I'll adjust before opening the PR.

Abhi2939 avatar Nov 10 '26 09:11 Abhi2939

After tracing the code, an exact json.dumps path would need changes to the shared _WriteToPandasFileSink and has parity gaps with pandas (timestamps as epoch ms, NaT/NaN as null). I’ll open a PR first with the double_precision=15 default plus docs and tests, which fixes silent loss of values like 1e-11, and can follow up on exact round-tripping if maintainers want it.

Abhi2939 avatar Nov 10 '26 10:11 Abhi2939