Skip to content

Reject duplicate object keys in NDJSON exports - #170

Open
DariusJDebugs wants to merge 2 commits into
prooflinedev:mainfrom
DariusJeffriesLabs:fix/reject-ambiguous-ndjson
Open

DariusJDebugs wants to merge 2 commits into
prooflinedev:mainfrom
DariusJeffriesLabs:fix/reject-ambiguous-ndjson

Conversation

@DariusJDebugs

@DariusJDebugs DariusJDebugs commented Dec 5, 2026 •

Copy link
Copy Markdown

A row can declare dataset_version twice, first as 2 and then as 1. With a version 1 header the old decoder accepts the row because Python silently keeps the final value. The same behavior can discard nested review fields.

The decoder now inspects decoded key/value pairs for each object. If a dictionary has fewer entries than the pair list, a name appeared twice. Checking at this point catches equivalent JSON escapes and keeps the comparison local to each object. The SDK returns its existing invalid-response error without echoing field values. Earlier pages remain available, but the ambiguous page never reaches the caller.

This intentionally changes behavior for producers sending duplicate names. The hook adds temporary pair storage per object. CSV handling and public signatures are unchanged.

Verification

In the SDK development environment run

python -m pytest -q -k 'duplicate_keys or allows_reused or ambiguous_ndjson'
python scripts/check.py

With the new tests and original decoder, five cases fail and the valid control passes. All six pass with the fix. Both sync and async tests use a local HTTP server to exercise pagination. All 242 tests passed, along with type checks, generation checks and installed package smoke tests. A separate local check rejected 500 repeated-name cases and preserved 500 distinct-name controls.

Review and performance

Proofline’s first code review on the fork reported no findings. The incremental review of the optimized decoder also reported no findings. Both reviews covered code only, so I checked runtime behavior separately. I kept the object-level check because the negative cases fail on the original decoder and the valid control preserves both rows. I changed how often the decoder is constructed after timing the first implementation.

On one local synthetic page of 1,000 rows, the original permissive parser took 2.11 ms, constructing a strict decoder per row took 4.44 ms, and reusing a strict decoder per page took 2.76 ms. Duplicate detection still has a cost. These are local measurements, excluding network time.

To reproduce, run this in each checkout with the SDK dependencies installed. The intermediate version is c22ce1c.

import httpx2, json, timeit, statistics
from proofline.exports import decode_export

rows = "\n".join(
    json.dumps(
        dict(
            dataset_version=1,
            review_id=i,
            items=[dict(id=j, claim="test") for j in range(3)],
        )
    )
    for i in range(1000)
)
r = httpx2.Response(
    200,
    content=rows.encode(),
    headers={
        "content-type": "application/x-ndjson",
        "x-proofline-export-version": "1",
        "x-proofline-export-settled-until": "2026-11-16T00:00:00Z",
    },
)


def f():
    assert len(decode_export(r, "ndjson", "reviews").rows) == 1000


f()
print(
    len(rows),
    round(statistics.median(timeit.repeat(f, number=20, repeat=9)) * 50, 2),
    "ms/page",
)

AI assistance was used to develop, test, and document this change.

@dboles99

dboles99 commented Dec 5, 2026

Copy link
Copy Markdown

Approve.

A repeated key, including one inside a nested object, now rejects the whole page with urn:proofline:sdk:invalid-response through object_pairs_hook, so no value is silently dropped, and the README states the behavior change. It also brings NDJSON in line with CSV, which already rejects duplicate header columns. The six new tests pass locally; no changes needed.

This branch has not been deployed

No deployments
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.

2 participants