smolagents
MelleaTool.from_smolagents() wraps any smolagents
Tool instance so it can be passed to any MelleaSession
call. The HuggingFace ecosystem provides many pre-built tools — PythonInterpreterTool,
DuckDuckGoSearchTool, WikipediaSearchTool, and others.
Prerequisites: pip install 'mellea[smolagents]'
Using smolagents tools
# Requires: mellea[smolagents], mellea[ollama]
# Returns: ModelOutputThunk
from mellea import start_session
from mellea.backends import ModelOption
from mellea.backends.tools import MelleaTool
from smolagents import PythonInterpreterTool
# Wrap the smolagents tool
python_tool = MelleaTool.from_smolagents(PythonInterpreterTool())
m = start_session()
result = m.instruct(
"Calculate the sum of numbers from 1 to 10 using Python",
model_options={ModelOption.TOOLS: [python_tool]},
tool_calls=True,
)
print(result)
if result.tool_calls:
try:
calc_result = result.tool_calls[python_tool.name].call_func()
print(f"Calculation result: {calc_result}")
except Exception as e:
print(f"Tool execution failed: {e}")
from_smolagents() uses smolagents' own JSON schema conversion, so the tool's
description and parameter types are preserved exactly.
Backend note: Tool calling requires a backend and model that support function calling (e.g., Ollama with
granite4.1:3b, OpenAI withgpt-4o). The default Ollama setup supports this.Full example:
docs/examples/tools/smolagents_example.py
Which approach to use
| Scenario | Use |
|---|---|
Your tool exists as a LangChain BaseTool | MelleaTool.from_langchain(tool) |
Your tool exists as a smolagents Tool | MelleaTool.from_smolagents(tool) |
| You have a plain Python function to expose | @tool decorator |
| You have LangChain message history to continue | convert_to_openai_messages → ChatContext |
| You want Mellea as an OpenAI endpoint for another framework | m serve |
See also: Tools and Agents | Context and Sessions