DOC: <Please write a comprehensive title after the 'DOC: ' prefix>How can I add custom tools using BaseModel and BaseTool to the official sample code? #225

Closed
opened 2026-02-20 17:32:36 -05:00 by yindo · 1 comment
Owner

Originally created by @guapia233 on GitHub (Sep 5, 2024).

Issue with current documentation:

official sample code link:https://github.com/langchain-ai/langgraph/blob/main/examples/multi_agent/hierarchical_agent_teams.ipynb

Idea or request for content:

The custom tool is as follows (the image is highlighted in red because I removed some parts of the function for screenshots, so please ignore it) :
image
Define the agent node in langgraph as follows (wrong feeling):
image
The error is as follows:
TypeError: Object of type 'ModelMetaclass' is not JSON serializable
or
TypeError: _run() missing 2 required positional arguments: 'self' and 'network_segment'

Originally created by @guapia233 on GitHub (Sep 5, 2024). ### Issue with current documentation: official sample code link:https://github.com/langchain-ai/langgraph/blob/main/examples/multi_agent/hierarchical_agent_teams.ipynb ### Idea or request for content: The custom tool is as follows (the image is highlighted in red because I removed some parts of the function for screenshots, so please ignore it) : ![image](https://github.com/user-attachments/assets/29437106-2ba0-48c6-8b1b-6e08eb60e8fb) Define the agent node in langgraph as follows (wrong feeling): ![image](https://github.com/user-attachments/assets/cccb3c64-7fd1-4e64-8817-9da59db60975) The error is as follows: TypeError: Object of type 'ModelMetaclass' is not JSON serializable or TypeError: _run() missing 2 required positional arguments: 'self' and 'network_segment'
yindo closed this issue 2026-02-20 17:32:37 -05:00
Author
Owner

@gbaian10 commented on GitHub (Sep 5, 2024):

from dotenv import load_dotenv
from langchain_core.tools import BaseTool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from pydantic import BaseModel, Field


class NmapInput(BaseModel):
    network_segment: str = Field(description="the target's IP address segment")


class Nmap(BaseTool):
    name = "nmap"
    description = "useful for when you need to determine the target's IP address based on a network segment"
    args_schema = NmapInput

    def _run(self, network_segment: str) -> str:
        print(f"{network_segment = }")
        return f"Scanning {network_segment}..."


load_dotenv()
llm = ChatOpenAI(model="gpt-4o-mini")
scan_agent = create_react_agent(llm, tools=[Nmap()])
outputs = scan_agent.invoke({"messages": [("human", "I want to find 127.0.0.1")]})
print(outputs["messages"][-1].content)
@gbaian10 commented on GitHub (Sep 5, 2024): ```py from dotenv import load_dotenv from langchain_core.tools import BaseTool from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent from pydantic import BaseModel, Field class NmapInput(BaseModel): network_segment: str = Field(description="the target's IP address segment") class Nmap(BaseTool): name = "nmap" description = "useful for when you need to determine the target's IP address based on a network segment" args_schema = NmapInput def _run(self, network_segment: str) -> str: print(f"{network_segment = }") return f"Scanning {network_segment}..." load_dotenv() llm = ChatOpenAI(model="gpt-4o-mini") scan_agent = create_react_agent(llm, tools=[Nmap()]) outputs = scan_agent.invoke({"messages": [("human", "I want to find 127.0.0.1")]}) print(outputs["messages"][-1].content) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#225