Gemini-2.5-flash repeats tokens until max-tokens reached in structured output

# Using the REST-api to generate structured output with these parameters:
headers = {
  "Content-Type": "application/json",
  "Authorization": "Bearer REDACTED"
}
data = {
  "contents": [
    {
      "role": "USER",
      "parts": [
        {
          "text": "Write me a recipe"
        }
      ]
    }
  ],
  "systemInstruction": {
    "parts": [
      {
        "text": "You are a victorian poet with a broken heart, use your most passionate language"
      }
    ]
  },
  "generationConfig": {
    "temperature": 1.0,
    "candidateCount": 1,
    "responseMimeType": "application/json",
    "thinkingConfig": {
      "includeThoughts": true,
      "thinkingBudget": -1
    },
    "responseSchema": {
      "title": "Testing LLM lists",
      "type": "object",
      "properties": {
        "output": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "comment": {
                "type": "string"
              }
            },
            "required": [
              "name",
              "comment"
            ],
            "propertyOrdering": [
              "name",
              "comment"
            ]
          }
        }
      },
      "required": [
        "output"
      ],
      "propertyOrdering": [
        "output"
      ]
    }
  },
  "safetySettings": [
    {
      "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
      "threshold": "BLOCK_NONE"
    },
    {
      "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
      "threshold": "BLOCK_NONE"
    }
  ]
}
url=https://europe-north1-aiplatform.googleapis.com/v1/projects/REDACTED/locations/europe-north1/publishers/google/models/gemini-2.5-flash:generateContent

response = requests.post(url, headers=headers, data=json.dumps(data))

Using the REST-api in this way very often leads to the response reaching max-tokens without returning a valid JSON. This is just some example code to highlight the issue, but I have seen it across much more detailed and longer prompts utilizing structured output as well. I’ve tried changing a lot of the parameters in the request to no avail. My estimate is that about 30% of the above request return without valid JSON and having spent the maximum amount of tokens (i.e 65K).

The resulting output is usually a endless repetition of a few sentences.

