Skip to content

Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.

fix(grounding): stop reading a slash date's day or month as a year (#126)

brekene0a7a55a01ec
diff --git a/needle/__init__.py b/needle/__init__.py
index 7b7f15d..516130b 100644
--- a/needle/__init__.py
+++ b/needle/__init__.py
@@ -349,7 +349,10 @@ def _source_years(text):
rf"\b{months}\s+\d{{1,2}}(?:st|nd|rd|th)?\s*,?\s+(\d{{1,4}})(?![0-9A-Za-z])",
rf"\b{months}[\s,]+(\d{{3,4}})(?![0-9A-Za-z])",
r"\byear\s+(\d{1,4})(?![0-9A-Za-z])",
- r"(?<![0-9])(\d{1,4})(?=[-/]\d{1,2}[-/]\d{1,2}(?![0-9]))",
+ # Year-first numeric dates only (2024-03-15, 2024/03/15). A day- or
+ # month-first date such as 5/6/24 must not mint its leading component
+ # as a year: no ISO argument can ever match it, so every date would fail.
+ r"(?<![0-9])(\d{4})(?=[-/]\d{1,2}[-/]\d{1,2}(?![0-9]))",
]
lowered = text.lower()
return {int(match.group(1)) for pattern in patterns
diff --git a/tests/test_grounding.py b/tests/test_grounding.py
index a65d010..195fb42 100644
--- a/tests/test_grounding.py
+++ b/tests/test_grounding.py
@@ -122,6 +122,27 @@ def test_input_without_a_year_is_not_checked(stub):
assert "validation" not in response
+def test_a_slash_date_does_not_mint_a_year_from_its_day_or_month(stub):
+ import needle
+
+ stub.envelopes = [_call("2024-05-06")]
+ agent = needle.Needle(tools=[Invoice])
+ response = agent.complete("Send an invoice to Acme due on 5/6/24")
+
+ assert "validation" not in response
+ assert needle._source_years("due on 5/6/24") == set()
+ assert needle._source_years("due on 2024-05-06") == {2024}
+
+
+def test_extract_accepts_a_date_written_with_slashes(stub):
+ import needle
+
+ stub.envelopes = [_call("2024-12-31")]
+ invoice = needle.extract("Invoice from Acme Corp due 12/31/24", Invoice)
+
+ assert invoice.due_date == datetime.date(2024, 12, 31)
+
+
def test_run_refuses_ungrounded_calls_unless_strict_is_off(stub):
import needle