Unterminated String for Long Outputs

Dear Gemini team,

Please fix this issue with the model. I have a use case where it is generating a long string using structured output, the issue is after a while I tend to get this error in my code:

 File "C:\Python312\Lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\json\decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) ^^^^^^^^^^^^^^^^^^^^^^ json.decoder.JSONDecodeError: Unterminated string starting at: line 134 column 19 (char 11266) 

I believe I know why this is occurring. The model has a bug where it just glitches and then repeats the same thing over and over again “ex. /text/text/text” and just goes on forever, and when it returns that of course it isn’t properly formatted with the intended structured output, so it errors out.

There is no way my output reached the output token limit of 60k tokens. Gemini has been used to generate entire code files, there’s no way my output was bigger than that.

Dear Gemini Team,

I’m experiencing an issue where Gemini 2.5 generates large structured outputs, but occasionally repeats text endlessly, producing unterminated strings that break JSON parsing:

json.decoder.JSONDecodeError: Unterminated string starting at … 

This happens even when the output is below the token limit. It seems like a model-level glitch causing invalid JSON.

Request: Please investigate this behavior and consider adding internal checks to ensure the model returns complete, valid JSON without repeating patterns.

Thank you,
devi kathirvel

1 Like

Hi @Firley_Company ,

Could you please let us know which Gemini Model you are using?
Also could you please share the minimum reproducible code ?

Hi @Mrinal_Ghosh

The model I was using was gemini-2.5-flash
The issue also is noticeable in 2.5-flash-lite and I have not used 2.5-pro for my use case.

Here is the minimal reproducible code:

import asyncio import os import json import google.generativeai as genai from google.generativeai import types GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY') if not GOOGLE_API_KEY: raise ValueError("GOOGLE_API_KEY environment variable not set.") genai.configure(api_key=GOOGLE_API_KEY) SCHEDULE_GENERATOR_PROMPT = """ You are an expert academic planner. Your task is to generate a comprehensive, day-by-day study schedule based on the user's provided context. RULES: - You MUST generate a schedule for every single specified study day between the start and end dates. Do not skip any days. - For each day, create between 3 to 5 distinct activities (e.g., reading, video, project, quiz). - Each activity must have a detailed, multi-sentence description. - Ensure the final output is a single, valid JSON object that strictly adheres to the provided schema. Do not add any text or explanations outside of the JSON structure. - The JSON output must be complete and well-formed. """ schedule_output_schema = types.Schema( type=types.Type.OBJECT, properties={ 'course_title': types.Schema(type=types.Type.STRING), 'schedule': types.Schema( type=types.Type.ARRAY, items=types.Schema( type=types.Type.OBJECT, properties={ 'date': types.Schema(type=types.Type.STRING, description="Date in YYYY-MM-DD format."), 'day_of_week': types.Schema(type=types.Type.STRING), 'daily_goal': types.Schema(type=types.Type.STRING, description="A concise goal for the day."), 'activities': types.Schema( type=types.Type.ARRAY, items=types.Schema( type=types.Type.OBJECT, properties={ 'type': types.Schema(type=types.Type.STRING, enum=['Reading', 'Video', 'Project', 'Quiz', 'Practice']), 'topic': types.Schema(type=types.Type.STRING, description="Specific topic for the activity."), 'description': types.Schema(type=types.Type.STRING, description="A detailed, 2-3 sentence description of the task."), 'duration_minutes': types.Schema(type=types.Type.INTEGER, description="Estimated time in minutes."), 'url': types.Schema(type=types.Type.STRING, description="An optional relevant URL."), }, required=['type', 'topic', 'description', 'duration_minutes'] ) ) }, required=['date', 'day_of_week', 'daily_goal', 'activities'] ) ) }, required=['course_title', 'schedule'] ) async def generate_long_schedule(): print("Attempting to generate a long study schedule from Gemini...") prompt_context = """ Course Name: Advanced Quantum Computing Start Date: 2025-09-01 End Date: 2025-12-31 Study days: Monday, Wednesday, Friday Student's Custom Instructions: "I want a very detailed plan. For project days, break down the project into smaller sub-tasks in the description. Be very thorough." """ try: model = genai.GenerativeModel( model_name="gemini-1.5-flash", system_instruction=SCHEDULE_GENERATOR_PROMPT ) full_genai_config = types.GenerationConfig( response_mime_type="application/json", response_schema=schedule_output_schema, ) contents = [types.Content(parts=[types.Part(text=prompt_context)])] response = await model.generate_content_async( contents=contents, generation_config=full_genai_config, ) raw_response_text = response.text print("\n--- RAW MODEL RESPONSE (first 500 chars) ---") print(raw_response_text[:500] + "...") print("--- (end of preview) ---\n") print(f"Total raw response length: {len(raw_response_text)} characters.") print("\nAttempting to parse the raw response as JSON...") schedule_data = json.loads(raw_response_text) print("\n✅ SUCCESS: JSON parsed successfully!") print(f"Generated schedule for {len(schedule_data.get('schedule', []))} days.") except json.JSONDecodeError as e: print(f"\n❌ FAILURE: Failed to decode JSON. Error: {e}") print("\nThis indicates the model's output was not a complete, valid JSON string.") print("\n--- PROBLEMATIC RAW TEXT (last 500 chars) ---") print("..." + raw_response_text[-500:]) print("--- (end of text) ---") except Exception as e: print(f"\n❌ An unexpected error occurred: {e}") if 'response' in locals() and hasattr(response, 'prompt_feedback'): print(f"Prompt Feedback: {response.prompt_feedback}") if __name__ == "__main__": asyncio.run(generate_long_schedule()) 

Note: admittedly I fed my actual Fast route to Gemini and asked it to make it MRC, hope that’s not an issue!

The context I feed is not the issue. The issue arrives when I ask it to generate structed output across a very long time range (The use case is an academic scheduling assistant based on syllabus, playlist, etc.)

Say I ask it to generate the whole year’s worth of activities, which I admit is a good amount of output tokens, always for long requests with a lot of content basically, since it is generating activities and content for each day over a much longer period of time, it returns the unterminated string error.

Bear with me though because I have not actually checked what the actual output tokens of responses with the error, so I apologize if this is not an issue by the Gemini API and just me not being careful.

Thanks,
Omkar M.

Any update on this issue, please?

Facing the same, and it’s October 12th, 2025.

Thanks.