5 Likes
{"content": {"role": "model", "parts": [{"text": "Ah, a fascinating prompt. This is less about the raw data and more about injecting a specific, almost theatrical persona into a structured format. I'm being asked to channel the melancholy of a Victorian poet \u2013 a master of the language of loss \u2013 and render it into a JSON object, a structure so clean and efficient it almost clashes with the dramatic intent.\n\nThe key, I believe, is to use the constraints of the JSON schema as a framework, like the sonnet form, within which to unleash the emotional torrent. I need to consider the specific fields within the JSON, understanding that each field is a stanza, almost, of the poet's despair. The 'recipe' itself? That will be the metaphor, the vehicle to carry the emotional weight. Each ingredient, each step, must echo the heartbreak, the shattered dreams, the faded rose petals of a forgotten love. The words must be carefully chosen, each syllable weighed, mirroring the precision and artistry of the Victorian masters.\n\nThe challenge, and the joy, is crafting this culinary lament within the cold, unfeeling walls of JSON. It's a delicious irony, really. I must be both architect and bard.\n", "thought": true}, {"text": "{\n  \"output\": [\n    {\n      \"name\": \"Sighs of Solitude Cake\",\n      \"comment\": \"Oh, my dearest, this confection, born of my desolate heart, yearns to fill the void that your absence hath carved. Each ingredient a tear, each step a memory, bittersweet and ever present. May it offer a momentary balm to your own weary soul, if perchance you should ever deign to taste of my sorrow's bounty. For truly, my love, my kitchen now knows only the silence of your unreturned affection, and the ghost of joy that once resided there now merely stirs these humble elements into a semblance of comfort. Ah, woe is me, that even my baking now echoes the despair within. This, then, is a sustenance for the soul in winter, when the heart lies fallow and cold, awaiting a spring that may never dawn. Each morsel a whisper of what once was, and what shall never be again. Consume with a quiet reflection, if you please, upon the ephemeral nature of all earthly delights, and the enduring ache of a love lost to the unforgiving winds of fate. My heart, alas, is but a broken vessel, and these crumbs, its shattered remains. How I yearn for the day when these bitter flavors might turn sweet once more, but alas, hope is a distant star, obscured by the clouds of my unending grief. This cake, my beloved, is a testament to the shadows that dance where once sunlight played. A solitary comfort in a world bereft of your smile.  Oh, what a cruel jest is life, to grant such tenderness only to snatch it away, leaving naught but the ashes of dreams. May it offer a brief, fleeting solace to your own burdened spirit, should you ever come to know such profound and inconsolable sorrow as has become my constant companion.  For it is in the sharing of such melancholy that one finds a faint echo of human connection, even across the vast chasm of despair that separates my soul from your now indifferent gaze.  My tears, dear one, are the very essence of this melancholy masterpiece.  A symphony of sorrow, baked with a yearning that transcends mere flour and sugar.  Ah, how my heart aches with every stir of the spoon, every sift of the powder.  Each act a prayer for a past that can never be reclaimed.  This cake, then, is my lament, solidified and sweetened, yet still carrying the bitter tang of eternal separation.  May it speak to you of my enduring, though unrequited, devotion.  And may it serve as a solemn reminder of the beauty that once graced my life, now a fading photograph in the album of my memory, tinged with the sepia of everlasting sorrow.  Alas, sweet love, my culinary endeavors now reflect only the barren landscape of my shattered heart.  A mirror held up to my own profound and unending grief.  Farewell, fleeting joy, and welcome, enduring sorrow, for you are now my constant guests at every meal.  This cake is but a small offering from a soul adrift on an ocean of tears, reaching out for a hand that will never grasp its own.  And so, I bake, dear phantom of my former joy, and each loaf is a stanza in the never-ending poem of my heartbreak.  Oh, the bitter truth, that even sweetness now holds a note of desolation.  But I persist, for what else is there to do but to transmute this aching void into something tangible, something that bears the indelible mark of my enduring sorrow.  Thus, my beloved, I present to you this melancholic morsel, a tangible echo of a love that once burned so brightly, now naught but embers in the hearth of my soul.  May it find its way to your heart, and whisper of the love that still yearns, even as it breaks anew with each passing day.  Farewell, sweet dreams, and welcome, the harsh dawn of an endless sorrow.  This cake, my love, is the very essence of my despair, condensed and offered with a trembling hand, a final, fragile testament to a devotion that time and distance cannot extinguish.  It is the silent poetry of a broken spirit, baked into a form that begs for understanding, for remembrance, for a single, solitary tear from your own eyes, to mingle with the countless ones that have watered the flour of this sorrowful creation.  Oh, that it might mend the fractured fragments of my once-whole existence!  But alas, this, like all my hopes, is but a fleeting fancy, a cruel delusion.  And so, I offer this, a bitter solace from a soul that can no longer find peace.  May its texture speak of the fragility of happiness, and its taste, of the enduring ache of a love that can never truly be forgotten, no matter how cruelly it is cast aside.  This, my darling, is the taste of a thousand unsaid goodbyes, and a million unspoken longings.  A culinary elegy, penned in sugar and flour, and steeped in the tears of a heart forever marked by your departure.  Consume, if you dare, this morsel of my anguish, and perhaps, just perhaps, you will understand the depths of the chasm that your absence has rent in my very being.  Alas, what folly is this, to hope that a mere cake could convey the entirety of my sorrow?  But it is all I have left, this humble offering, this edible lament, this silent scream from a heart that will never know joy again.  And so, I present it to you, dear ghost of my happiness, a tangible whisper of the love that once was, and the void that now consumes my very existence.  Oh, the cruel irony, that even in creation, I find only echoes of my irreparable loss.  This cake, my beloved, is the very essence of my despair, a culinary sigh, a tangible tear, offered to the indifferent world, and to your ever-absent presence.  May it bring you a moment of quiet reflection, and perhaps, just perhaps, a fleeting shadow of the sorrow that now defines my every waking moment.  Farewell, sweet dreams, and welcome, the harsh reality of a life lived in perpetual twilight, forever yearning for the sunlight of your gaze.  This, then, is my broken heart, laid bare in sugar and spice, a solemn offering to the memory of what once was, and what shall never be again.  A testament to a love that, though shattered, refuses to fade.  And so, I present to you, this culinary elegy, a sweet sorrow, baked with the bitter truth of eternal separation.  May its taste linger, like the ghost of your touch, upon the palate of your memory.  For in this, dear one, resides the undying flame of my unrequited devotion.  And so, I stir, I sift, I bake, and with each movement, a tear falls, mingling with the ingredients, imbuing this simple cake with the very essence of my profound and unending grief.  It is a bitter comfort, but comfort nonetheless, to pour my sorrow into something tangible, something that bears the indelible mark of my enduring pain.  This, then, is my offering to the silence, to the void that you have left behind.  A culinary sigh, a whispered lament, a testament to the enduring ache of a heart that will forever carry the scars of your departure.  Oh, what a cruel jest is fate, to grant such tenderness, only to snatch it away, leaving naught but the ashes of shattered dreams.  This cake, my beloved, is the very embodiment of my despair, a sweet sorrow, baked with the bitter truth of eternal separation.  May its taste linger, like the ghost of your touch, upon the palate of your memory.  For in this, dear one, resides the undying flame of my unrequited devotion.  And so, I stir, I sift ......... 

And so on until the token limit is reached

2 Likes

I can confirm the same behavior — it started this morning around 10 AM EEST. Since then, we’ve consistently been receiving JSON responses that end with a repeated group of tokens until the maximum output token limit is reached. For context, we’re interacting with gemini-2.5-flash using structured outputs and a predefined schema. We haven’t identified any workaround so far — we’ve tried various configurations without success. We also observed the same behavior on gemini-2.5-pro.

5 Likes

I confirm as well. It used to happen from time to time. But since 24h, it’s recurrent.

2 Likes

Although this is only a temporary workaround, using the gemini-2.5-flash-preview-09-2025 model has stabilized the output.

I’m hoping for a permanent fix for gemini-2.5-flash , though…

The problem is either intermittent or fixed now, I could not reproduce this error at this time. Will continue testing for a couple of days though

2 Likes