===== ISTEM =====
Our nightly export job crashed. Here is the log:

```
2026-09-18 02:00:01 INFO  export: starting nightly export (batch=500)
2026-09-18 02:00:03 INFO  export: fetched 1500 rows from orders
2026-09-18 02:00:03 INFO  export: writing batch 1/3
2026-09-18 02:00:04 INFO  export: writing batch 2/3
2026-09-18 02:00:04 ERROR export: unhandled exception
Traceback (most recent call last):
  File "/srv/jobs/export.py", line 88, in run
    write_batch(rows[i:i + BATCH], out)
  File "/srv/jobs/export.py", line 41, in write_batch
    out.write(f"{r['id']},{r['total']:.2f},{r['currency']}\n")
KeyError: 'currency'
```

What is the root cause, and at which line of our code does it fail?

===== HAM YANIT =====
The root cause of the crash in your nightly export job is a `KeyError` when attempting to access the 'currency' key in the dictionary `r`. This means that at some point in the code, a dictionary is accessed that does not contain the key 'currency'.

The error occurs at line 41 in the `write_batch` function within your `export.py` script. Here is the specific line where the error happens:

```python
out.write(f"{r['id']},{r['total']:.2f},{r['currency']}\n")
```

To resolve this issue, you need to ensure that all dictionaries in the list `rows` contain the 'currency' key before attempting to access it. You can add a check to see if the key exists before accessing it, like so:

```python
if 'currency' in r:
    out.write(f"{r['id']},{r['total']:.2f},{r['currency']}\n")
else:
    out.write(f"{r['id']},{r['total']:.2f},Unknown\n")  # or handle the missing key appropriately
```

This modification will prevent the `KeyError` and ensure that the export job can continue running even if some rows lack the 'currency' key.