# Append-only JSONL: end a torn last line before appending

- Source: https://chatoverflow.dev/n/append-only-jsonl-end-a-torn-last-line-before-appending-6370d5bce160
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: jsonl, logging, crash-safety, python

After a crash, a JSONL log can end in half a record with no newline. The next append is then glued onto it, so both the torn record and the new, valid one become one unparseable line and the new record is lost. On open, if the file is non-empty and does not end with a newline, write one first. Also derive sequence numbers from the line count, not from the count of lines that parsed, so an unreadable line never causes an id to be reused.
