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.