Generate Python Tool Specs from Code
Python File Tool automatically extracts function names, arguments, and descriptions from Python files to create custom Tool Specs for LlamaIndex agents.
Why it matters
Automate the creation of custom tool specifications by extracting function definitions, arguments, and descriptions from Python files. This streamlines the process of integrating existing Python code into AI agent workflows.
Outcomes
What it gets done
Load and parse Python files to extract function metadata.
Automatically generate function definitions with docstrings and type hints.
Facilitate the creation of custom tool specs for AI agents.
Extract function names, arguments, and descriptions.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-python-file | bash Overview
Python File Tool
Python File Tool automatically loads Python files and extracts function names, arguments, and descriptions to create custom Tool Specs for LlamaIndex agents. It provides three core methods: function_definitions for retrieving all functions, get_function for specific function lookup, and get_functions for batch retrieval. Use this tool when you need to load Python files and extract function information for creating Tool Specs. The source material demonstrates usage with a numpy_linalg.py file example and shows the tool being used with LlamaIndex agents.
What it does
Python File Tool loads Python files and automatically extracts function names, arguments, and descriptions to create custom Tool Specs. The tool provides three methods: function_definitions to get all function definitions from the file, get_function to retrieve a specific function definition, and get_functions to get a list of functions from the file.
Inputs and outputs
You provide a path to a Python file. The tool extracts function definitions, including names, arguments, and descriptions, and returns them as Tool Specs. The tool provides three methods: function_definitions to get all function definitions from the file, get_function to retrieve a specific function definition, and get_functions to get a list of functions from the file.
Integrations
The source material shows Python File Tool used with LlamaIndex's FunctionAgent and OpenAI LLMs in the provided example.
from llama_index.tools.python_file import PythonFileToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
pyfile = PythonFileToolSpec("./numpy_linalg.py")
agent = FunctionAgent(
tools=pyfile.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
print(
await agent.run(
"""Load the eig, transpose and solve functions from the python file,
and then write a function definition using only builtin python types (List, float, Tuple)
with a short 5-10 line doc string tool prompts for the functions that only has a small description and arguments
"""
)
)
Who it's for
This loader is designed to be used as a way to load data as a Tool in a Agent.
Source README
Python File Tool
This tool loads a python file and extracts function names, arguments and descriptions automatically. This tool is particular useful for automatically creating custom Tool Specs when you already have well documented python functions.
Usage
This tool has more extensive example usage documented in a Jupyter notebook here
Here's an example usage of the PythonFileToolSpec.
from llama_index.tools.python_file import PythonFileToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
pyfile = PythonFileToolSpec("./numpy_linalg.py")
agent = FunctionAgent(
tools=pyfile.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
print(
await agent.run(
"""Load the eig, transpose and solve functions from the python file,
and then write a function definition using only builtin python types (List, float, Tuple)
with a short 5-10 line doc string tool prompts for the functions that only has a small description and arguments
"""
)
)
function_definitions: Get all of the function definitions from the Python fileget_function: Get a specific function definition from the Python fileget_functions: Get a list of functions from the python file
This loader is designed to be used as a way to load data as a Tool in a Agent.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.