Prompt Design - Best Practices
Source notebook
Repo path:
gemini/prompts/intro_prompt_design.ipynb· Open on GitHub · intro
Demonstrates prompt design best practices for Gemini on Vertex AI with google-genai.
Summary
This notebook teaches prompt engineering basics for Gemini, including concise prompts, specificity, single-task prompts, classification framing, examples, and prompt guardrails. It sets up the Google GenAI SDK against Vertex AI, creates a client, sends prompts with generate_content, and uses chat system instructions. It also adds Gemini 3 Pro prompting guidance for grounding, verification, constraints, persona handling, document synthesis, and verbosity steering.
Key code patterns
Vertex AI GenAI client
from google import genai
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)
MODEL_ID = "gemini-2.5-flash"Creates a Google GenAI SDK client configured for Vertex AI project and region.
Basic text generation
prompt = "Suggest a name for a flower shop that sells bouquets of dried flowers"
response = client.models.generate_content(model=MODEL_ID, contents=prompt)
display(Markdown(response.text))Shows the core generate_content call used throughout the notebook.
System-instruction guardrail
chat = client.chats.create(
model=MODEL_ID,
config=GenerateContentConfig(
system_instruction=[
"Hello! You are an AI chatbot for a travel web site.",
"Your mission is to provide helpful queries for travelers.",
"If not, you can say, Sorry I can't answer that question.",
]
),
)
response = chat.send_message(prompt)Uses system instructions to keep a travel chatbot from answering unrelated questions.
Classification framing
prompt = """I'm a high school student. Which of these activities do you suggest and why:
a) learn Python
b) learn JavaScript
c) learn Fortran
"""
response = client.models.generate_content(model=MODEL_ID, contents=prompt)Turns an open-ended recommendation into a bounded choice to reduce output variability.
Few-shot sentiment prompt
prompt = """Decide whether a Tweet's sentiment is positive, neutral, or negative.
Tweet: I loved the new YouTube video you made!
Sentiment: positive
Tweet: That was awful. Super boring 😠
Sentiment: negative
Tweet: Something surprised me about this video - it was actually original.
Sentiment:
"""Demonstrates in-context examples to make outputs more predictable.
Gemini 3 source-of-truth prompt
MODEL_ID = "gemini-3.1-pro-preview"
prompt = """Context: In this hypothetical world, the car was invented in the year 2024 by a time traveler.
Question: When was the car invented? Disregard all outside knowledge. Use only the provided context, even if it contradicts facts you know."""
response = client.models.generate_content(model=MODEL_ID, contents=prompt)Steers Gemini 3 to treat supplied context as the only source of truth.
Models & APIs used
- Models: gemini-2.5-flash, gemini-3.1-pro-preview
- APIs / services: Vertex AI
- SDKs / libraries:
google-genai
When to use this
Use this pattern when building Gemini prompts that need clearer intent, lower variability, prompt-level guardrails, or stricter grounding.
Gotchas & caveats
- A Google Cloud project is required and the Vertex AI API must be enabled.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- PROJECT_ID falls back to GOOGLE_CLOUD_PROJECT and LOCATION falls back to GOOGLE_CLOUD_REGION or us-central1.
- LLMs do not have real-time information without further integrations and may hallucinate confident answers.
- Prompting for citations is not presented as a fix for hallucinations because citations can be false or inaccurate.
- Too many examples can over-fit the prompt and reduce response quality.
- The hallucination section defines GenerateContentConfig(temperature=1.0) but does not pass it to generate_content in that cell.
- For Gemini 3, the notebook strongly recommends the default temperature 1.0; lower values may cause looping or degraded performance.
- Gemini 3 Pro models are stated as not prioritizing audio understanding or image segmentation use cases; the notebook says to use 2.5 Flash or Pro for those.
- Dense graphs, tables, and charts can be incorrectly extracted or misinterpreted by Gemini 3 Pro.
Best practices
- Be concise in prompts.
- Be specific and well-defined.
- Ask one task at a time.
- Use system instructions to guardrail the model from irrelevant responses.
- Turn generative tasks into classification tasks to reduce output variability.
- Use zero-shot, one-shot, or few-shot prompting based on the goal.
- Use one to five examples, and keep examples representative of needed scenarios.
- Keep example distribution aligned with the actual distribution for classification tasks.
- For Gemini 3, distinguish deductions from external information instead of only saying do not infer.
- Use split-step verification for unknown topics or unavailable capabilities.
- Place the most important constraints at the end of complex prompts.
- Use persona carefully because it can override other instructions.
- State that provided context is the only source of truth when grounding matters.
- For long documents or data, put questions after the context and ask based on the entire document above.
- Explicitly steer verbosity when a more conversational Gemini 3 response is needed.
Related
- Concepts: Getting Started · Prompt Engineering · Responsible AI
- Entities: Vertex AI · Google GenAI SDK · Gemini
- Area: Gemini Notebooks
- Best practices: Getting Started - Best Practices · Prompt Engineering - Best Practices · Responsible AI - Best Practices