CodeRabbit Documentation - AI code reviews on pull requests, IDE, and CLI (original) (raw)

Generate docstrings

Automatically generate comprehensive docstrings that match your existing format, delivered through PR workflow for complete review control

Writing documentation for every function is time-consuming, but undocumented code creates bottlenecks during code reviews. You spend cycles explaining what functions do instead of focusing on business logic and architecture decisions. CodeRabbit generates docstrings for functions missing documentation in your PRs. Comment @coderabbitai generate docstrings or check the box in your PR walkthrough to trigger generation. The system scans your changes with ast-grep, identifies functions needing documentation, and generates docstrings that match your codebase’s existing format. Depending on platform support, CodeRabbit either opens a follow-up PR with the generated docstrings or posts them back as a PR comment for manual application. This handles the initial documentation grunt work while keeping you in control through the standard PR review process.

How it works

Docstrings PR example showing generated documentation in a pull request You review the generated PR like any other code change. CodeRabbit preserves existing docstrings and only documents functions that genuinely need it.

Platform support

Language support

Docstring generation supports 18+ languages via ast-grep: Bash, C, C++, C#, Elixir, Go, Java, JavaScript, Kotlin, Lua, PHP, Python, React TypeScript, Ruby, Rust, Swift, TypeScript Language support is provided by ast-grep. If your language is missing, consider making a contribution following the ast-grep language addition guide.

Configure per-directory styles

Different parts of your codebase need different documentation approaches. Use path-based instructions to match your team’s standards:

code_generation:
  docstrings:
    path_instructions:
      - path: "src/**/*.ts"
        instructions: |
          Use TSDoc format with @param, @returns, and @example tags.
          Include code examples for public API functions.
          Focus on behavior and edge cases.
      - path: "**/*test*"
        instructions: |
          Describe test purpose and expected behavior.
          Keep docstrings concise - focus on what is being tested.
      - path: "scripts/**/*.py"
        instructions: |
          Use Google-style docstrings with Args and Returns sections.
          Include usage examples for utility scripts.
      - path: "models/**/*.py"
        instructions: |
          Document database models with field descriptions and relationships.
          Include example queries and common usage patterns.

Path patterns use minimatch syntax to target specific directories, file types, or naming conventions.

Common configuration examples

- path: "components/**/*.tsx"
  instructions: "Document props, usage examples, and accessibility considerations"
- path: "api/**/*.{js,ts}"
  instructions: "Document request/response formats, status codes, and error handling"
- path: "utils/**/*"
  instructions: "Focus on input validation, return types, and edge case behavior"
- path: "models/**/*.py"
  instructions: "Document fields, relationships, and include example queries"

Reviewing generated docstrings

When CodeRabbit opens a docstring PR, focus your review on: Content accuracy:

Format consistency:

Completeness:

Most generated docstrings need minimal adjustment. Common tweaks include refining parameter descriptions or adding project-specific terminology.

Technical implementation

CodeRabbit uses ast-grep for precise code parsing across different programming languages and coding styles. This enables accurate function detection while respecting your code structure and automatically detecting your preferred docstring format.

What’s next