{"record":{"id":"474a96226e6309ff","repo":"huggingface/smolagents","slug":"tool-dictionary-must-contain-code-key-with-the-t","errorCode":null,"errorMessage":"Tool dictionary must contain 'code' key with the tool source code","messagePattern":"Tool dictionary must contain 'code' key with the tool source code","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/smolagents/tools.py","lineNumber":380,"sourceCode":"        if hasattr(self, \"output_schema\") and self.output_schema is not None:\n            tool_dict[\"output_schema\"] = self.output_schema\n\n        return tool_dict\n\n    @classmethod\n    def from_dict(cls, tool_dict: dict[str, Any], **kwargs) -> \"Tool\":\n        \"\"\"\n        Create tool from a dictionary representation.\n\n        Args:\n            tool_dict (`dict[str, Any]`): Dictionary representation of the tool.\n            **kwargs: Additional keyword arguments to pass to the tool's constructor.\n\n        Returns:\n            `Tool`: Tool object.\n        \"\"\"\n        if \"code\" not in tool_dict:\n            raise ValueError(\"Tool dictionary must contain 'code' key with the tool source code\")\n\n        tool = cls.from_code(tool_dict[\"code\"], **kwargs)\n\n        # Set output_schema if it exists in the dictionary\n        if \"output_schema\" in tool_dict:\n            tool.output_schema = tool_dict[\"output_schema\"]\n\n        return tool\n\n    def save(self, output_dir: str | Path, tool_file_name: str = \"tool\", make_gradio_app: bool = True):\n        \"\"\"\n        Saves the relevant code files for your tool so it can be pushed to the Hub. This will copy the code of your\n        tool in `output_dir` as well as autogenerate:\n\n        - a `{tool_file_name}.py` file containing the logic for your tool.\n        If you pass `make_gradio_app=True`, this will also write:\n        - an `app.py` file providing a UI for your tool when it is exported to a Space with `tool.push_to_hub()`\n        - a `requirements.txt` containing the names of the modules used by your tool (as detected when inspecting its","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/tools.py#L362-L398","documentation":"Tool.from_dict requires the dictionary to contain a 'code' key holding the tool's Python source, since reconstruction works by exec-ing that code via from_code. Missing 'code' raises ValueError.","triggerScenarios":"Calling Tool.from_dict(d) where d was built manually, or a dict produced by a different serializer, that lacks the 'code' key; passing a dict with only 'class_name'/'name'/'description' metadata.","commonSituations":"Hand-editing a saved agent's tools JSON and dropping keys; loading a dict produced by an older/newer smolagents version whose save format differs; constructing tool dicts from YAML configs without embedding source.","solutions":["Use tool.to_dict() output as the canonical format and load it back unmodified","If constructing the dict manually, include 'code': the full tool source string (what to_dict produces)","Alternatively load via Tool.from_hub(repo_id) if the tool lives on the Hub"],"exampleFix":"# before\nd = {\"name\": \"my_tool\", \"description\": \"...\"}\nTool.from_dict(d)\n# after\nd = my_tool.to_dict()  # includes 'code'\nTool.from_dict(d)","handlingStrategy":"validation","validationCode":"def is_valid_tool_dict(d: dict) -> bool:\n    return isinstance(d, dict) and \"code\" in d and isinstance(d[\"code\"], str)","typeGuard":"from typing import TypedDict\nclass ToolDict(TypedDict):\n    code: str\n    class_name: str\n    name: str\n    description: NotRequired[str]","tryCatchPattern":null,"preventionTips":["Only load dicts produced by Tool.to_dict(); don't hand-craft them","Round-trip test: assert Tool.from_dict(t.to_dict()).name == t.name","Version-pin smolagents so the save format stays stable"],"tags":["smolagents","deserialization","from-dict","missing-key"],"backgroundTag":"missing-required-key","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}