Skip to content

Commit 2efd280

Browse files
authored
fix: make dataset and language optional in HallucinationSample.from_json (#54)
1 parent 5230ee6 commit 2efd280

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎lettucedetect/datasets/hallucination_dataset.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def from_json(cls, json_dict: dict) -> "HallucinationSample":
7676
labels=json_dict["labels"],
7777
split=json_dict["split"],
7878
task_type=json_dict["task_type"],
79-
dataset=json_dict["dataset"],
80-
language=json_dict["language"],
79+
dataset=json_dict.get("dataset", "unknown"),
80+
language=json_dict.get("language", "en"),
8181
context_modality=json_dict.get("context_modality", "prose"),
8282
category=json_dict.get("category"),
8383
subcategory=json_dict.get("subcategory"),

‎tests/test_datasets_pytest.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Pytest tests for the datasets module."""
2+
3+
from lettucedetect.datasets.hallucination_dataset import HallucinationSample
4+
5+
6+
def test_from_json_with_minimal_keys():
7+
"""Test that from_json handles data missing optional dataset/language keys."""
8+
sample = HallucinationSample.from_json(
9+
{
10+
"prompt": "User request: What is AI?\n\nAI is artificial intelligence.",
11+
"answer": "AI stands for Artificial Intelligence.",
12+
"labels": [],
13+
"split": "test",
14+
"task_type": "qa",
15+
}
16+
)
17+
assert sample.prompt == "User request: What is AI?\n\nAI is artificial intelligence."
18+
assert sample.answer == "AI stands for Artificial Intelligence."
19+
assert sample.labels == []
20+
assert sample.split == "test"
21+
assert sample.task_type == "qa"
22+
assert sample.dataset == "unknown"
23+
assert sample.language == "en"
24+
25+
26+
def test_from_json_explicit_dataset_language():
27+
"""Test that explicit dataset and language keys are preserved."""
28+
sample = HallucinationSample.from_json(
29+
{
30+
"prompt": "Test prompt",
31+
"answer": "Test answer",
32+
"labels": [{"start": 0, "end": 4, "label": "intrinsic"}],
33+
"split": "train",
34+
"task_type": "summarization",
35+
"dataset": "cnn_dailymail",
36+
"language": "de",
37+
}
38+
)
39+
assert sample.dataset == "cnn_dailymail"
40+
assert sample.language == "de"

0 commit comments

Comments
 (0)