MelleaTool class (and the @tool decorator shorthand) for
wrapping Python callables as OpenAI-compatible tool schemas, with factory methods
for LangChain and smolagents interoperability. Also includes helpers for converting
tool lists to JSON, extracting tool call requests from raw LLM output strings, and
validating/coercing tool arguments against the tool’s JSON schema using Pydantic.
Functions
FUNC tool
func: The function to decorate (when used without arguments)name: Optional custom name for the tool (defaults to function name)
- A MelleaTool instance. Use .run() to invoke the tool.
- The returned object passes isinstance(result, MelleaTool) checks.
FUNC add_tools_from_model_options
tools_dict: Mutable mapping of tool name to tool instance; modified in-place.model_options: Model options dict that may contain aModelOption.TOOLSentry (either a list ofMelleaToolor adict[str, MelleaTool]).
FUNC add_tools_from_context_actions
tools_dict: Mutable mapping of tool name to tool instance; modified in-place.ctx_actions: List of[Component](../core/base#class-component)or[CBlock](../core/base#class-cblock)objects whose template representations may declare tools, orNoneto skip.
FUNC convert_tools_to_json
tools: Mapping of tool name toAbstractMelleaToolinstance.
- List of OpenAI-compatible JSON tool schema dicts, one per tool.
- Huggingface transformers library lets you pass in an array of functions but doesn’t like methods.
- WatsonxAI uses
from langchain_ibm.chat_models import convert_to_openai_toolin their demos, but it gives the same values. - OpenAI uses the same format / schema.
FUNC json_extraction
text: Input string potentially containing one or more JSON objects.
- A generator that yields each valid JSON object found in
text, - in order of appearance.
FUNC find_func
...\{"name": string, "arguments": \{\}\}...
Args:
d: A JSON-like Python object (typically adict) to search for a function call record.
- A
(name, args)tuple wherenameis the tool name string andargs - is the arguments mapping, or
(None, None)if no function call was found.
FUNC parse_tools
llm_response: Raw string output from a language model.
- List of
(tool_name, arguments)tuples for each tool call found.
FUNC validate_tool_arguments
tool: The MelleaTool instance to validate againstargs: Raw arguments from model (post-JSON parsing)coerce_types: If True, attempt type coercion for common cases (default: True)strict: If True, raise ValidationError on failures; if False, log warnings and return original args (default: False)
- Validated and optionally coerced arguments dict
ValidationError: If strict=True and validation fails
FUNC convert_function_to_ollama_tool
func: The Python callable to convert.name: Optional override for the tool name; defaults tofunc.__name__.
- An
OllamaToolinstance representing the function as an OpenAI-compatible - tool schema.
Classes
CLASS MelleaTool
Tool class to represent a callable tool with an OpenAI-compatible JSON schema.
Wraps a Python callable alongside its JSON schema representation so it can be
registered with backends that support tool calling (OpenAI, Ollama, HuggingFace, etc.).
Args:
name: The tool name used for identification and lookup.tool_call: The underlying Python callable to invoke when the tool is run.as_json_tool: The OpenAI-compatible JSON schema dict describing the tool’s parameters.
FUNC run
args: Positional arguments forwarded to the underlying callable.kwargs: Keyword arguments forwarded to the underlying callable.
- The return value of the underlying callable.
FUNC as_json_tool
FUNC from_langchain
tool: Alangchain_core.tools.BaseToolinstance to wrap.
- A Mellea tool wrapping the LangChain tool.
ImportError: Iflangchain-coreis not installed.ValueError: Iftoolis not aBaseToolinstance.
FUNC from_smolagents
tool: A smolagents.Tool instance
- A Mellea tool wrapping the smolagents tool
ImportError: If smolagents is not installedValueError: If tool is not a smolagents Tool instance
FUNC from_callable
func: The Python callable to wrap as a tool.name: Optional name override; defaults tofunc.__name__.
- A Mellea tool wrapping the callable.
CLASS SubscriptableBaseModel
Pydantic BaseModel subclass that also supports subscript ([]) access.
Imported from the Ollama Python client. Allows model fields to be accessed
via model["field"] in addition to model.field, which is required for
compatibility with Ollama’s internal response parsing.
Methods:
FUNC get
key: The field name to look up on the model.default: Value to return whenkeyis not a field on the model. Defaults toNone.
- The field value if the attribute exists, otherwise
default.
CLASS OllamaTool
Pydantic model for an Ollama-compatible tool schema, imported from the Ollama Python SDK.
Represents the JSON structure that Ollama (and OpenAI-compatible endpoints) expect
when a tool is passed to the chat API. Mellea builds these objects internally via
convert_function_to_ollama_tool and never exposes them to end users directly.
Attributes:
type: Tool type; always"function"for function-calling tools.function: Nested object containing the function name, description, and parameters schema.