AI / Google Antigravity Gemini Fundamentals Interview Questions
How do you implement multi-turn conversations in the Gemini API?
Multi-turn conversations maintain context across several exchanges. The Gemini API supports two approaches: manually managing conversation history with generateContent, or using the Interactions API's previous_interaction_id for server-side state management.
from google import genai from google.genai import types client = genai.Client() # Approach 1: Manual history with generateContent (legacy) history = [] while True: user_input = input("You: ") history.append(types.Content(role="user", parts=[types.Part(text=user_input)])) response = client.models.generate_content( model="gemini-3.5-flash", contents=history, ) assistant_reply = response.candidates[0].content history.append(assistant_reply) # add model turn to history print(f"Gemini: {assistant_reply.parts[0].text}") # Approach 2: Interactions API with previous_interaction_id (recommended) # First turn: first = client.interactions.create( model="gemini-3.5-flash", input="I am building a task management app in Python.", ) # Second turn - model remembers the first: second = client.interactions.create( model="gemini-3.5-flash", input="What database would you recommend for my app?", previous_interaction_id=first.id, # server retrieves first turn context ) # Third turn: third = client.interactions.create( model="gemini-3.5-flash", input="Show me the schema for that database.", previous_interaction_id=second.id, ) print(third.output_text)
More Related questions...