GoogleGenerativeAI integration
Warning
You are currently on a page documenting the use of Google models as text completion models. Many popular Google models are chat completion models.
You may be looking for this page instead.
A guide on using Google Generative AI models with LangChain. Note: It's separate from Gemini Enterprise Agent Platform integration.
Setting up
To use Google Generative AI you must install the langchain-google-genai Python package and generate an API key. Read more details.
pip install -qU langchain-google-genaifrom langchain_google_genai import GoogleGenerativeAIfrom getpass import getpass
api_key = getpass()llm = GoogleGenerativeAI(model="gemini-3.6-flash", google_api_key=api_key)
print(
llm.invoke(
"What are some of the pros and cons of Python as a programming language?"
)
)**Pros:**
* **Simplicity and Readability:** Python is known for its simple and easy-to-read syntax, which makes it accessible to beginners and reduces the chance of errors. It uses indentation to define blocks of code, making the code structure clear and visually appealing.
* **Versatility:** Python is a general-purpose language, meaning it can be used for a wide range of tasks, including web development, data science, machine learning, and desktop applications. This versatility makes it a popular choice for various projects and industries.
* **Large Community:** Python has a vast and active community of developers, which contributes to its growth and popularity. This community provides extensive documentation, tutorials, and open-source libraries, making it easy for Python developers to find support and resources.
* **Extensive Libraries:** Python offers a rich collection of libraries and frameworks for various tasks, such as data analysis (NumPy, Pandas), web development (Django, Flask), machine learning (Scikit-learn, TensorFlow), and many more. These libraries provide prebuilt functions and modules, allowing developers to quickly and efficiently solve common problems.
* **Cross-Platform Support:** Python is cross-platform, meaning it can run on various operating systems, including Windows, macOS, and Linux. This allows developers to write code that can be easily shared and used across different platforms.
**Cons:**
* **Speed and Performance:** Python is generally slower than compiled languages like C++ or Java due to its interpreted nature. This can be a disadvantage for performance-intensive tasks, such as real-time systems or heavy numerical computations.
* **Memory Usage:** Python programs tend to consume more memory compared to compiled languages. This is because Python uses a dynamic memory allocation system, which can lead to memory fragmentation and higher memory usage.
* **Lack of Static Typing:** Python is a dynamically typed language, which means that data types are not explicitly defined for variables. This can make it challenging to detect type errors during development, which can lead to unexpected behavior or errors at runtime.
* **GIL (Global Interpreter Lock):** Python uses a global interpreter lock (GIL) to ensure that only one thread can execute Python bytecode at a time. This can limit the scalability and parallelism of Python programs, especially in multi-threaded or multiprocessing scenarios.
* **Package Management:** While Python has a vast ecosystem of libraries and packages, managing dependencies and package versions can be challenging. The Python Package Index (PyPI) is the official repository for Python packages, but it can be difficult to ensure compatibility and avoid conflicts between different versions of packages.Using in a chain
from langchain_core.prompts import PromptTemplatetemplate = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
chain = prompt | llm
question = "How much is 2+2?"
print(chain.invoke({"question": question}))4Streaming calls
import sys
stream = llm.stream_events("Tell me a short poem about snow", version="v3")
for token in stream.text:
sys.stdout.write(token)
sys.stdout.flush()In winter's embrace, a silent ballet,
Snowflakes descend, a celestial display.
Whispering secrets, they softly fall,
A blanket of white, covering all.
With gentle grace, they paint the land,
Transforming the world into a winter wonderland.
Trees stand adorned in icy splendor,
A glistening spectacle, a sight to render.
Snowflakes twirl, like dancers on a stage,
Creating a symphony, a winter montage.
Their silent whispers, a sweet serenade,
As they dance and twirl, a snowy cascade.
In the hush of dawn, a frosty morn,
Snow sparkles bright, like diamonds reborn.
Each flake unique, in its own design,
A masterpiece crafted by the divine.
So let us revel in this wintry bliss,
As snowflakes fall, with a gentle kiss.
For in their embrace, we find a peace profound,
A frozen world, with magic all around.Safety settings
Gemini models have default safety settings that can be overridden. If you are receiving lots of "Safety Warnings" from your models, you can try tweaking the safety_settings attribute of the model. For example, to turn off safety blocking for dangerous content, you can construct your LLM as follows:
from langchain_google_genai import GoogleGenerativeAI, HarmBlockThreshold, HarmCategory
llm = GoogleGenerativeAI(
model="gemini-3.6-flash",
google_api_key=api_key,
safety_settings={
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
},
)For an enumeration of the categories and thresholds available, see Google's safety settings guide.