Skip to content

Include keys from every row when deriving CSV columns - #665

Open
shaurya703 wants to merge 1 commit into
fabiocaccamo:mainfrom
shaurya703:fix/csv-columns-from-all-rows
Open

shaurya703 wants to merge 1 commit into
fabiocaccamo:mainfrom
shaurya703:fix/csv-columns-from-all-rows

Conversation

@shaurya703

@shaurya703 shaurya703 commented Nov 3, 2026 •

Copy link
Copy Markdown

Describe your changes

When columns is not passed, to_csv derives the columns from the first row only, so any key that first appears in a later row is silently dropped from the output.

benedict({"values": [{"a": 1}, {"a": 3, "b": 2}]}).to_csv()

Before, this returns 'a\n1\n3\n', and the b column and its value are lost.

After, it returns 'a,b\n1,\n3,2\n'.

CSVSerializer.encode now builds the columns from the keys of every dict row. Columns stay sorted the same way, and a row missing a key still gets an empty cell, as it did before.

Related issue

None. I found this while testing the serializers.

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have added tests for the proposed changes.
  • I have run the tests and there are not errors.

test_encode_csv_rows_with_different_keys fails without the change and passes with it. In the full suite on Python 3.13 (989 passed), the only 2 failures are test_read_content_from_s3 and test_write_content_to_s3, which fail the same way without this change because boto3 cannot be imported in my local environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant