Getting Started with Chat with Gemini
Source notebook
Repo path:
gemini/getting-started/intro_gemini_chat.ipynb· Open on GitHub · intro
Shows stateful Gemini chat with Google GenAI SDK and LangChain on Vertex AI.
Summary
This notebook teaches how to send chat prompts to a Gemini model on Vertex AI. It demonstrates GenAI SDK chat sessions, system instructions, response metadata, chat history, code generation, seeded history, and LangChain chat patterns with safety settings and memory.
Key code patterns
Initialize GenAI client
from google import genai
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "global")
client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)Creates a Vertex AI-backed GenAI SDK client using project and location settings.
Stateful chat with system instruction
chat = client.chats.create(
model=MODEL_ID,
config=GenerateContentConfig(
system_instruction="You are an astronomer, knowledgeable about the solar system.."
),
)
response = chat.send_message("How many moons does Mars have?")Starts a chat session that preserves context across messages.
Inspect response and history
print(response)
print(chat.get_history())Shows how to view response metadata such as safety ratings, usage metadata, and chat turns.
Seed chat history
chat2 = client.chats.create(
model=MODEL_ID,
history=[
UserContent("My name is Ned..."),
ModelContent("I work for Ned."),
],
)Preloads alternating user and model messages so later turns can reference prior context.
LangChain ChatVertexAI
chat = ChatVertexAI(
project=PROJECT_ID,
model_name=MODEL_ID,
convert_system_message_to_human=True,
safety_settings={HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE},
)
result = chat.generate([messages])Uses Gemini through LangChain with explicit safety settings and system-message conversion.
ConversationChain memory
memory = ConversationBufferMemory(memory_key="history", return_messages=True)
conversation = ConversationChain(llm=model, prompt=prompt, verbose=True, memory=memory)
conversation.invoke(input="Translate this sentence from English to French. I love programming.")Wraps ChatVertexAI in a LangChain conversation chain that remembers previous turns.
Models & APIs used
- Models: gemini-3.5-flash
- APIs / services: Vertex AI
- SDKs / libraries:
google-genai,langchain-google-vertexai,langchain
When to use this
Use this pattern when building text or code chat workflows with Gemini on Vertex AI, either directly or through LangChain.
Gotchas & caveats
- Requires an existing Google Cloud project with the Vertex AI API enabled.
- The tutorial uses billable Vertex AI components.
- Colab requires explicit Google Cloud authentication; Vertex AI Workbench does not.
- PROJECT_ID falls back to GOOGLE_CLOUD_PROJECT when the placeholder is not replaced.
- LOCATION defaults to global from GOOGLE_CLOUD_REGION.
- Gemini 3 does not support SystemMessage in LangChain at the moment, so convert_system_message_to_human=True is used.
Best practices
- Use environment variables for project and region fallback.
- Set system instructions when creating GenAI SDK chat sessions.
- Inspect response metadata including safety_ratings and usage_metadata.
- Retrieve chat history when validating stateful behavior.
- Add prior chat history as alternating user and model messages.
- Configure LangChain safety settings explicitly.
Related
- Concepts: Getting Started · Gemini Capabilities
- Entities: Vertex AI · Google GenAI SDK · LangChain · Gemini
- Area: Gemini Notebooks
- Best practices: Getting Started - Best Practices · Gemini Capabilities - Best Practices