Code Suggestions

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

Code Suggestions is available for:

  • GitLab Duo Agent Platform. Billing is usage-based.
  • GitLab Duo Core, Pro, or Enterprise, GitLab Duo with Amazon Q. Billing is based on your add-on.

Use GitLab Duo Code Suggestions to write code more efficiently by using generative AI to suggest code while you’re developing.

Prerequisites

To use Code Suggestions:

GitLab Duo requires GitLab 17.2 or later. For GitLab Duo Core access, and for the best user experience and results, upgrade to GitLab 18.0 or later. Earlier versions might continue to work, however the experience might be degraded.

Use Code Suggestions

To use Code Suggestions:

  1. Open your Git project in a supported IDE.

  2. Add the project as a remote of your local repository using git remote add.

  3. Add your project directory, including the hidden .git/ folder, to your IDE workspace or project.

  4. Author your code. As you type, suggestions are displayed. Code Suggestions provides code snippets or completes the current line, depending on the cursor position.

  5. Describe the requirements in natural language. Code Suggestions generates functions and code snippets based on the context provided.

  6. When you receive a suggestion, you can do any of the following:

    • To accept a suggestion, press Tab.
    • To accept a partial suggestion, press either Control+Right arrow or Command+Right arrow.
    • To reject a suggestion, press Esc. In Neovim, press Control+E to exit the menu.
    • To ignore a suggestion, keep typing as you usually would.

View multiple code suggestions

For a code completion suggestion in VS Code, multiple suggestion options might be available. To view all available suggestions:

  1. Hover over the code completion suggestion.
  2. Scroll through the alternatives. Either:
    • Use keyboard shortcuts:
      • On a Mac, press Option+[ to view the previous suggestion, and press Option+] to view the next suggestion.
      • On Linux and Windows, press Alt+[ to view the previous suggestion, and press Alt+] to view the next suggestion.
    • On the dialog that’s displayed, select the right or left arrow to see next or previous options.
  3. Press Tab to apply the suggestion you prefer.

Code completion and generation

Code Suggestions uses code completion and code generation:

Code completionCode generation
PurposeProvides suggestions for completing the current line of code.Generates new code based on a natural language comment.
TriggerTriggers when typing, usually with a short delay.Triggers when pressing Enter after writing a comment that includes specific keywords.
ScopeLimited to the current line or small block of code.Can generate entire methods, functions, or even classes based on the context.
AccuracyMore accurate for small tasks and short blocks of code.Is more accurate for complex tasks and large blocks of code because a bigger large language model (LLM) is used, additional context is sent in the request (for example, the libraries used by the project), and your instructions are passed to the LLM.
How to useCode completion automatically suggests completions to the line you are typing.You write a comment and press Enter, or you enter an empty function or method.
When to useUse code completion to quickly complete one or a few lines of code.Use code generation for more complex tasks, larger codebases, when you want to write new code from scratch based on a natural language description, or when the file you’re editing has fewer than five lines of code.

Code Suggestions always uses both of these features. You cannot use only code generation or only code completion.

View a code completion vs. code generation comparison demo.

Best practices for code generation

To get the best results from code generation:

  • Be as specific as possible while remaining concise.
  • State the outcome you want to generate (for example, a function) and provide details on what you want to achieve.
  • Add additional information, like the framework or library you want to use.
  • Add a space or new line after each comment. This space tells the code generator that you have completed your instructions.
  • Review and adjust the context available to Code Suggestions.

For example, to create a Python web service with some specific requirements, you might write something like:

# Create a web service using Tornado that allows a user to sign in, run a security scan, and review the scan results.
# Each action (sign in, run a scan, and review results) should be its own resource in the web service
...

AI is non-deterministic, so you may not get the same suggestion every time with the same input. To generate quality code, write clear, descriptive, specific tasks.

For use cases and best practices, follow the GitLab Duo examples documentation.

Available language models

Different language models can be the source for Code Suggestions.

Accuracy of results

We are continuing to work on the accuracy of overall generated content. However, Code Suggestions might generate suggestions that are:

  • Irrelevant.
  • Incomplete.
  • Likely to result in failed pipelines.
  • Potentially insecure.
  • Offensive or insensitive.

When using Code Suggestions, code review best practices still apply.