Articles → LANGCHAIN → Interact With Google Gemini API In Python.

Interact With Google Gemini API In Python.






Create An API Key




https://aistudio.google.com/app/api-keys



Install Package




pip install google-generativeai



Code


import os
import pathlib
import textwrap

import google.generativeai as genai
from IPython.display import Markdown


# ---------------------------------------------------------
# Helper: Format model output as Markdown
# ---------------------------------------------------------
def to_markdown(text: str):
    text = text.replace('.', ' *')
    return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))

# ---------------------------------------------------------
# API Key Setup
# ---------------------------------------------------------
# Set your API key here
os.environ['GOOGLE_API_KEY'] = "your_api_key"

# Configure the Generative AI SDK
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])


# ---------------------------------------------------------
# Initialize the model
# ---------------------------------------------------------
def init_model():
    return genai.GenerativeModel('gemini-3.6-flash')

# ---------------------------------------------------------
# Run a prompt
# ---------------------------------------------------------
def run_prompt(model, prompt: str):
    response = model.generate_content(prompt)
    return response.text

# ---------------------------------------------------------
# Main execution
# ---------------------------------------------------------
if __name__ == "__main__":
    print("\n=== Initializing Model ===")
    model = init_model()

    print("\n=== Running Prompt ===")
    prompt = "What is the capital of India?"
    output = run_prompt(model, prompt)

    print("\n=== Model Response ===")
    print(output)



Output


Picture showing the output of generating the text content using gemini





Posted By  -  Karan Gupta
 
Posted On  -  Friday, August 7, 2026

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250