gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

CountTokensRequest.contents[2].parts[0].data: required oneof field 'data' must have one initialized field\n","status":"INVALID_ARGUMENT"

Open yashasvigirdhar opened this issue 1 year ago • 2 comments

What happened?

See the screenshot: Image

Note that this happened after I cancelled an ongoing operation above, so this might have to do something with what happens when user cancels the request in between, and not really a compress command.

What did you expect to happen?

No Error

Client information

  • CLI Version: 0.1.17
  • Git Commit: 99ba2f64 (local modifications)
  • Operating System: darwin v24.3.0
  • Sandbox Environment: no sandbox
  • Model Version: gemini-2.5-pro
  • Memory Usage: 352.4 MB

Login information

No response

Anything else we need to know?

No response

yashasvigirdhar avatar Aug 05 '25 16:08 yashasvigirdhar

After scrutinizing the countTokens 400 Bad Request error, I've narrowed down the root cause. The problem originates in src/core/client.js with this line:

const { text: summary } = await this.getChat().sendMessage(/* ... */);

The failure unfolds through several interlocked steps:

  1. Malformed Request: An inspection with Proxyman confirms the /countTokens API request is malformed. The payload contains an empty object in the parts array for contents[2]:

    {
      "role": "user",
      "parts": [{}]
    }
    

    Manually correcting this part to include a text field results in a successful API response.

  2. Immediate Cause: The empty part object is created because the summary variable becomes undefined. This is due to the destructuring assignment const { text: summary } = ... failing to find a text property on the object returned by sendMessage.

  3. Root Cause: The fundamental issue lies in the response from the preceding generateContent API call. The prompt 'First, reason in your scratchpad. Then, generate the <state_snapshot>.' is only yielding a single thought part, not a standard text part. The response is structured as follows:

    "content": {
      "parts": [
        {
          "text": "...",
          "thought": true
        }
      ],
      "role": "model"
    }
    

    This triggers a failure where the model doesn't correctly follow the second part of the instruction ('Then, generate the <state_snapshot>').

Conclusion:

The /compress command in the Gemini CLI is failing to generate the expected summary text. This leads to the undefined variable, the malformed request, and triggers the final INVALID_ARGUMENT error.

can-non avatar Aug 18 '25 16:08 can-non

After scrutinizing the countTokens 400 Bad Request error, I've narrowed down the root cause. The problem originates in src/core/client.js with this line:

const { text: summary } = await this.getChat().sendMessage(/* ... */);

The failure unfolds through several interlocked steps:

1. **Malformed Request:** An inspection with Proxyman confirms the `/countTokens` API request is malformed. The payload contains an empty object in the `parts` array for `contents[2]`:
   {
     "role": "user",
     "parts": [{}]
   }
       
         
       
   
         
       
   
       
     
   Manually correcting this part to include a `text` field results in a successful API response.

2. **Immediate Cause:** The empty part object is created because the `summary` variable becomes `undefined`. This is due to the destructuring assignment `const { text: summary } = ...` failing to find a `text` property on the object returned by `sendMessage`.

3. **Root Cause:** The fundamental issue lies in the response from the preceding `generateContent` API call. The prompt `'First, reason in your scratchpad. Then, generate the <state_snapshot>.'` is only yielding a single `thought` part, not a standard text part. The response is structured as follows:
   "content": {
     "parts": [
       {
         "text": "...",
         "thought": true
       }
     ],
     "role": "model"
   }
       
         
       
   
         
       
   
       
     
   This triggers a failure where the model doesn't correctly follow the second part of the instruction (`'Then, generate the <state_snapshot>'`).

Conclusion:

The /compress command in the Gemini CLI is failing to generate the expected summary text. This leads to the undefined variable, the malformed request, and triggers the final INVALID_ARGUMENT error.

Thanks a ton for the input, it's a life-saver. I edited ~/.gemini/tmp/<64-char-string>/checkpoint-<name-of-checkpoint>.json to remove the erroneous entry per your suggestion and it works now.

Hopefully future releases will have better internal handling for such issues that won't require this type of manual workaround.

jitmar avatar Aug 22 '25 12:08 jitmar