API Response & Conversation History
1. API Response is not a string
When we call the model:
response = client_which_is_gemini.chat.completions.create(
model="gemini-2.5-flash-lite",
messages=messages
)
you can also use other models, gemini 3.5 has even higher limits (reason:With every new generation of AI, Google optimizes the underlying architecture and TPU (Tensor Processing Unit) hardware.
Gemini 3.1 Flash Lite is a highly compressed, ultra-efficient model specifically engineered for high-volume, low-latency tasks. 1)
Because it is so highly optimized, it requires significantly less computing power and cost for Google to run per request compared to the older Gemini 2.5 architecture. Google can easily afford to give you 500 free requests of a highly optimized model because it barely dents their server capacity.2. Encouraging Migration to the New PlatformGoogle wants developers building on their newest, most advanced platform. Gemini 3.1 comes with a completely redesigned native reasoning engine (letting you control "thinking levels" from minimal to high) and improved tool integrations.By offering generous free limits on Gemini 3.1, Google incentivizes developers to migrate their projects to the newer version.By contrast, restricting older "legacy" models like Gemini 2.5 to 20 daily requests gently pushes developers away from outdated tech.The model does not return just the generated text.
Instead, it returns a response object containing several pieces of information, such as:
- Metadata (e.g., response ID)
- Token usage
- Finish reason
- Choices
- Assistant message
A simplified response looks like:
response = {
"id": "...",
"choices": [
{
"message": {
"role": "assistant",
"content": "If a clockmaker creates..."
}
}
]
}To access only the generated text, use:
print(response.choices[0].message.content)Roles in Chat APIs
Every message sent to the model has a role that identifies who is speaking.
User
Represents a message sent by the human.
{"role": "user", "content": "How are you?"}Assistant
Represents a message generated by the AI.
{"role": "assistant", "content": "I'm doing well!"}System
Provides instructions that guide the model's behavior throughout the conversation.
{"role": "system", "content": "You are a helpful Python tutor."}Conversation History (messages)
The model maintains context using a list called messages.
Each new message is appended (added to the end) of this list.
messages = []
messages.append({"role": "user", "content": "How are you?"})
messages.append({"role": "assistant", "content": "I'm doing well!"})
messages.append({"role": "user", "content": "Tell me a joke."})The conversation history now looks like:
messages = [
{"role": "user", "content": "How are you?"},
{"role": "assistant", "content": "I'm doing well!"},
{"role": "user", "content": "Tell me a joke."}
]When this list is sent back to the model, it can understand the entire conversation and respond with the correct context.
Key Takeaways
responseis an object, not a string.The generated text is accessed using:
pythonresponse.choices[0].message.contentmessagesstores the entire conversation history.roleindicates who spoke the message (user,assistant, orsystem).Every new message is appended to the
messageslist, allowing the model to maintain context across the conversation.