custom_tool() — langchain Function Reference
Architecture documentation for the custom_tool() function in custom_tool.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 36d50a89_56e5_c281_6ef1_1f4a9574ce0a["custom_tool()"] 2bc1c3fe_4564_705b_4b4f_ef73fdf56b3c["custom_tool.py"] 36d50a89_56e5_c281_6ef1_1f4a9574ce0a -->|defined in| 2bc1c3fe_4564_705b_4b4f_ef73fdf56b3c 96434308_aeec_dce2_9fb2_a803ae096f07["_make_wrapped_coroutine()"] 36d50a89_56e5_c281_6ef1_1f4a9574ce0a -->|calls| 96434308_aeec_dce2_9fb2_a803ae096f07 f0fb9f2e_4f8a_b540_b097_0a1c095ba3a2["_make_wrapped_func()"] 36d50a89_56e5_c281_6ef1_1f4a9574ce0a -->|calls| f0fb9f2e_4f8a_b540_b097_0a1c095ba3a2 style 36d50a89_56e5_c281_6ef1_1f4a9574ce0a fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/openai/langchain_openai/tools/custom_tool.py lines 27–111
def custom_tool(*args: Any, **kwargs: Any) -> Any:
"""Decorator to create an OpenAI custom tool.
Custom tools allow for tools with (potentially long) freeform string inputs.
See below for an example using LangGraph:
```python
@custom_tool
def execute_code(code: str) -> str:
\"\"\"Execute python code.\"\"\"
return "27"
model = ChatOpenAI(model="gpt-5", output_version="responses/v1")
agent = create_react_agent(model, [execute_code])
input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
for step in agent.stream(
{"messages": [input_message]},
stream_mode="values",
):
step["messages"][-1].pretty_print()
```
You can also specify a format for a corresponding context-free grammar using the
`format` kwarg:
```python
from langchain_openai import ChatOpenAI, custom_tool
from langgraph.prebuilt import create_react_agent
grammar = \"\"\"
start: expr
expr: term (SP ADD SP term)* -> add
| term
term: factor (SP MUL SP factor)* -> mul
| factor
factor: INT
SP: " "
ADD: "+"
MUL: "*"
%import common.INT
\"\"\"
format = {"type": "grammar", "syntax": "lark", "definition": grammar}
# highlight-next-line
@custom_tool(format=format)
def do_math(input_string: str) -> str:
\"\"\"Do a mathematical operation.\"\"\"
return "27"
model = ChatOpenAI(model="gpt-5", output_version="responses/v1")
agent = create_react_agent(model, [do_math])
input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
for step in agent.stream(
{"messages": [input_message]},
stream_mode="values",
):
step["messages"][-1].pretty_print()
```
"""
def decorator(func: Callable[..., Any]) -> Any:
metadata = {"type": "custom_tool"}
if "format" in kwargs:
metadata["format"] = kwargs.pop("format")
tool_obj = tool(infer_schema=False, **kwargs)(func)
tool_obj.metadata = metadata
tool_obj.description = func.__doc__
if inspect.iscoroutinefunction(func):
tool_obj.coroutine = _make_wrapped_coroutine(func)
else:
tool_obj.func = _make_wrapped_func(func)
return tool_obj
Domain
Subdomains
Source
Frequently Asked Questions
What does custom_tool() do?
custom_tool() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/tools/custom_tool.py.
Where is custom_tool() defined?
custom_tool() is defined in libs/partners/openai/langchain_openai/tools/custom_tool.py at line 27.
What does custom_tool() call?
custom_tool() calls 2 function(s): _make_wrapped_coroutine, _make_wrapped_func.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free