BUG: get_json_schema crashes on non-string docstring choices
Problem
get_json_schema() raises AttributeError when a Google-style argument description contains numeric, boolean, or null values in its (choices: [...]) block. The JSON is parsed successfully, but every decoded value is then treated as a string. For example, documenting an integer argument with (choices: [0, 1, 3]) prevents schema construction.
This report concerns docstring choices, not Literal unions. Searches for choices/strip and choices/enum did not find the same report; #1195 and #2639 concern the separate type-annotation path.
Steps to reproduce
From a smolagents checkout, save the following as repro.py and run python repro.py. It loads the unchanged helper directly, so no model, credentials, network access, or package installation is needed. An optional first argument allows using a separately fetched copy of the same source file.
import importlib.util
import sys
from pathlib import Path
source = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("src/smolagents/_function_type_hints_utils.py")
spec = importlib.util.spec_from_file_location("type_hints", source)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
def choose_retries(retries: int) -> int:
"""Choose the retry count.
Args:
retries: Number of retries (choices: [0, 1, 3])
"""
return retries
print(module.get_json_schema(choose_retries))Actual behavior and error logs
Full traceback from the executed reproducer (the local filename was minimal_repro.py):
Traceback (most recent call last):
File "/mnt/data/smolagents-contribution/minimal_repro.py", line 22, in <module>
print(module.get_json_schema(choose_retries))
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
File "/mnt/data/smolagents-contribution/vendor/_function_type_hints_utils.py", line 224, in get_json_schema
schema["enum"] = [c.strip() for c in json.loads(enum_choices.group(1))]
^^^^^^^
AttributeError: 'int' object has no attribute 'strip'Additional isolated cases executed against the same unchanged module:
| Argument annotation | Docstring choices | Observed result |
|---|---|---|
int |
[0, 1, 3] |
AttributeError for int.strip |
float |
[0.1, 0.5, 1.0] |
AttributeError for float.strip |
bool |
[true, false] |
AttributeError for bool.strip |
int | None |
[null, 1] |
AttributeError for NoneType.strip |
str |
["tea", "coffee"] |
Works |
str |
[" tea ", "coffee "] |
Works; existing string trimming is preserved |
int |
No choices block | Works |
Literal[0, 1, 3] |
No choices block | Works as an annotation-based workaround |
A small local test harness asserting the expected behavior produced 4 failed, 4 passed on upstream code. Those are reproduction/control results, not results for a completed fix.
Expected behavior
The integer example should produce an argument schema containing "type": "integer" and "enum": [0, 1, 3], rather than raising. Non-string enum members should retain their decoded JSON types, not be converted to strings. Existing string-only choices behavior should continue to work.
Environment
- OS: Linux x86_64, kernel 6.18.44, glibc 2.41
- Python: 3.13.5
- pytest: 9.0.2 (used only for the additional regression/control harness)
- Source:
main, inspected at commit227ef5e49ddd82339295939072f0223249aa8d38 - File:
src/smolagents/_function_type_hints_utils.py - Verified Git blob SHA:
e8102ead0d7e64c49ebaa95d69081ba373876e79 - Package version: not installed. Validation loaded this exact upstream module in isolation; the full package suite and public
@toolintegration were not run.
Additional context / proposed scope
The relevant expression is in get_json_schema. A narrow fix would trim string members only and preserve other decoded values, with regression coverage for the four failing cases and existing string behavior. Broader validation of malformed choices blocks would remain out of scope.
Could a maintainer confirm whether this scope can be accepted for a follow-up fix?
AI tools were used for source inspection and the isolated reproduction tests.
Checklist
- Searched existing issues and did not find the same bug report.
- Provided a minimal, reproducible example.
- Provided the full traceback.
- Provided environment details and exact source identity.
Source: huggingface/smolagents