Skip to content

feat(cli): add 'gemini models list' with JSON output - #29404

Open
Kaushik2210 wants to merge 1 commit into
google-gemini:mainfrom
Kaushik2210:feat/models-list-command
Open

Kaushik2210 wants to merge 1 commit into
google-gemini:mainfrom
Kaushik2210:feat/models-list-command

Conversation

@Kaushik2210

Copy link
Copy Markdown

Summary

Adds a gemini models list subcommand so integrations can discover which models are valid for -m/--model without hardcoding IDs that go stale. The interactive /model dialog can't be parsed by external tools.

$ gemini models list -o json
{
  "currentModel": "auto",
  "models": [
    {
      "id": "auto",
      "name": "Auto",
      "description": "Let Gemini CLI decide the best model for the task: gemini-2.5-pro, gemini-2.5-flash",
      "inputTokenLimit": 1048576
    },
    { "id": "gemini-2.5-pro", "name": "gemini-2.5-pro", "description": "", "inputTokenLimit": 1048576 }
  ]
}

Without -o json it prints a human-readable list and marks the current model.

Design notes

  • Follows the skills list pattern: a models <command> parent registered via defer(), with list as its subcommand, and -o/--output-format text|json matching the existing global flag's spelling.
  • Reuses buildAvailableModels() (the function ACP mode uses) rather than reimplementing selection logic, so this list can't drift from what /model shows, and it covers both the legacy and experimental.dynamicModelConfiguration paths.
  • stdout is pure JSON: warnings such as "Project hooks disabled because the folder is not trusted" go to stderr, so gemini models list -o json | jq works.
  • Only inputTokenLimit is reported, from the existing tokenLimit() helper. The issue also mentions output limits, but the codebase has no source for those, so I didn't invent one.
  • Availability is local, not a live account query. It reflects your current settings and cached access state and does not trigger a network auth check, so plan or region differences that are only knowable from the server won't be reflected until they're cached.
  • Naming trade-off: like skills/hooks/mcp, a new top-level subcommand means a prompt whose first word is literally models is now parsed as the command. I kept the surface small by not adding a model alias.

Test plan

  • packages/cli/src/commands/models.test.ts and models/list.test.ts (7 tests): JSON shape, JSON-only stdout, text output with current-model marker, and yargs option wiring.
  • Ran the built CLI against a real config: gemini models list -o json returns valid JSON (checked by parsing stdout with stderr discarded), text mode renders correctly, -o xml is rejected by yargs, and bare gemini models prints help.
  • npx eslint on all touched files and npx tsc --noEmit on packages/cli: clean.
  • Added the command to the CLI commands table in docs/cli/cli-reference.md.

Fixes #27847

Integrations that wrap the CLI have no programmatic way to discover
which models are valid for -m/--model; the interactive /model dialog
can't be parsed, so callers hardcode model IDs that go stale.

Add a `models list` subcommand (following the `skills list` pattern)
that prints the available models as text, or as JSON with
`-o json` (currentModel plus id, name, description and inputTokenLimit
per model). It reuses buildAvailableModels(), the same function ACP
mode uses, so the list can't drift from what /model shows and covers
both the legacy and dynamic model configuration paths.

Fixes google-gemini#27847
@Kaushik2210
Kaushik2210 requested review from a team as code owners September 19, 2026 03:32
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request adds a new models list subcommand to the Gemini CLI. The primary purpose is to provide a reliable way for users and external integrations to query available models without relying on hardcoded IDs or parsing interactive dialogs. The command supports both human-readable text output and machine-readable JSON output, and it leverages existing model selection logic to ensure consistency with the current configuration.

Highlights

  • New CLI Subcommand: Introduced the gemini models list command to allow users and external tools to programmatically discover available models.
  • JSON Output Support: Added an -o json flag to the new command, enabling machine-readable output for integration with tools like jq.
  • Documentation Update: Updated the CLI reference documentation to include the new models list command.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the size/l A large sized PR label Sep 19, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 298
  • Additions: +298
  • Deletions: -0
  • Files changed: 6

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the gemini models list command to list available models in either text or JSON format, along with corresponding documentation and unit tests. The review feedback highlights two key improvement opportunities: handling potential undefined values from tokenLimit and m.name to prevent TypeScript compilation errors under strictNullChecks, and wrapping the subcommand handler in a try/catch block to gracefully handle errors and ensure a clean terminal exit.

Comment on lines +47 to +52
const models: ListedModel[] = availableModels.map((m) => ({
id: m.modelId,
name: m.name,
description: m.description ?? '',
inputTokenLimit: tokenLimit(m.modelId),
}));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The tokenLimit function can return undefined if a model's limit is not defined or if the model is unrecognized. Since inputTokenLimit in the ListedModel interface is strictly typed as a number, assigning a potentially undefined value will cause a TypeScript compilation error under strictNullChecks. Additionally, m.name may be optional in the model interface and should have a fallback to prevent potential runtime issues.

Suggested change
const models: ListedModel[] = availableModels.map((m) => ({
id: m.modelId,
name: m.name,
description: m.description ?? '',
inputTokenLimit: tokenLimit(m.modelId),
}));
const models: ListedModel[] = availableModels.map((m) => ({
id: m.modelId,
name: m.name ?? m.modelId,
description: m.description ?? '',
inputTokenLimit: tokenLimit(m.modelId) ?? 0,
}));
References
  1. When consuming an object, if a property is optional in its type definition (interface), callers must handle the undefined case (e.g., by providing a default with ??). Do not rely on the implementation details of the function that creates the object to always provide a value, as this can change. Code against the interface contract.

Comment on lines +88 to +94
handler: async (argv) => {
await handleList({
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
outputFormat: argv['outputFormat'] as ModelsListOutputFormat,
});
await exitCli();
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If handleList throws an error (e.g., due to configuration initialization failure or network issues), the command handler will fail with an unhandled promise rejection. This can cause the CLI to crash abruptly without restoring the terminal state or performing proper cleanup. Wrapping the execution in a try/catch block and calling exitCli(1) ensures a graceful exit and clean terminal state restoration.

  handler: async (argv) => {
    try {
      await handleList({
        // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
        outputFormat: argv['outputFormat'] as ModelsListOutputFormat,
      });
      await exitCli();
    } catch (error) {
      console.error(chalk.red(error instanceof Error ? error.message : String(error)));
      await exitCli(1);
    }
  },

@gemini-cli gemini-cli Bot added priority/p3 Backlog - a good idea but not currently a priority. area/non-interactive Issues related to GitHub Actions, SDK, 3P Integrations, Shell Scripting, Command line automation labels Sep 19, 2026
@Kaushik2210

Copy link
Copy Markdown
Author

Hi maintainers, this PR implements #27847 (gemini models list -o json). An earlier PR for this was auto-closed only for lacking help wanted, not on the merits. If the feature and the models list shape are welcome, could someone triage the issue and add the label so this is eligible for review? I'm happy to rework the design (naming, fields, flag) to match what you'd prefer. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/non-interactive Issues related to GitHub Actions, SDK, 3P Integrations, Shell Scripting, Command line automation priority/p3 Backlog - a good idea but not currently a priority. size/l A large sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add a command to list available models in machine-readable format (e.g., --list-models --json)

1 participant