Skip to main content
Botmartz AI Solutions Private Limited
AGENTS

Custom Tool Creation: @tool Decorator, StructuredTool, Schema Validation, and Tool Composition

Agents need tools. Build custom tools with the @tool decorator, define schemas for inputs, and compose tools into agent toolkits for specialized tasks.

Soham Sharma
Soham Sharma
AI Engineer, Botmartz · Dec 4, 2026
Depth
Full Spec
Failure Modes
5
Code Snippets
4
Runnable Notebook
0
Custom Tool Creation: @tool Decorator, StructuredTool, Schema Validation, and Tool Composition

Tools are callable functions that agents invoke to interact with the world. The @tool decorator turns Python functions into agent tools. StructuredTool with Pydantic schemas provides type safety and self-documenting interfaces.

Simple Tools with @tool

Code Snippet
from langchain.agents import tool

@tool
def search_wikipedia(query: str) -> str:
    """Search Wikipedia for information about a topic."""
    # Implementation
    return f"Results for {query}"

@tool
def get_weather(city: str) -> str:
    """Get the current weather in a city."""
    # Implementation
    return f"Weather in {city}"

# Agents can now call these tools

Structured Tools with Pydantic

Code Snippet
from langchain.agents import StructuredTool
from pydantic import BaseModel, Field

class SearchInput(BaseModel):
    query: str = Field(description="Search query")
    limit: int = Field(default=5, description="Number of results")

def search_function(query: str, limit: int) -> str:
    # Implementation
    return f"Found {limit} results for {query}"

search_tool = StructuredTool.from_function(
    func=search_function,
    args_schema=SearchInput,
)

Conclusion

Custom tools enable agents to interact with external systems. Structured tools with schemas ensure type safety and clear interfaces. Building a toolkit of domain-specific tools powers specialized agents. Next: tracing and debugging agent execution with LangSmith.

#Agents#Tools#API Integration#Pydantic
1 views
Soham Sharma
Soham Sharma
Leading AI systems research, enterprise RAG, and multi-agent workflow architecture at Botmartz AI Solutions.