Skip to content

feat(core/cli): expose model thinking events in --output-format stream-json#22089

Closed
KobiAms wants to merge 3 commits intogoogle-gemini:mainfrom
KobiAms:feat/thinking-stream-json
Closed

feat(core/cli): expose model thinking events in --output-format stream-json#22089
KobiAms wants to merge 3 commits intogoogle-gemini:mainfrom
KobiAms:feat/thinking-stream-json

Conversation

@KobiAms
Copy link
Copy Markdown

@KobiAms KobiAms commented Mar 11, 2026

Summary

Fixes #22083

When using --output-format stream-json with a thinking-capable model (e.g. Gemini 2.5 Pro), model Thought events were silently dropped. The interactive UI already handles thinking via useGeminiStream, but the non-interactive stream-json path had no corresponding support.

  • Add THINKING = 'thinking' to JsonStreamEventType enum in packages/core/src/output/types.ts
  • Add ThinkingEvent interface (content: string, subject?: string) to the JsonStreamEvent union
  • Handle GeminiEventType.Thought in nonInteractiveCli.ts to emit structured thinking events in stream-json mode
  • Update docs/reference/configuration.md to document all stream-json event types including thinking

Example stream-json output with thinking:

{"type":"thinking","timestamp":"2025-...","content":"**Analysis** Let me think through this...","subject":"Analysis"}

Test plan

  • Run gemini -p "solve step by step: what is 15% of 240" --output-format stream-json with a thinking-capable model and verify thinking events appear in the output
  • Run npm run preflight — all checks pass (2 pre-existing flaky UI integration test timeouts unrelated to this change)
  • Verify non-thinking models are unaffected (no thinking events emitted when none are produced)

@KobiAms KobiAms requested review from a team as code owners March 11, 2026 22:53
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 11, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@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 --output-format stream-json functionality in the CLI by exposing model thinking events. Previously, these internal thought processes from models like Gemini 2.5 Pro were only visible in the interactive UI. Now, non-interactive users can also access these valuable insights, improving transparency and debugging capabilities for AI model interactions.

Highlights

  • Exposed Model Thinking Events: Introduced thinking events to the --output-format stream-json output, allowing non-interactive CLI users to observe model reasoning from thinking-capable models like Gemini 2.5 Pro.
  • New Event Type and Interface: Added THINKING as a new member to the JsonStreamEventType enum and defined a ThinkingEvent interface to structure these new events with content and an optional subject.
  • CLI Integration: Implemented logic in nonInteractiveCli.ts to capture GeminiEventType.Thought events and transform them into JsonStreamEventType.THINKING for streaming output.
  • Documentation Update: Updated the docs/reference/configuration.md to clearly document the new thinking event type and its purpose within the stream-json output format.
Changelog
  • docs/reference/configuration.md
    • Documented the new thinking event type for the stream-json output format, detailing its purpose and example usage.
  • packages/cli/src/nonInteractiveCli.ts
    • Added conditional logic to detect GeminiEventType.Thought events.
    • Implemented emission of JsonStreamEventType.THINKING events, formatting the thought content and subject.
  • packages/core/src/output/types.ts
    • Expanded the JsonStreamEventType enum to include THINKING.
    • Defined the ThinkingEvent interface with type, content, and optional subject properties.
    • Updated the JsonStreamEvent union type to incorporate ThinkingEvent.
Activity
  • No specific activity (comments, reviews, progress updates) has been recorded for this pull request yet.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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 support for 'thinking' events in the stream-json output format. However, a critical security vulnerability has been identified where these new events fail to apply necessary ANSI escape sequence sanitization, potentially allowing malicious model output to bypass security controls and lead to terminal-based attacks like phishing or command injection. This issue is addressed by applying stripAnsi to relevant fields, aligning with best practices for sanitizing display output. Additionally, there's an opportunity to improve the structure of the new ThinkingEvent for better machine-readability by separating data from presentation, suggesting a rename of content to description for clearer intent.


export interface ThinkingEvent extends BaseJsonStreamEvent {
type: JsonStreamEventType.THINKING;
content: string;
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 ThinkingEvent interface is part of a machine-readable stream-json API. Using content and pre-formatting it with markdown in nonInteractiveCli.ts mixes data with presentation. A cleaner API would be to provide the raw data components. I suggest renaming content to description to make the intent clearer and to align with the ThoughtSummary type from which this event is generated. This will allow consumers of the stream to handle formatting themselves.

Suggested change
content: string;
description: string;

@gemini-cli gemini-cli bot added the area/non-interactive Issues related to GitHub Actions, SDK, 3P Integrations, Shell Scripting, Command line automation label Mar 11, 2026
@KobiAms KobiAms force-pushed the feat/thinking-stream-json branch 2 times, most recently from b07a4da to 07e9dc6 Compare March 11, 2026 23:35
KobiAms added 2 commits March 12, 2026 01:51
Add THINKING event type to JsonStreamEventType enum and ThinkingEvent
interface in types.ts. Handle GeminiEventType.Thought in the
non-interactive stream loop to emit structured thinking events when
using --output-format stream-json.

Previously, thinking tokens from models like Gemini 2.5 Pro were
silently dropped in non-interactive mode. The interactive UI already
handled these via useGeminiStream, but the stream-json path had no
corresponding support. Developers building integrations can now
observe model reasoning in real time.

Fixes google-gemini#22083
…oughtSummary

- Rename ThinkingEvent.content to description to match the ThoughtSummary
  type and avoid mixing presentation into the machine-readable API
- Apply stripAnsi to subject and description fields in thinking events,
  consistent with how Content events are handled, to prevent ANSI
  injection from model output
- Respect getRawOutput/getAcceptRawOutputRisk flags for thinking events
@KobiAms KobiAms force-pushed the feat/thinking-stream-json branch from 07e9dc6 to 31345a7 Compare March 11, 2026 23:51
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli bot commented Mar 26, 2026

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383).

We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'.

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community!

@gemini-cli gemini-cli bot closed this Mar 26, 2026
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/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(cli): expose model thinking events in --output-format stream-json

1 participant