huggingface/smolagents · error · TypeError
Attribute output_schema should have type dict, got {type(out
Error message
Attribute output_schema should have type dict, got {type(output_schema)} instead. What it means
The optional Tool.output_schema attribute, when set, must be a dict (JSON-schema style). validate_arguments raises TypeError if output_schema is present but is any other type. Leaving it unset (None) is fine; the check only applies when you provide it.
Source
Thrown at src/smolagents/tools.py:164
"description": str,
"name": str,
"inputs": dict,
"output_type": str,
}
# Validate class attributes
for attr, expected_type in required_attributes.items():
attr_value = getattr(self, attr, None)
if attr_value is None:
raise TypeError(f"You must set an attribute {attr}.")
if not isinstance(attr_value, expected_type):
raise TypeError(
f"Attribute {attr} should have type {expected_type.__name__}, got {type(attr_value)} instead."
)
# Validate optional output_schema attribute
output_schema = getattr(self, "output_schema", None)
if output_schema is not None and not isinstance(output_schema, dict):
raise TypeError(f"Attribute output_schema should have type dict, got {type(output_schema)} instead.")
# - Validate name
if not is_valid_name(self.name):
raise Exception(
f"Invalid Tool name '{self.name}': must be a valid Python identifier and not a reserved keyword"
)
# Validate inputs
for input_name, input_content in self.inputs.items():
assert isinstance(input_content, dict), f"Input '{input_name}' should be a dictionary."
assert "type" in input_content and "description" in input_content, (
f"Input '{input_name}' should have keys 'type' and 'description', has only {list(input_content.keys())}."
)
# Get input_types as a list, whether from a string or list
if isinstance(input_content["type"], str):
input_types = [input_content["type"]]
elif isinstance(input_content["type"], list):
input_types = input_content["type"]
# Check if all elements are stringsView on GitHub (pinned to 30bb116109)
Solutions
- Convert the value to a dict, e.g. json.loads(schema_string) or use the already-parsed schema object
- If using pydantic, pass MyModel.model_json_schema() (which returns a dict), not the model class
- Remove output_schema entirely if the tool returns plain text
Example fix
# before
class MyTool(Tool):
output_schema = '{"type": "object", ...}' # str -> TypeError
# after
class MyTool(Tool):
output_schema = {"type": "object", "properties": {"result": {"type": "string"}}} Defensive patterns
Strategy: type-guard
Validate before calling
schema = getattr(MyTool, "output_schema", None) assert schema is None or isinstance(schema, dict), "output_schema must be a dict"
Type guard
def output_schema_ok(cls) -> bool:
s = getattr(cls, "output_schema", None)
return s is None or isinstance(s, dict) Try / catch
try:
MyTool()
except TypeError as e:
if "output_schema" in str(e):
MyTool.output_schema = dict(MyTool.output_schema) # or parse JSON string
MyTool() Prevention
- Use pydantic's model_json_schema() (returns dict) directly
- json.loads() any schema string before assigning
- Omit output_schema for plain-text tools
When it happens
Trigger: Setting output_schema to a JSON string, a pydantic model, a list, or any non-dict value on a Tool subclass; error fires at instantiation via new_init → validate_arguments.
Common situations: Copy-pasting a JSON schema from docs as a string instead of parsing it into a dict; assigning a pydantic BaseModel class or .model_json_schema() result's string form; refactoring a tool from dict-based to string-based config.
Related errors
- Attribute {attr} should have type {expected_type.__name__},
- Input '{input_name}': when type is a list, all elements must
- {e}
- Object is not iterable
- Cannot add non-list value {value_to_add} to a list.
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/9c4dc4390f5d25ef.
Report an issue: GitHub.