Fix: Add backward compatibility for deprecated HfApiModel by MohammadKassas143 · Pull Request #1863 · huggingface/smolagents (original) (raw)

This PR fixes an AttributeError: module 'smolagents.models' has no attribute 'HfApiModel' that occurs when loading older agents from the Hub.
I faced this problem when loading Alfred

The fix adds backward compatibility to the from_dict method, mapping the deprecated HfApiModel class name to the new InferenceClientModel and logging a warning.

Tested locally by successfully loading sergiopaniego/AlfredAgent.

Running on Colab without the Modification:

Fetching 14 files: 100%  14/14 [00:00<00:00, 699.67it/s]

AttributeError Traceback (most recent call last) /tmp/ipython-input-3564958758.py in <cell line: 0>() 1 agent = CodeAgent(tools=[], model=InferenceClientModel()) ----> 2 alfred_agent = agent.from_hub('sergiopaniego/AlfredAgent', trust_remote_code=True)

3 frames /usr/local/lib/python3.12/dist-packages/smolagents/agents.py in from_dict(cls, agent_dict, **kwargs) 1015 # Load model 1016 model_info = agent_dict["model"] -> 1017 model_class = getattr(importlib.import_module("smolagents.models"), model_info["class"]) 1018 model = model_class.from_dict(model_info["data"]) 1019 # Load tools

AttributeError: module 'smolagents.models' has no attribute 'HfApiModel'

Running after Modification:

Fetching 14 files: 100%|14/14 [00:00<?, ?it/s] WARNING:smolagents.agents:Loading an agent created with the deprecated 'HfApiModel' class. This class has been renamed to 'InferenceClientModel'. The agent's model class has been automatically updated for compatibility.