Bug: documents saved with no extractions cannot be loaded back (TypeError)
Describe the overall issue and situation
AnnotatedDocument.extractions defaults to None. When such a document is saved with io.save_annotated_documents, the JSONL line contains "extractions": null. Loading the file back with io.load_annotated_documents_jsonl then crashes in data_lib.dict_to_annotated_document, which iterates over adoc_dic.get("extractions", []). The key exists, so .get returns None instead of the default. A document with no extractions (e.g. nothing found in the text) can be saved but not reloaded.
Expected behavior
Save then load round-trips: the document is loaded with no extractions.
Actual behavior
File ".../langextract/data_lib.py", line 99, in dict_to_annotated_document
for extractions in adoc_dic.get("extractions", []):
TypeError: 'NoneType' object is not iterable
Steps to reproduce the issue
import pathlib, tempfile
from langextract import io
from langextract.core import data
doc = data.AnnotatedDocument(document_id="d1", text="Nothing to extract here.")
with tempfile.TemporaryDirectory() as tmp:
io.save_annotated_documents(iter([doc]), output_dir=tmp, show_progress=False)
list(io.load_annotated_documents_jsonl(pathlib.Path(tmp) / "data.jsonl", show_progress=False))
Any additional content
Current main. A minimal fix is to treat a null extractions value like a missing one when iterating, and keep None on the restored document. I have it ready with a round-trip regression test in tests/data_lib_test.py (fails before, passes after) and can open a PR once this issue is acknowledged.
Reproduced on current main (3a68580) on Windows 11 with Python 3.11.9 and 3.12.12, using the snippet from the description: TypeError: 'NoneType' object is not iterable at for extractions in adoc_dic.get("extractions", []): in dict_to_annotated_document. The saved line is {"extractions": null, "text": "Nothing to extract here.", "document_id": "d1"}.
@LemonCondor, you wrote that you have a fix and a test ready. Do you still plan to open the PR? If you would rather land yours, say so and I will close mine in its favour; I can also run yours on Windows.
In the meantime I am opening a small PR so there is something to review. It treats null like a missing key when iterating and keeps None on the restored document, as you proposed. The second adoc_dic.get("extractions", []) in the return statement needs the same handling, otherwise the document comes back with [] instead of None. The tests are a dict round trip, a missing-key case, and a save/load JSONL round trip. Total change is about 10 lines plus tests.
One coordination note: #577 (for #576) rewrites the loop in the same function, so whichever PR lands second needs a small rebase. Also, visualize() on a document with extractions=None raises ValueError: annotated_doc must contain extractions to visualise., in memory and after this fix from disk; I am leaving that as is.
Thanks for reproducing it and for the thorough PR. #584 is the fix I had in mind (including the second .get in the return), so please go ahead with yours; I won't open a competing one. I've looked over the diff and it looks good to me.