{"record":{"id":"31ea5a35cc1ddc16","repo":"huggingface/smolagents","slug":"could-not-find-specified-api-name-among-availab","errorCode":null,"errorMessage":"Could not find specified {api_name=} among available api names.","messagePattern":"Could not find specified (.+?) among available api names\\.","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"src/smolagents/tools.py","lineNumber":674,"sourceCode":"                self.name = name\n                self.description = description\n                self.client = Client(space_id, hf_token=token)\n                space_api = self.client.view_api(return_format=\"dict\", print_info=False)\n                assert isinstance(space_api, dict)\n                space_description = space_api[\"named_endpoints\"]\n\n                # If api_name is not defined, take the first of the available APIs for this space\n                if api_name is None:\n                    api_name = list(space_description.keys())[0]\n                    warnings.warn(\n                        f\"Since `api_name` was not defined, it was automatically set to the first available API: `{api_name}`.\"\n                    )\n                self.api_name = api_name\n\n                try:\n                    space_description_api = space_description[api_name]\n                except KeyError:\n                    raise KeyError(f\"Could not find specified {api_name=} among available api names.\")\n                self.inputs = {}\n                for parameter in space_description_api[\"parameters\"]:\n                    parameter_type = parameter[\"type\"][\"type\"]\n                    if parameter_type == \"object\":\n                        parameter_type = \"any\"\n                    self.inputs[parameter[\"parameter_name\"]] = {\n                        \"type\": parameter_type,\n                        \"description\": parameter[\"python_type\"][\"description\"],\n                        \"nullable\": parameter[\"parameter_has_default\"],\n                    }\n                output_component = space_description_api[\"returns\"][0][\"component\"]\n                if output_component == \"Image\":\n                    self.output_type = \"image\"\n                elif output_component == \"Audio\":\n                    self.output_type = \"audio\"\n                else:\n                    self.output_type = \"any\"\n                self.is_initialized = True","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/tools.py#L656-L692","documentation":"When wrapping a Gradio Space with a specific api_name, smolagents looks up that endpoint in the Space's API description. If api_name doesn't match any endpoint, a KeyError is raised with the available names implied by the lookup.","triggerScenarios":"Tool.from_space(repo_id, api_name=\"/predict2\") where the Space exposes endpoints like '/predict'; using an api_name with a missing or extra leading slash; the Space changed its endpoint names after an update; hitting a Spaces router page instead of the direct Space URL.","commonSituations":"Passing the function name from Python ('predict') instead of the Gradio API path ('/predict'); Space author renamed endpoints; URL points to a Spaces router (e.g. huggingface.co/spaces/... redirects) so the wrong API description is fetched.","solutions":["Omit api_name to let smolagents pick the first available endpoint","Get exact names via gradio_client.Client(repo).view_api() and pass one of those (usually with a leading slash, e.g. '/predict')","Pin the Space revision or use the direct space URL if the Space layout changed"],"exampleFix":"# before\ntool = Tool.from_space(\"user/space\", api_name=\"predict\")\n# after\nfrom gradio_client import Client\nprint(Client(\"user/space\").view_api())  # copy exact endpoint name\ntool = Tool.from_space(\"user/space\", api_name=\"/predict\")","handlingStrategy":"validation","validationCode":"from gradio_client import Client\ndef get_api_names(repo_id: str) -> list[str]:\n    return [ep.name() for ep in Client(repo_id).endpoints()]\n\nnames = get_api_names(\"user/space\")\nassert \"/predict\" in names before Tool.from_space(\"user/space\", api_name=\"/predict\")","typeGuard":null,"tryCatchPattern":"try:\n    tool = Tool.from_space(repo, api_name=name)\nexcept KeyError:\n    names = [ep.name() for ep in Client(repo).endpoints()]\n    tool = Tool.from_space(repo, api_name=names[0])","preventionTips":["List endpoints with Client(repo).view_api() before choosing api_name","Note Gradio api_names usually start with a slash ('/predict')","Omit api_name to default to the first endpoint"],"tags":["smolagents","gradio","from-space","api-name"],"backgroundTag":"endpoint-not-found","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}