Skip to content

Commit e9aa8f0

Browse files
committed
feat: Disable safety filters by default and add --safety flag
The default safety settings of the Gemini API can be restrictive for certain queries, occasionally blocking responses that are not harmful. This commit disables content safety filters by default to provide a less restrictive user experience out-of-the-box. This is achieved by setting the `threshold` to `BLOCK_NONE` for harassment, hate speech, sexually explicit, and dangerous content categories. A new `--safety` command-line flag is introduced to allow users to re-enable the API's default safety mechanisms if they prefer. When the `--safety` flag is used, the custom safety settings are omitted from the request, and the API's standard filtering is applied.
1 parent 8bd7336 commit e9aa8f0

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

Changelog.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
### **Version 2.0.9**
2+
3+
This is a user-safety and content filtering release that gives users explicit control over the API's safety settings.
4+
5+
* **Features:**
6+
* **Default Safety-Off:** The client now automatically disables the API's safety filters by default. It sends `BLOCK_NONE` thresholds for `HARM_CATEGORY_HARASSMENT`, `HARM_CATEGORY_HATE_SPEECH`, `HARM_CATEGORY_SEXUALLY_EXPLICIT`, and `HARM_CATEGORY_DANGEROUS_CONTENT`. This prevents the API from preemptively blocking responses, which is often desirable in a direct command-line interface.
7+
* **Opt-In Safety Flag:** A new `--safety` command-line flag has been introduced. When this flag is used, the client does *not* send the custom safety settings, thereby re-enabling the Gemini API's default content filtering. This allows users to opt back into the default safety measures if they prefer.
8+
19
### **Version 2.0.8**
210

311
This is a major feature release that introduces the ability to use YouTube videos as direct context for analysis.
@@ -289,4 +297,4 @@ This release enhances security and refactors the credential input process for be
289297
* **Secure Origin Input:** The prompt for the API `Origin` now masks user input with asterisks, similar to the API key prompt. This prevents credentials from being accidentally exposed on-screen during the initial setup.
290298
* **Refactoring:**
291299
* **Modularized Secure Input:** A new `get_masked_input` helper function was created to handle all terminal-masked input. This abstracts the platform-specific (Windows/Unix) logic for disabling terminal echo.
292-
* **Simplified Credential Logic:** The `get_api_key_securely` function was refactored to use the new `get_masked_input` helper, which reduces code duplication and makes the credential-gathering process cleaner and more secure.
300+
* **Simplified Credential Logic:** The `get_api_key_securely` function was refactored to use the new `get_masked_input` helper, which reduces code duplication and makes the credential-gathering process cleaner and more secure.

gemini-cli.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ typedef struct AppState {
105105
char* save_session_path;
106106
char* final_code;
107107
char *host;
108+
bool safety;
108109
} AppState;
109110

110111
typedef struct {
@@ -2643,7 +2644,7 @@ typedef enum {
26432644
OPT_MODEL, OPT_HOST, OPT_SYSTEM, OPT_CONFIG,
26442645
OPT_TEMP, OPT_PROXY, OPT_SEED, OPT_MAX_TOKENS,
26452646
OPT_TOPK, OPT_TOPP, OPT_BUDGET,
2646-
OPT_EXECUTE, OPT_QUIET, OPT_NO_GROUNDING, OPT_FREE, OPT_NO_URL_CONTEXT,
2647+
OPT_EXECUTE, OPT_QUIET, OPT_NO_GROUNDING, OPT_FREE, OPT_NO_URL_CONTEXT, OPT_SAFETY,
26472648
OPT_LOC, OPT_MAP,
26482649
OPT_LIST_KEYS, OPT_ADD_KEY, OPT_REMOVE_KEY, OPT_CHECK_KEYS,
26492650
OPT_LIST_MODELS, OPT_LIST_SESSIONS, OPT_SAVE_SESSION, OPT_LOAD_SESSION,
@@ -2670,6 +2671,7 @@ static OptionType classify_option(const char *arg) {
26702671
if (!STRCASECMP(arg, "-ng") || !STRCASECMP(arg, "--no-grounding")) return OPT_NO_GROUNDING;
26712672
if (!STRCASECMP(arg, "-f") || !STRCASECMP(arg, "--free")) return OPT_FREE;
26722673
if (!STRCASECMP(arg, "-nu") || !STRCASECMP(arg, "--no-url-context")) return OPT_NO_URL_CONTEXT;
2674+
if (!STRCASECMP(arg, "--safety")) return OPT_SAFETY;
26732675
if (!STRCASECMP(arg, "--loc")) return OPT_LOC;
26742676
if (!STRCASECMP(arg, "--map")) return OPT_MAP;
26752677
if (!STRCASECMP(arg, "--list-keys")) return OPT_LIST_KEYS;
@@ -2804,6 +2806,10 @@ int parse_common_options(int argc, char *argv[], AppState *state) {
28042806
state->loc_tile |= 1;
28052807
break;
28062808

2809+
case OPT_SAFETY:
2810+
state->safety |= 1;
2811+
break;
2812+
28072813
case OPT_MAP:
28082814
state->loc_tile |= 2;
28092815
break;
@@ -3047,6 +3053,9 @@ void initialize_default_state(AppState* state) {
30473053
state->final_code = NULL;
30483054

30493055
state->loc_tile = 0;
3056+
3057+
state->safety = 0;
3058+
30503059
state->loc_gathered = false;
30513060

30523061
state->save_session_path = NULL;
@@ -3627,6 +3636,31 @@ cJSON* build_request_json(AppState* state) {
36273636
cJSON_AddItemToObject(root, "tools", tools_array);
36283637
}
36293638

3639+
// --- Add Safety Settings (Hardcoded to BLOCK_NONE) ---
3640+
if (!state->safety) {
3641+
cJSON* safety_settings_array = cJSON_CreateArray();
3642+
if (safety_settings_array) {
3643+
// CORRECTED: Use only the four universally supported categories.
3644+
const char* categories[] = {
3645+
"HARM_CATEGORY_HARASSMENT",
3646+
"HARM_CATEGORY_HATE_SPEECH",
3647+
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
3648+
"HARM_CATEGORY_DANGEROUS_CONTENT",
3649+
"HARM_CATEGORY_CIVIC_INTEGRITY"
3650+
};
3651+
int num_categories = sizeof(categories) / sizeof(categories[0]);
3652+
3653+
for (int i = 0; i < num_categories; i++) {
3654+
cJSON* setting_item = cJSON_CreateObject();
3655+
if (setting_item) {
3656+
cJSON_AddStringToObject(setting_item, "category", categories[i]);
3657+
cJSON_AddStringToObject(setting_item, "threshold", "BLOCK_NONE");
3658+
cJSON_AddItemToArray(safety_settings_array, setting_item);
3659+
}
3660+
}
3661+
cJSON_AddItemToObject(root, "safetySettings", safety_settings_array);
3662+
}
3663+
}
36303664
// --- 4. Add Generation Configuration ---
36313665
cJSON* gen_config = cJSON_CreateObject();
36323666
cJSON_AddNumberToObject(gen_config, "temperature", state->temperature);

0 commit comments

Comments
 (0)