Gemini-3.1-pro-preview – Truncated Responses & Code with finishReason: "STOP" in AI Studio

I’ve been experiencing a consistent bug for the past few days with gemini-3.1-pro-preview in Google AI Studio. The model frequently returns incomplete or truncated responses — especially when generating code or structured answers — even though the dashboard log traces show a normal finishReason: "STOP". This is not a UI issue; the backend response itself is cut off mid-sentence or mid-block.

Exact message ID** (visible in the dashboard traces/logs): bjPJaduaAqGBmtkP_KHGoQI

{
    "model": "models/gemini-3.1-pro-preview",
    "temperature": 1,
    "topK": 64,
    "topP": 0.95,
    "endTokens": [],
    "tokenLimits": 65536,
    "responseMimeType": "text/plain",
    "safetyCatFilters": [
        {
            "category": "HARM_CATEGORY_HARASSMENT",
            "threshold": "OFF"
        },
        {
            "category": "HARM_CATEGORY_HATE_SPEECH",
            "threshold": "OFF"
        },
        {
            "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
            "threshold": "OFF"
        },
        {
            "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
            "threshold": "OFF"
        }
    ],
    "enableCodeExecution": false,
    "enableFunctionCalling": false,
    "functionDeclarations": [],
    "enableAutoFunctionResponse": false,
    "enableSearchAsATool": true,
    "googleSearch": [],
    "enableBrowseAsATool": false,
    "responseModalities": []
}

The response stops abruptly. The output is clearly missing the remainder of the code block or explanation. Retrying the same prompt (or slight variations) sometimes succeeds, sometimes fails again at the same point. Token usage is well under the 65k limit.

This matches symptoms reported in these related threads (but my case includes the exact message ID and full request parameters for easier reproduction):

3 Likes

You’ve provided excellent diagnostic data here. The fact that your finishReason is “STOP” while the output is truncated confirms this is a downstream rendering/buffer issue in the AI Studio Canvas rather than a model context limit.

This appears to be a known regression in the 3.1 Pro Preview (March 2026 build) affecting how structured code blocks are streamed. Here are a few workarounds the community has found effective while we wait for a backend patch:

1. Toggle “Raw Mode”

In the top right of the response window, try toggling the “Raw” view. Sometimes the Markdown renderer in AI Studio hangs on specific character sequences (like [] or nested brackets in TypeScript/Dart), causing the UI to stop displaying text even though the full string was received.

2. Increase the “Thinking Budget”

If you are using the “Thinking” features of 3.1 Pro, ensure your Thinking Budget (under Settings) is set to the maximum. Users have reported that if the model exhausts its reasoning time, it occasionally forced-closes the stream prematurely, leading to truncated code blocks.

3. The “Scroll Refresh” Trick

Strangely, some users have found that the text is actually there but “invisible” in the DOM. Try scrolling to the top of the chat and then back down to the bottom; this can sometimes force the container to re-render the full message.

4. Immediate Follow-up

Since the model thinks it finished (hence the STOP reason), it usually has the full response in its immediate KV cache. If you reply with “Continue from [last visible line]”, it almost always completes the block perfectly.

For the Google Team: Your message ID bjPJaduaAqGBmtkP_KHGoQI is the most helpful part of this post. I’d recommend also submitting this via the “Send Feedback” (cog icon) in AI Studio, as that attaches the internal trace logs that help the engineering team identify which specific node in the inference pipeline is dropping the packets.

1 Like

Thanks for your detailed response. Unfortunately it is not a downstream rendering/buffer issue in the AI Studio Canvas. I have Gemini API Logs and Datasets enabled, so I have access to the traces by going to https://aistudio.google.com/app/logs.

Here is an example of truncated response. Look at "finishReason": "STOP",. And look at how the parts content is truncated in the middle of the response and how the subsequent "text" parts are all weirdly empty. This is an inference and model issue.

ID: LUjKabnoJLywqtsPzKzpmAc

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "**Analyzing Execution Efficiency**\n\nI'm currently focused on the challenge of optimizing type-guard conditionals in JavaScript, specifically aiming for V8 engine efficiency. My approach involves analyzing how V8 executes different condition orderings to identify the most cost-effective arrangements. I am trying to determine the minimal runtime impact of various approaches.",
            "thought": true
          },
          {
            "text": "\n\n\n**Optimizing Conditional Placement**\n\nI'm now zeroing in on the optimal placement for `!(\"type\" in contentBlock)` within the conditional. My analysis indicates its cost is moderate, and it throws a TypeError when used on a non-object. The crucial part is figuring out where to place it relative to the `typeof` and `null` checks to minimize execution time and avoid errors. I am also currently benchmarking different orders to determine which results in better efficiency when the condition is most likely true.",
            "thought": true
          },
          {
            "text": "If `contentBlock` is an array (e.g., `"
          },
          {
            "text": "[1, 2, 3]`), `!(\"type\" in contentBlock)` will evaluate to `true`. The statement will short-circuit"
          },
          {
            "text": " right there, jump into the `if` block, and `continue`. It will never even reach `Array.isArray"
          },
          {
            "text": "()`.\n\nIf you leave `Array.isArray` at the very end, the only time V8 will ever execute"
          },
          {
            "text": " it is if an object *has* a `\"type\"` property, but happens to be a heavily mutated array (e"
          },
          {
            "text": ".g., `const arr ="
          },
          {
            "text": ""
          },
          {
            "text": ""
          },
          {
            "text": ""
          },
          {
            "text": ""
          },
          {
            "text": ""
          },
          {
            "text": "",
            "thoughtSignature": "EsNQCs..."
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "groundingMetadata": {}
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 3223,
    "candidatesTokenCount": 980,
    "totalTokenCount": 6461,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 3223
      }
    ],
    "thoughtsTokenCount": 2258
  },
  "turnToken": "v1_ChdDa1hLYVpuUUZ0S28wX3dQeXM2SDhRSRIXTFVqS2Fibm9KTHl3cXRzUHpLenBtQWM"
}

