Skip to content

fix(config): coerce env var strings to booleans and numbers in settings validation#25641

Open
ixchio wants to merge 1 commit intogoogle-gemini:mainfrom
ixchio:fix/boolean-coercion-env-vars
Open

fix(config): coerce env var strings to booleans and numbers in settings validation#25641
ixchio wants to merge 1 commit intogoogle-gemini:mainfrom
ixchio:fix/boolean-coercion-env-vars

Conversation

@ixchio
Copy link
Copy Markdown

@ixchio ixchio commented Apr 18, 2026

Hey team! Ran into this while debugging a config issue.

What's going on

When you use env vars in settings.json like this:

{
  "ui": {
    "autoThemeSwitching": "${GEMINI_AUTO_THEME:-true}"
  }
}

The resolved value is the string "true" — not boolean true. Zod blows up with a validation error because it expects an actual boolean. Same thing happens with number fields getting string values from env vars.

How I fixed it

Added z.preprocess coercion in both schema-building paths (buildPrimitiveSchema and buildZodSchemaFromJsonSchema). Pretty straightforward:

  • "true" / "false"true / false (case-insensitive, whitespace-trimmed)
  • Numeric strings like "50"50
  • Everything else (like "yes" or "maybe") still fails validation as it should

This avoids the schema-traversal complexity that tripped up earlier PRs — just hooks into Zod's own preprocessing pipeline.

Why not the existing PRs?

Tests

Added 6 tests covering all the edge cases — coercion, case insensitivity, whitespace, rejection of non-boolean strings, and numeric coercion. All existing tests still pass.

Test Files  1 passed (1)
     Tests  31 passed (31)

Fixes #25573

cc @scidomino @NTaylorMullen would appreciate a look when you get a chance 🙏

…gs validation

When settings.json uses environment variable interpolation like
"${GEMINI_AUTO_THEME:-true}", the resolved value is the string "true"
rather than boolean true. Zod rejects this with a validation error,
effectively breaking env-based configuration for boolean and number
settings.

This adds z.preprocess coercion for boolean and number types in both
schema-building paths (buildPrimitiveSchema and buildZodSchemaFromJsonSchema).
Only "true"/"false" (case-insensitive, trimmed) are coerced to booleans —
arbitrary strings like "yes" still fail validation as expected.

Fixes google-gemini#25573
@ixchio ixchio requested a review from a team as a code owner April 18, 2026 22:44
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 18, 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 addresses a configuration validation issue where environment variables injected into settings are treated as strings rather than their intended types. By integrating Zod's preprocessing pipeline, the system now correctly coerces these strings into booleans and numbers, preventing validation errors and improving the robustness of the configuration loading process.

Highlights

  • Environment Variable Coercion: Implemented Zod preprocessing to automatically coerce string-based environment variables into their expected boolean or numeric types during settings validation.
  • Robust Parsing: Added case-insensitive matching and whitespace trimming for boolean strings, ensuring reliable configuration parsing.
  • Test Coverage: Added 6 new test cases to verify successful coercion of booleans and numbers, as well as the rejection of invalid input strings.
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 implements string-to-boolean and string-to-number coercion for configuration settings validation. By utilizing Zod's preprocess feature along with new helper functions coerceBooleanString and coerceNumberString, the system now correctly handles environment variables that resolve as strings (e.g., "true", "50"). The changes include comprehensive unit tests covering case-insensitivity, whitespace trimming, and numeric conversion. I have no feedback to provide.

@gemini-cli gemini-cli bot added area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Apr 18, 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 help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support boolean types when configuring settings.json via environment variables

1 participant