Skip to main content
All right — let’s talk about documentation. Documentation can live inside the codebase (inline docstrings and comments) or separately as external docs (hosted sites, generated artifacts). This article demonstrates three practical approaches:
  • Inline documentation using docstrings (with a cleaned Flask example).
  • Using an AI assistant to generate or enhance documentation in your editor.
  • Creating documentation rules that guide the assistant so generated content is consistent.
Each section keeps the original examples and code blocks but improves explanatory flow and context to help you apply these patterns quickly.

Inline documentation (docstrings)

A reliable place to put documentation is directly in the source code using docstrings. Keep docstrings concise, follow PEP 8 style, and include purpose, parameters, return values, and short examples where appropriate. Below is a minimal Flask application fragment illustrating clear docstrings for a CSV reader and simple database helpers. Use this pattern to make functions self-describing and easier for both humans and tools to consume.
Key tips for inline docstrings:
  • Keep them short and factual.
  • Document each parameter and the return value.
  • Include small usage or output examples for clarity.
  • Prefer reStructuredText or Google-style docstrings depending on your project conventions.

Generating or enhancing docs with an AI assistant

An AI assistant integrated into your editor can:
  • Add or rewrite docstrings to follow your style guidelines.
  • Insert usage examples or expected outputs.
  • Reformat comments to match standards (e.g., PEP 8).
  • Add high-level headers for modules and tests.
When requesting changes from the assistant, be explicit about the format (Google-style, NumPy-style, or reST), required sections (Args, Returns, Example), and any repository-level rules to enforce. Example route handlers below illustrate how the assistant might generate concise route docstrings and include parameter descriptions and return behavior.
For quick reference, here are the example endpoints described above: When working with the assistant:
  • Provide the target file(s) and preferred doc style.
  • Specify which rules should be applied (e.g., PEP 8, required sections).
  • Iterate: ask for refinements or stricter enforcement if the first result is not acceptable.

Using model selection and agent mode

Many editor integrations let you choose both the AI model and the mode of operation (for example, an “ask” mode for single requests or an “agent” mode for multi-step tasks). Different models and modes can produce varying levels of detail or adherence to style rules. Below is an example screenshot from an editor where the user hovers over the model selection dropdown before requesting documentation for a file. This illustrates the typical UI pattern: file view on the left, chat assistant on the right, and a model dropdown you can change before making the request.
A dark-mode screenshot showing a documentation file in an editor on the left and a chat interface on the right with a model-selection dropdown, the cursor hovering over "claude-3.5-sonnet."
After selecting a model and running a documentation request, the assistant will often:
  • Add module headers.
  • Generate or rewrite docstrings for functions and tests.
  • Enumerate fixtures and explain expected behavior.
  • Provide examples that follow your rules file.
Choose a model that balances accuracy and verbosity for your needs; lighter models may be faster but provide less context, while larger models often generate more complete examples and higher-quality prose.

Creating documentation rules to guide the AI

To produce consistent outputs, create a repository-level rules file the assistant can read and apply. Rules can enforce language style, required docstring sections, formatting, and whether examples are mandatory. Below is an example Python snippet that creates a documentation rule file. The assistant can use this file to enforce concise language, PEP 8 compliance, and inclusion of examples.
A compact summary table for rule considerations: Once rule files are added to the workspace, most assistants will allow you to auto-attach them or explicitly apply them when generating docs. Scope rules to specific file patterns and decide whether they apply automatically or on-demand.
Best practice: Keep rules concise and focused on measurable formatting and content requirements (for example, required sections, parameter/return descriptions, and example usage). Clear, testable rules produce more consistent generated documentation.

Best practices for AI-assisted documentation

  • Keep your requests to the assistant specific: target a file, state the docstring style, and describe required sections.
  • Include examples in rule files so the assistant knows the expected snippet format.
  • Group related information within docstrings: purpose, args, returns, and a short example.
  • Review and iterate: ask the assistant to refine outputs until they match your standards.
  • Version your documentation rules and store them in the repository so teams share the same conventions.
References and further reading: Using clear inline docstrings along with well-defined documentation rules helps teams produce consistent, searchable, and maintainable documentation that benefits current and future maintainers.

Watch Video