Creating a Chat Session
Create a chat session usingclient.chats.create() to start a conversation:
Synchronous Non-Streaming
Send messages and receive complete responses:send_message() retains the conversation context, so the model can reference previous messages.
Synchronous Streaming
Stream responses as they’re generated for real-time user experiences:Asynchronous Non-Streaming
Use async/await for non-blocking chat operations:Asynchronous Streaming
Combine async operations with streaming for the best performance:Context Retention
Chat sessions automatically maintain conversation history. The model can:- Reference previous messages
- Build on earlier responses
- Maintain consistency across turns
- Use pronouns and context from prior exchanges
Configuration Options
You can configure chat sessions with the same options asgenerate_content:
Accessing Chat History
You can access the full conversation history from a chat session:Best Practices
- Use streaming for better user experience in chat interfaces
- Use async for handling multiple concurrent chat sessions
- Set system instructions to define the assistant’s personality and behavior
- Monitor context length - long conversations may exceed token limits
- Create new sessions when starting a new topic or conversation