Generating Python docstrings with OpenAI ChatGPT!
What is gpt4docstrings?
gpt4docstrings is a powerful library designed to assist you in writing docstrings for your Python code. Just select the paths where you want gpt4docstrings applied, run the command, and wait for the results!
Requirements
Before proceeding, note that gpt4docstrings supports Python 3.9 and above. Make sure your environment meets this requirement.
Installation
Warning: As this library is currently under heavy development, it is advisable to always install the latest version. You can install gpt4docstrings via pip from PyPI:
console
$ pip install -U gpt4docstrings
Usage
To run gpt4docstrings on a specific file, use the following command:
bash
gpt4docstrings my_file.py
If you do not have your OpenAI API Key set as an environment variable (OPENAI_API_KEY), you can supply it as an option:
bash
gpt4docstrings --api_key sk-xxxxxxxxxxxx my_file.py
For safety reasons, gpt4docstrings does not overwrite the file in place. Instead, it generates a patch file named gpt4docstring_docstring_generator_patch.diff that contains all the necessary changes. To apply the patch, simply run:
bash
patch -p1 gpt4docstring_docstring_generator_patch.diff
If you prefer to modify the files directly, you can use the –overwrite or -w option:
bash
gpt4docstrings -w my_file.py
gpt4docstrings can also process folders recursively:
bash
gpt4docstrings src
To exclude specific folders (e.g., the tests folder), you can easily do so:
bash
gpt4docstrings --exclude tests src
The default docstring style generated is Google style, but you can choose from several formats including Google, Numpy, Epytext, and reStructuredText by specifying the -st option. For example:
bash
gpt4docstrings -st epytext my_file.py
For a complete list of available options, access the help info by running:
bash
gpt4docstrings --help
Example
Here’s a practical example of using gpt4docstrings to generate docstrings for a Python code snippet. Let’s say we have a file named exampleexample.py containing the following code:
python
import asyncio
async def async_example():
await asyncio.sleep(2)
class MyClass:
def __init__(self, value):
self.value = value
@staticmethod
def nested_method():
def inner_function():
print("Nested method inner function")
print("Nested method start")
inner_function()
print("Nested method completed")
We will run gpt4docstrings to create Numpy-style docstrings instead of directly overwriting the file:
bash
gpt4docstrings exampleexample.py -v 1 -st numpy
After execution, a new patch file named gpt4docstring_docstring_generator_patch.diff will be created in the directory. To apply the patch, just run:
bash
patch -p1 gpt4docstring_docstring_generator_patch.diff
This example will showcase how gpt4docstrings generates detailed docstrings based on the initial code.
Troubleshooting
- Ensure that your Python version is 3.9 or above.
- Make sure your OpenAI API Key is correctly set as an environment variable or passed as an option to the command.
- If patch application fails, confirm that you are in the correct directory and that the patch file exists.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

