Skip to content

feat(cli): add JSON output support for list-sessions#24690#24711

Open
Khalodddd wants to merge 2 commits intogoogle-gemini:mainfrom
Khalodddd:main
Open

feat(cli): add JSON output support for list-sessions#24690#24711
Khalodddd wants to merge 2 commits intogoogle-gemini:mainfrom
Khalodddd:main

Conversation

@Khalodddd
Copy link
Copy Markdown

Summary

This PR resolves #24690 by adding support for the --output-format json flag to the list-sessions command. Previously, the command would only output human-readable text, regardless of the format flag, making it difficult to use session data in automated scripts or third-party integrations.

Problem

The listSessions function was previously hardcoded for string-based terminal output only. Additionally, the generateSummary function was called unconditionally, which often produced background logs (e.g., "Generating summary...") that would corrupt the JSON output if a user tried to pipe the data to another tool like jq.

Details

  • JSON Support: Modified packages/cli/src/utils/sessions.ts to detect OutputFormat.JSON from the config.
  • Output Cleanliness: Wrapped the generateSummary call in a conditional check. It is now skipped when JSON is requested, ensuring a clean data stream to stdout.
  • Refactoring: Extracted the sorting logic into a sortedSessions constant. This ensures that the order of sessions (sorted by startTime) is identical in both text and JSON modes.
  • Formatting: Used JSON.stringify(sortedSessions, null, 2) for standardized, pretty-printed JSON.

Related Issues

Fixes #24690

How to Validate

  1. Build the bundle:
    npm run bundle
  2. Test command:
    node .\bundle\gemini.js --list-sessions --output-format json

See attached Image for validation

Screenshot 2026-04-05 171405

@Khalodddd Khalodddd requested a review from a team as a code owner April 5, 2026 14:16
@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 enhances the list-sessions command by introducing JSON output support, enabling better integration with external tools and scripts. It ensures that the output remains clean by conditionally suppressing auxiliary summary logs when JSON mode is active and standardizes the session sorting logic for consistency.

Highlights

  • JSON Output Support: Added support for the --output-format json flag to the list-sessions command to facilitate automated data processing.
  • Output Cleanliness: Wrapped the generateSummary call in a conditional check to prevent background logs from polluting the JSON output.
  • Refactoring: Extracted session sorting logic into a constant to ensure consistent ordering across both text and JSON output formats.
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 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 counter productive. 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.

Copy link
Copy Markdown
Contributor

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

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 JSON output support for the listSessions command and prevents summary generation when JSON is requested. The review feedback identifies a redundant sorting operation that can be removed and suggests using grapheme-aware string truncation to avoid issues with multi-byte Unicode characters in the terminal output.

Comment on lines +30 to +32
const sortedSessions = sessions.sort(
(a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime(),
);
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 SessionSelector.listSessions() method already returns an array of sessions sorted by startTime (ascending), as implemented in the underlying getSessionFiles utility. Re-sorting the array here is redundant and adds unnecessary complexity. You can simply use the sessions array directly.

  const sortedSessions = sessions;

@gemini-cli gemini-cli bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p3 Backlog - a good idea but not currently a priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--list-sessions does not support --output-format json

1 participant