Structured JSON Output
Motivation
Gemini CLI is a powerful tool for interactive use. However, its current plain-text output format, while excellent for human readability, presents a significant barrier in non-interactive use. To unlock the next level of integration and enable developers to build robust tools and automation on top of the CLI, we must provide a stable, machine-readable output format.
This proposal outlines a foundational feature: a structured JSON output mode. This will establish a clear API contract for developers, allowing them to reliably parse session results, including responses, errors, and performance statistics. Adopting this feature will unblock a wide range of advanced use cases in CI/CD integrations, automations and third-party tool development.
Proposal
Introduce both a command line argument and a persistent configuration setting to control the output format for the CLI. This provides both runtime control for automation and a persistent preference for users who prefer JSON output.
CLI Argument
Add a new --output-format flag that accepts two values:
"text"(Default): Preserves the existing behavior, outputting a human-readable, plain-text response."json": Outputs a single, structured JSON object to stdout containing the complete result of the session.
Usage Example:
gemini --output-format json --prompt "What is machine learning?"
Configuration Setting
Introduce a new, persistent configuration setting, output.format, which will govern the default output format for the CLI when no command line flag is specified. The setting will accept two values:
"text"(Default): Preserves the existing behavior, outputting a human-readable, plain-text response."json": Outputs a single, structured JSON object to stdout containing the complete result of the session.
Configuration Example:
{
"output": {
"format": "json"
}
}
JSON Schema Definition
When output.format is set to "json", the CLI will produce a JSON object adhering to the following schema.
Top-Level Object
| Key | Type | Description |
|---|---|---|
response |
string | null |
The plain-text response from the model. Null if an error occurred. |
stats |
object |
An object containing performance and usage statistics for the session. |
error |
object | null |
An object containing error details. Null if the session was successful. |
stats Object
| Key | Type | Description |
|---|---|---|
session |
object |
Contains session-wide stats, such as duration in milliseconds. |
model |
object |
Contains model-related stats, such as the number of turns. |
tools |
object |
Contains tool-related stats, such as the number of calls. |
user |
object |
Contains user-related stats, such as the number of turns. |
error Object
| Key | Type | Description |
|---|---|---|
type |
string |
A machine-readable error type (e.g., BadRequestError, FatalAuthenticationError). |
message |
string |
A human-readable error message. |
code |
number | null |
The process exit code, if the error was fatal. |
JSON Output Examples
Successful Output:
{
"response": "I found the following files: ...",
"stats": {
"session": { "duration": 1234 },
"model": { "turns": 2 },
"tools": { "calls": 1 },
"user": { "turns": 1 }
},
"error": null
}
Error Output:
{
"response": null,
"stats": {
"session": { "duration": 25 },
"model": { "turns": 0 },
"tools": { "calls": 0 },
"user": { "turns": 1 }
},
"error": {
"type": "FatalAuthenticationError",
"message": "Authentication failed. Please check your credentials.",
"code": 41
}
}
Future Extensibility
This design is intentionally foundational, providing a clear path for future enhancements.
Extending the output Configuration
The output object in the configuration is the designated namespace for all output-related settings. Future additions could include:
output.styles: For theming and customizing the color and style of the text output.output.redact: For defining patterns to automatically redact from the output.
Extending the JSON Output
The top-level JSON object can be gracefully extended with new keys for more detailed analysis. A prime candidate for future inclusion is:
conversation: An optional key containing an array of the full conversation history. This would provide a complete, self-contained record of the session for deep analysis and could be enabled by a verbosity setting.
cc @leehagoodjames @NTaylorMullen @allenhutchison @abhipatel12
This issue is a well-defined feature request for structured JSON output to improve integration with other tools. It is classified as an enhancement for extensibility with a medium priority.
In #6707 we are planning to remove the --prompt flag and convert the --prompt-interactive flag to --interactive. Some other tools use --p for --print or --print-format and that would be an excellent place to introduce different output formats for our tool as well. I think defining the output is more of a runtime thing than a config thing and to me is seems better positioned as a command line flag.
In https://github.com/google-gemini/gemini-cli/issues/6707 we are planning to remove the --prompt flag and convert the --prompt-interactive flag to --interactive. Some other tools use --p for --print or --print-format and that would be an excellent place to introduce different output formats for our tool as well.
@allenhutchison when you remove --prompt flag, what will you replace it with? is it --print? or is the idea that it defaults to non-interactive, and only starts interactively when you pass in --interactive?
I think defining the output is more of a runtime thing than a config thing and to me is seems better positioned as a command line flag.
I was thinking we can have the flag for runtime configuration but also support a setting for persistent configuration, especially for automation and for users who prefer json output whenever they run gemini cli non-interactively -- happy to start with runtime only though, and get user feedback on whether they'd like a persistent setting
Found possible duplicate issues:
- #8500
If you believe this is not a duplicate, please remove the status/possible-duplicate label.
Hi @jerop thanks so much for adding this! This is so useful and helpful.
I've been trying it out and and have been wondering if it should exit upon tool error.
My understanding is some tool errors are to be expected, and the CLI should at least try to self correct.
Perhaps we should not fail upon tool error for now?
@capachino this can be handled in downstream systems where they can retry when an error is not fatal, but could be interesting to model this upstream as well - do you mind opening an issue for this?
@capachino this can be handled in downstream systems where they can retry when an error is not fatal, but could be interesting to model this upstream as well - do you mind opening an issue for this?
Thanks, opened #9281