I have the same issue. Gemini 3 pro preview in Google AI studio has some issues with code truncation. It in many cases happens with the specific sequence :[] (a colon followed by an empty array bracket) at the end of the ternary operator, not only in typescript /dart, as it happened to me just this morning with a QML file. The model just stops outputting code.
This never happens in https://gemini.google.com/app
So either the gemini 3.1 pro in https://gemini.google.com/app and the gemini 3.1 pro preview in google ai studio are different models, or if they are the exact same mode, then the issue is not in the model itself, but rather in what’s built around the model in google ai studio.
Toggling raw mode doesn’t help at all, and honestly makes the platform unreadable.
This bug has been happening since 3.1 pro was released.. I feel like nobody is trying to fix it because it’s odd for a company with such good enginners as google to not be able to fix a bug for such a long time.
I’ve also reported it in feedback section of google ai studio, together with other bugs reports, but I guess they really don’t care about the google ai studio platform

Yes, I have noticed this and I can confirm this correlation.

Are you by any chance from Brazil (or Latin America) as well? AI Studio may automatically route requests based on our location, latency, and load-balancing.

I’m in italy. I doubt it has anything to do with location, the routing is probably the standard one used by google across their network for most products

I’m in the process of developing a Gemini API client and during that process, diagnosed what was causing this in my client.

Apparently, when my custom client is set to allow the model to utilize the “Grounding with Google Search” ability, Google’s backend parser aggressively scans the raw text stream for the left square bracket character ([) to build citation links. Most of the time, if the model generates the bracket in its thoughts or output block, such as for a Python list or JSON array, the parser interrupts the model (prematurely closing thought tags or truncating response generation, depending on where it happened).

The only exception I saw was some instances in the model’s thought block where the deadly bracket was presented inline and inside of what might have been code tags (the font changed). That thought block didn’t exit prematurely. But having it in a markdown block (such as “Python”) or in general output buys the farm.

The consumer web UI doesn’t have the problem, but in my app where it uses an API key, it’s in full effect. It is definitely a STOP action (caught it in my console logs). It is resolved 100% by turning off the search ability.

2 Likes

In my experience it doesn’t ALWAYS cause the model to stop, but most of the times it does stop. I have gotten to go around it by asking the model to kindly write the same file but with slightly different syntax in that exact line where it truncated the file.
Anyways the squared brackets are not the sole cause. Most of the times it is a combo of squared brackets in netsted code parts. The code part where files get truncated looks almost always the same, and it’s crazy that they have not fixed this in such long time that the issue has been happening.

Either it’s caused by model dilution prevention efforts on google side (eg making some responses bad on purpose to ruin the efforts of illegal competitors trying to dilute model) or they are just not even trying, not sure which one is worse. Today this issue happened 4 times already, it’s SO annoying and results in so many wasted tokens.

Thanks dude, turning off search works.

But this is still just a temporary fix as turning the search tool on is “risky” now.

ye but grounding with google search gives such better quality of answers in most cases..

Anyways it’s inconsistent, because there are times when it doesn’t block on square bracket character even with grounding with google search on, for example it just managed to generate this part of code for me, with search on, without issues:

// 3. Sort inside clusters chronologically, and determine representative
List<List<Anime>> clusteredLists =\[\];

for (var clusterIds in clusters) {

So clearly it’s very inconsistent and there must be more factors at play than what we can see. This is just one of those lucky times though, the next 10 times it will get stuck and truncate the code lol

1 Like

It’s just freaking sad that, even after our detailed report, no Google representative came forward to say anything.

By the way, I searched Reddit and elsewhere, and it doesn’t seem like many people are complaining about this bug, which makes this elusive/intermittent bug more frustrating.

1 Like

Even my post about markdown code blocks disappearing in long code replies got ignored. That one is a bug that is reproducible 100% of the times, so I really don’t know what’s going on here, I’m confused, maybe this is not the right platform to report issues and bugs, and I just might have misunderstood what it is meant for?
It would have been enough to say “hey guys we know of these issues and they will get fixed eventually”, like at least let us know that some people at google are aware of them and will get to fixing them at some point..

+1 having same issue, only with gemini 3.1 pro in ai studio

1 Like