Skip to content
Closed
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
12 changes: 11 additions & 1 deletion assayer/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

from assayer.models import ModelResult

_FIELDNAMES = [
"model",
"output",
"tokens_input",
"tokens_output",
"latency_seconds",
"cost_usd",
"error",
]


def _to_dict(result: ModelResult) -> dict:
return {
Expand All @@ -23,7 +33,7 @@ def export(results: list[ModelResult], path: str) -> None:

if dest.suffix.lower() == ".csv":
with dest.open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=list(records[0].keys()))
writer = csv.DictWriter(f, fieldnames=_FIELDNAMES)
writer.writeheader()
writer.writerows(records)
else:
Expand Down
22 changes: 21 additions & 1 deletion tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
from assayer.exporter import export
from assayer.models import ModelResult

_EXPECTED_FIELDS = {"model", "output", "tokens_input", "tokens_output", "latency_seconds", "cost_usd", "error"}
_EXPECTED_FIELDS = {
"model",
"output",
"tokens_input",
"tokens_output",
"latency_seconds",
"cost_usd",
"error",
}


def _results() -> list[ModelResult]:
Expand Down Expand Up @@ -93,6 +101,18 @@ def test_export_csv_has_all_headers(tmp_path):
assert set(reader.fieldnames) == _EXPECTED_FIELDS


def test_export_csv_empty_results_writes_headers(tmp_path):
path = tmp_path / "results.csv"
export([], str(path))

with path.open(encoding="utf-8") as f:
reader = csv.DictReader(f)
rows = list(reader)

assert set(reader.fieldnames or []) == _EXPECTED_FIELDS
assert rows == []


def test_export_csv_case_insensitive_extension(tmp_path):
path = tmp_path / "results.CSV"
export(_results(), str(path))
Expand Down