ChatGPT: Docstring Generator
Automatically generate Python docstrings using ChatGPT.

Notice!
Table of Contents
Installation
Install it from Visual Studio Marketplace or download and install .vsix file from Releases.
Requirements
- VSCode 1.78.0 or greater
- Python 3.9 or greater
Usage
Code Completion
Type """
and select Generate Docstring (ChatGPT)
from the completion items.

Right click in the Text Editor area and select Generate Docstring (ChatGPT)
.

Command Palette
Open the Command Palette (F1), type docstring
and select Generate Docstring (ChatGPT)
.

Keyboard Shortcut
Use the following keyboard shortcut:
- Windows/Linux:
Ctrl+Alt+D
- Mac:
Cmd+Opt+D

You can change the default keyboard shortcut by Keyboard Shortcuts editor (File > Preferences > Keyboard Shortcuts).
API key
To use the extension, you need an API key from OpenAI or another OpenAI-compatible provider.
How to Set or Change API Key
When you first generate a docstring, a pop-up window will appear requesting you to enter your API key. You can change the API key through the Settings editor (File > Preferences > Settings > Extensions > ChatGPT: Docstring Generator). Or using the Set API key
command in the Command Palette (F1).
Switching AI Providers
By default, this extension uses OpenAI API to generate docstrings. However, you can easily switch to other AI services that support OpenAI-compatible APIs.
How to Change AI Provider
- Set the Base URL: In the extension settings, locate the
baseUrl
parameter. You can change this to point to a different OpenAI-compatible API, such as OpenRouter, which offers access to a variety of AI models. For example, to use OpenRouter, set the baseUrl
to: https://openrouter.ai/api/v1
- Specify the AI Model: To specify which model you want to use, locate the
aiModelCustom
parameter in the settings. Here, you can define the exact AI model you wish to interact with, depending on the service you're using. For example, if you're using OpenRouter, you might choose from models such as anthropic/claude-3.5-haiku
, google/gemini-pro-1.5
, or others depending on availability.
- Set the API Key: Set your API key for the selected AI service. You can obtain this key from the service website.
Now, the extension will route requests through the selected AI service. Ensure the provider you choose is compatible with OpenAI’s API.
Settings
chatgpt-docstrings.interpreter
: Path to the Python executable used to start the language server. If not set, the Python interpreter selected in the VSCode status bar is used.
chatgpt-docstrings.baseUrl
: The base URL for the OpenAI-compatible API.
- Default value: "
https://api.openai.com/v1
"
chatgpt-docstrings.aiModel
: Which AI language model to use. GPT-4, GPT-4 Turbo, and GPT-4o are available in the OpenAI API to paying customers.
- Default value: "gpt-4o-mini"
- Available options:
- "gpt-4o-mini"
- "gpt-4o"
- "gpt-4-turbo"
- "gpt-4"
- "gpt-3.5-turbo"
chatgpt-docstrings.aiModelCustom
: If set, overrides the model configured in chatgpt-docstrings.aiModel
.
chatgpt-docstrings.docstringStyle
: Which the docstring style to use.
- Default value: "google"
- Available options:
- "google"
- "numpy"
- "sphinx"
chatgpt-docstrings.onNewLine
: Option to start the docstring on a new line after the triple quotes.
- Default value: false
- Available options:
This option is ignored when generating one-line docstrings.
chatgpt-docstrings.promptPattern
: The AI prompt used to generate docstrings.
- Default value: "Generate a {docstring_style}-style docstring for the following Python {entity} code:\n{code}"
Placeholders enclosed in curly brackets {}
will be replaced as follows:
{docstring_style}
— the value of the parameter chatgpt-docstrings.docstringStyle
{entity}
— the type of code entity, such as "function" or "class", based on the context
{code}
— the source code of the function or class for which the docstring will be generated
Example: Generate a google-style docstring for the following Python function code:\n def sum(x, y): ...
chatgpt-docstrings.requestTimeout
: The timeout in seconds to use when sending AI API requests.
chatgpt-docstrings.showProgressNotification
: Option to display a notification about the progress of docstring generation.
- Default value: true
- Available options:
chatgpt-docstrings.codeAnalyzer
: Which Python library to use for analyzing source files. Jedi is a third-party package. Jedi may not support the latest versions of Python. ast
is a module of the Python Standard Library. With ast
, syntax errors in the code are not allowed.
- Default value: "jedi"
- Available options:
chatgpt-docstrings.proxy
: The URL of the proxy server for AI API requests. The format of the URL is: <protocol>://[<username>:<password>@]<host>:<port>
. Where protocol
can be: 'http', 'https', 'socks5' or 'socks5h'. The username and password are optional. If not set, will be inherited from the http.proxy
setting.
- Default value: ""
- Examples:
http://proxy.com:80
http://127.0.0.1:80
socks5://user:password@127.0.0.1:1080
chatgpt-docstrings.proxyAuthorization
: The value to send as the Proxy-Authorization
HTTP header.
chatgpt-docstrings.proxyStrictSSL
: "Controls whether the proxy server certificate should be verified against the list of supplied CAs."
- Default value: false
- Available options:
Telemetry
This extension collects anonymous information related to the usage of the extension, such as well as performance and error data. You can disable telemetry as described here.
Change Log
See Change Log here
Feedback
Submit the issues if you find any bug or have any suggestion.
Contribution
Fork the repo and submit pull requests.
License
This extension is licensed under the MIT License