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(tools): keep a docstring argument's wrapped lines in its description (#158)

kevine6f23229cb5f
diff --git a/needle/agent/tools.py b/needle/agent/tools.py
index 821973a..054436a 100644
--- a/needle/agent/tools.py
+++ b/needle/agent/tools.py
@@ -112,13 +112,29 @@ def _parse_doc(doc):
while i < len(lines) and lines[i].strip().lower() not in heads:
desc.append(lines[i].strip())
i += 1
+ head = _indent(lines[i]) if i < len(lines) else 0
+ name, depth = None, 0
for line in lines[i + 1:]:
+ if not line.strip():
+ continue
+ # A section after the arguments ("Returns:", "Raises:") ends them.
+ if _indent(line) <= head and re.match(r"\s*[A-Za-z][\w ]*:\s*$", line):
+ break
m = re.match(r"\s+(\w+)\s*(?:\([^)]*\))?\s*:\s*(.+)", line)
- if m:
- args[m.group(1)] = m.group(2).strip()
+ if m and (name is None or _indent(line) <= depth):
+ name, depth = m.group(1), _indent(line)
+ args[name] = m.group(2).strip()
+ elif name and _indent(line) > depth:
+ # A Google-style description wraps onto lines indented under
+ # its argument; those lines are the rest of it.
+ args[name] += " " + line.strip()
return " ".join(w for w in desc if w).strip(), args
+def _indent(line):
+ return len(line) - len(line.lstrip())
+
+
def build_schema(fn: Callable) -> dict:
signature = inspect.signature(fn)
try:
diff --git a/tests/test_tools.py b/tests/test_tools.py
index b920bd5..fc193c7 100644
--- a/tests/test_tools.py
+++ b/tests/test_tools.py
@@ -135,6 +135,45 @@ def test_docstring_description_falls_back_to_args():
assert props["city"]["description"] == "the city to look up"
+def test_docstring_arg_description_keeps_its_wrapped_lines():
+ # A Google-style description that wraps continues on lines indented under
+ # its argument. Only the first line reached the schema, so the constraint
+ # on the second never reached the model.
+ def f(time: str, label: str = ""):
+ """Set an alarm.
+
+ Args:
+ time: The alarm time in 24-hour HH:MM form, for example 07:30
+ or 18:45. Never a relative time like "in ten minutes".
+ label (str): A short name
+ shown when it rings.
+
+ Returns:
+ dict: The alarm that was set.
+ """
+
+ props = build_schema(f)["parameters"]["properties"]
+ assert props["time"]["description"] == (
+ 'The alarm time in 24-hour HH:MM form, for example 07:30 '
+ 'or 18:45. Never a relative time like "in ten minutes".')
+ assert props["label"]["description"] == "A short name shown when it rings."
+
+ # The Returns section is not arguments: an entry there that shares a
+ # parameter's name overwrote the parameter's description.
+ def g(count: int):
+ """Count things.
+
+ Args:
+ count: How many to count.
+
+ Returns:
+ count: How many were counted.
+ """
+
+ props = build_schema(g)["parameters"]["properties"]
+ assert props["count"]["description"] == "How many to count."
+
+
def test_pydantic_model_schema():
import pydantic