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): validate extract() against the date fact the engine saw (#152)

breken7206d1b1372b
diff --git a/needle/__init__.py b/needle/__init__.py
index 2f5d722..131b419 100644
--- a/needle/__init__.py
+++ b/needle/__init__.py
@@ -630,6 +630,9 @@ def extract(text: str, schema: type | dict, system: str | None = None,
_track("extract", {"n_tools": 1, "tuned": bool(selected),
"generation": generation})
agent = Needle(tools=[schema], system=system, weights=selected, generation=generation)
+ # Validate against the facts the engine saw, including the automatic
+ # `date:` fact, so a relative date is licensed exactly as in complete().
+ facts = agent._system_text
try:
response = agent._complete(text, max_new_tokens)
finally:
@@ -639,5 +642,5 @@ def extract(text: str, schema: type | dict, system: str | None = None,
return None
arguments = calls[0].get("arguments") or {}
if strict:
- _validate_extraction(text, schema, arguments, response, system)
+ _validate_extraction(text, schema, arguments, response, facts)
return schema(**arguments) if _is_pydantic_model(schema) else arguments
diff --git a/tests/test_grounding.py b/tests/test_grounding.py
index b642b86..2cc2657 100644
--- a/tests/test_grounding.py
+++ b/tests/test_grounding.py
@@ -143,6 +143,19 @@ def test_extract_accepts_a_date_written_with_slashes(stub):
assert invoice.due_date == datetime.date(2024, 12, 31)
+def test_extract_licenses_a_relative_date_against_its_date_fact(stub):
+ import needle
+
+ tomorrow = datetime.date.today() + datetime.timedelta(days=1)
+ text = "Invoice from Acme for the March 2019 order, due tomorrow"
+ stub.envelopes = [_call(tomorrow.isoformat())]
+ assert "validation" not in needle.Needle(tools=[Invoice]).complete(text)
+
+ invoice = needle.extract(text, Invoice)
+
+ assert invoice.due_date == tomorrow
+
+
def test_run_refuses_ungrounded_calls_unless_strict_is_off(stub):
import needle