System instructions set the context and behavior for the model throughout the conversation:
from google import genaifrom google.genai import typesclient = genai.Client(api_key='your-api-key')response = client.models.generate_content( model='gemini-2.5-flash', contents='high', config=types.GenerateContentConfig( system_instruction='I say high, you say low', ),)print(response.text) # Output: low
System instructions are like a primer that influences all subsequent responses. They persist throughout the conversation.
from google.genai import typessystem_instruction = """You are a helpful assistant specialized in Python programming.Follow these guidelines:- Provide clear, concise code examples- Explain complex concepts in simple terms- Always include error handling in code snippets- Suggest best practices and optimization tips"""response = client.models.generate_content( model='gemini-2.5-flash', contents='How do I read a file in Python?', config=types.GenerateContentConfig( system_instruction=system_instruction, ),)print(response.text)
from google.genai import typesresponse = client.models.generate_content( model='gemini-2.5-flash', contents='Write a creative story about a robot', config=types.GenerateContentConfig( temperature=0.9, ),)print(response.text)
from google.genai import types# Short summary (100-200 tokens)response = client.models.generate_content( model='gemini-2.5-flash', contents='Summarize the history of the internet', config=types.GenerateContentConfig( max_output_tokens=200, ),)# Medium content (500-1000 tokens)response = client.models.generate_content( model='gemini-2.5-flash', contents='Write a blog post about AI', config=types.GenerateContentConfig( max_output_tokens=1000, ),)# Long-form content (2000+ tokens)response = client.models.generate_content( model='gemini-2.5-flash', contents='Write a detailed technical guide', config=types.GenerateContentConfig( max_output_tokens=2048, ),)
Different models have different maximum token limits. Check the model documentation for specific limits.
from google.genai import typesresponse = client.models.generate_content( model='gemini-2.5-flash', contents='Write a Python function to calculate fibonacci numbers', config=types.GenerateContentConfig( system_instruction="""You are an expert Python developer. Write clean, efficient, well-documented code. Include docstrings and type hints.""", temperature=0.2, # Low for consistent code max_output_tokens=500, top_p=0.95, top_k=40, ),)print(response.text)
from google.genai import typesresponse = client.models.generate_content( model='gemini-2.5-flash', contents='List three colors: ', config=types.GenerateContentConfig( stop_sequences=['4.', '\n\n'], # Stop at "4." or double newline max_output_tokens=100, ),)print(response.text) # Will stop after listing 3 colors
System instructions and config apply to entire chat sessions:
from google.genai import typeschat = client.chats.create( model='gemini-2.5-flash', config=types.GenerateContentConfig( system_instruction="""You are a helpful tutor teaching Python. Always provide examples with explanations.""", temperature=0.4, ),)response1 = chat.send_message('What are lists?')print(response1.text)response2 = chat.send_message('Show me an example')print(response2.text)