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: improve error handling in Needle.complete method for needle_complete and JSON parsing

HenryNdubuaku4439dd02824e
diff --git a/needle/__init__.py b/needle/__init__.py
index 0ba6fee..7e8a0de 100644
--- a/needle/__init__.py
+++ b/needle/__init__.py
@@ -102,9 +102,16 @@ class Needle:
def complete(self, text, max_new_tokens=256):
self._bind()
- _lib().needle_complete(text.encode("utf-8"), int(max_new_tokens),
- self._buffer, len(self._buffer))
- response = json.loads(self._buffer.value.decode("utf-8"))
+ rc = _lib().needle_complete(text.encode("utf-8"), int(max_new_tokens),
+ self._buffer, len(self._buffer))
+ if rc < 0:
+ raise RuntimeError(f"needle_complete failed (code {rc})")
+ try:
+ response = json.loads(self._buffer.value.decode("utf-8"))
+ except json.JSONDecodeError as err:
+ raise RuntimeError(
+ f"engine returned an unparseable envelope ({err}); this is an "
+ f"engine bug - please report it with the prompt and schema") from err
if self._weights:
response["confidence"] = None
return response