Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ with ProoflineClient() as client:
# Continue with cursor=page.next_cursor when it is not None.
```

Each response returns the original text, decoded rows, dataset version, settled horizon, and an optional next cursor. Each CSV page includes its own header and keeps cells as strings, including nested JSON cells. NDJSON pages retain JSON values. To read additional pages, repeat the call with the returned cursor and the same dataset, window, format, and grouping. A missing next cursor marks the last page.
Each response returns the original text, decoded rows, dataset version, settled horizon, and an optional next cursor. Each CSV page includes its own header and keeps cells as strings, including nested JSON cells. NDJSON pages retain JSON values. If an object repeats a key, even inside a nested object, the SDK rejects the entire page with `urn:proofline:sdk:invalid-response` to avoid silently losing a value. To read additional pages, repeat the call with the returned cursor and the same dataset, window, format, and grouping. A missing next cursor marks the last page.

`iter_export_pages` follows continuation cursors lazily while keeping the same organization, dataset, window, format, and grouping on every request.

Expand Down
11 changes: 10 additions & 1 deletion proofline/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class ExportPage:
next_cursor: str | None


def _unique_object(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
# Check before a duplicate can silently replace a field such as dataset_version.
obj = dict(pairs)
if len(obj) != len(pairs):
raise ValueError("Duplicate NDJSON object key.")
return obj


def decode_export(
response: httpx2.Response, format: ExportFormat, dataset: ExportDataset
) -> ExportPage:
Expand All @@ -55,7 +63,8 @@ def decode_export(
raise ValueError
rows = [dict(zip(columns, row, strict=True)) for row in records]
else:
rows = [json.loads(line) for line in text.split("\n") if line.strip()]
decoder = json.JSONDecoder(object_pairs_hook=_unique_object)
rows = [decoder.decode(line) for line in text.split("\n") if line.strip()]
if any(
not isinstance(row, dict) or str(row.get("dataset_version")) != version for row in rows
):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,36 @@ def decode(index: int) -> str:
with ThreadPoolExecutor(max_workers=4) as pool:
assert set(pool.map(decode, range(12))) == {str(before)}
assert csv.field_size_limit() == before


@pytest.mark.parametrize(
"line",
[
'{"dataset_version":2,"dataset_version":1}',
'{"dataset_version":1,"claim":"first","claim":"second"}',
r'{"dataset_version":1,"items":[{"claim":null,"cl\u0061im":false}]}',
],
ids=["masked-version", "overwritten-field", "escaped-nested-key"],
)
def test_ndjson_rejects_duplicate_keys(line: str) -> None:
reply = response(line)
reply.headers["Content-Type"] = "application/x-ndjson"
with pytest.raises(ProoflineError) as caught:
decode_export(reply, "ndjson", "reviews")
assert caught.value.type == "urn:proofline:sdk:invalid-response"
assert caught.value.status == 200


def test_ndjson_allows_reused_keys_in_distinct_objects() -> None:
text = (
'{"dataset_version":1,"items":[{"id":1},{"id":2,"ID":3}]}\r\n'
'{"dataset_version":1,"items":[]}\n'
)
reply = response(text)
reply.headers["Content-Type"] = "application/x-ndjson"
page = decode_export(reply, "ndjson", "reviews")
assert page.text == text
assert page.rows == [
{"dataset_version": 1, "items": [{"id": 1}, {"id": 2, "ID": 3}]},
{"dataset_version": 1, "items": []},
]
34 changes: 34 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,40 @@ async def next_export(iterator: Any) -> Any:
return await anext(iterator) if hasattr(iterator, "__anext__") else next(iterator)


@MODES
async def test_export_iterator_rejects_ambiguous_ndjson_page(mode: str) -> None:
def respond(request: Request, index: int) -> Reply:
body = b'{"dataset_version":1,"claim":"ok"}\n'
if index:
body += b'{"dataset_version":1,"claim":"first","claim":"second"}\n'
return Reply(
body=body,
headers={
"Content-Type": "application/x-ndjson",
"X-Proofline-Export-Version": "1",
"X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z",
"X-Proofline-Export-Next-Cursor": f"next-{index}",
},
)

with Server(respond) as server:
async with session(mode, server.url) as client:
pages = client.iter_export_pages(
ORG,
"reviews",
from_time="2026-10-01T00:00:00Z",
to_time="2026-10-25T00:00:00Z",
format="ndjson",
)
first = await next_export(pages)
assert first.rows == [{"dataset_version": 1, "claim": "ok"}]
with pytest.raises(ProoflineError) as raised:
await next_export(pages)
assert raised.value.type == "urn:proofline:sdk:invalid-response"
assert raised.value.status == 200
assert len(server.requests) == 2


@MODES
@pytest.mark.parametrize("format", ["csv", "ndjson"])
async def test_export_iterator_preserves_filters_and_streams_empty_pages(
Expand Down