Tool Class
The Tool class represents a generated tool and provides methods for execution and framework adaptation.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
name | str | Tool identifier. |
intent | str | Natural language description. |
inputs | list[InputParam] | Input parameter definitions. |
Execution Methods
Section titled “Execution Methods”Execute the tool and return the result.
result = tool.run(**kwargs) -> AnyParameters
Section titled “Parameters”Accepts keyword arguments matching the tool’s input parameters.
Returns
Section titled “Returns”The result from the tool’s run() function.
Raises
Section titled “Raises”RuntimeError - If execution fails and self-healing is disabled or exhausted.
Example
Section titled “Example”result = tool.run(query="python tutorials", limit=10)print(result)run_safe()
Section titled “run_safe()”Execute the tool without raising exceptions.
result = tool.run_safe(**kwargs) -> ToolResultReturns
Section titled “Returns”ToolResult - Object with success, data, and error fields.
Example
Section titled “Example”result = tool.run_safe(query="test")
if result.success: print(result.data)else: print(f"Error: {result.error}")run_async()
Section titled “run_async()”Execute the tool asynchronously.
result = await tool.run_async(**kwargs) -> AnyExample
Section titled “Example”import asyncio
async def main(): result = await tool.run_async(query="test") print(result)
asyncio.run(main())run_safe_async()
Section titled “run_safe_async()”Execute the tool asynchronously without raising exceptions.
result = await tool.run_safe_async(**kwargs) -> ToolResultExample
Section titled “Example”async def main(): result = await tool.run_safe_async(query="test") if result.success: print(result.data)run_interactive()
Section titled “run_interactive()”Collect inputs via CLI prompts and execute.
result = tool.run_interactive() -> AnyPrompts the user for each input parameter, then executes the tool.
Example
Section titled “Example”# Prompts user for inputsresult = tool.run_interactive()# Enter query: python tutorials# Enter limit [10]:# Result: {...}Framework Adapters
Section titled “Framework Adapters”to_langchain()
Section titled “to_langchain()”Convert to a LangChain BaseTool.
lc_tool = tool.to_langchain() -> BaseToolReturns
Section titled “Returns”BaseTool - LangChain-compatible tool.
Requirements
Section titled “Requirements”pip install "anvil-agent[langchain]"Example
Section titled “Example”from langchain.agents import create_tool_calling_agent
lc_tool = tool.to_langchain()agent = create_tool_calling_agent(llm, [lc_tool], prompt)to_crewai()
Section titled “to_crewai()”Convert to a CrewAI BaseTool.
crew_tool = tool.to_crewai() -> BaseToolReturns
Section titled “Returns”BaseTool - CrewAI-compatible tool.
Requirements
Section titled “Requirements”pip install "anvil-agent[crewai]"Example
Section titled “Example”from crewai import Agent
crew_tool = tool.to_crewai()agent = Agent(role="Researcher", tools=[crew_tool])to_autogen()
Section titled “to_autogen()”Convert to an AutoGen FunctionTool.
ag_tool = tool.to_autogen() -> FunctionToolReturns
Section titled “Returns”FunctionTool - AutoGen-compatible tool.
Requirements
Section titled “Requirements”pip install "anvil-agent[autogen]"Example
Section titled “Example”from autogen_agentchat.agents import AssistantAgent
ag_tool = tool.to_autogen()agent = AssistantAgent(name="assistant", tools=[ag_tool])to_openai_agents()
Section titled “to_openai_agents()”Convert to an OpenAI Agents SDK FunctionTool.
oai_tool = tool.to_openai_agents() -> FunctionToolReturns
Section titled “Returns”FunctionTool - OpenAI Agents-compatible tool.
Requirements
Section titled “Requirements”pip install "anvil-agent[openai-agents]"Example
Section titled “Example”from agents import Agent
oai_tool = tool.to_openai_agents()agent = Agent(name="assistant", tools=[oai_tool])Chaining
Section titled “Chaining”pipe()
Section titled “pipe()”Chain this tool with another tool.
chain = tool.pipe(other: Tool) -> ToolChainParameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
other | Tool | Tool to chain after this one. |
Returns
Section titled “Returns”ToolChain - A chain of tools for sequential execution.
Example
Section titled “Example”chain = search_tool.pipe(summarize_tool).pipe(translate_tool)result = chain.run(query="AI news")Information Methods
Section titled “Information Methods”get_inputs()
Section titled “get_inputs()”Get the list of input parameters.
inputs = tool.get_inputs() -> list[InputParam]Returns
Section titled “Returns”list[InputParam] - List of input parameter definitions.
Example
Section titled “Example”for param in tool.get_inputs(): print(f"{param.name}: {param.param_type} (required={param.required})")has_required_inputs()
Section titled “has_required_inputs()”Check if the tool has any required inputs.
has_required = tool.has_required_inputs() -> boolReturns
Section titled “Returns”bool - True if any input is required.
ToolResult
Section titled “ToolResult”Returned by run_safe() methods.
from anvil.models import ToolResult
result = ToolResult( success: bool, data: Any = None, error: str | None = None,)Fields
Section titled “Fields”| Field | Type | Description |
|---|---|---|
success | bool | Whether execution succeeded. |
data | Any | Result data on success. |
error | str | None | Error message on failure. |
Example
Section titled “Example”result = tool.run_safe(query="test")
if result.success: process(result.data)else: log_error(result.error)ToolChain
Section titled “ToolChain”Created by pipe() method.
Methods
Section titled “Methods”| Method | Description |
|---|---|
run(**kwargs) | Execute chain, raising on error. |
run_safe(**kwargs) | Execute chain safely. |
run_async(**kwargs) | Execute chain asynchronously. |
run_safe_async(**kwargs) | Execute chain async safely. |
pipe(tool) | Add another tool to the chain. |
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
len(chain) | int | Number of tools in chain. |
str(chain) | str | String representation. |
Example
Section titled “Example”from anvil import ToolChain
# Create chainchain = ToolChain([tool1, tool2, tool3])
# Or use pipechain = tool1.pipe(tool2).pipe(tool3)
# Executeresult = chain.run(initial="data")print(len(chain)) # 3print(chain) # ToolChain(tool1 -> tool2 -> tool3)