iTranslated by AI
Trying the Nano Banana (gemini-2.5-flash-image-preview) API
Introduction
I tried the Nano Banana (gemini-2.5-flash-image-preview) API using LangChain and the Google Gen AI SDK, so here is a summary.
Please note that this time I will be using the Vertex AI Nano Banana (gemini-2.5-flash-image-preview) model. In other words, being able to use Google Cloud's Vertex AI is a prerequisite.
If you want to use the Gemini API model, I think you can use it in almost the same way by referring to the article below and changing the authentication part and model definition part.
If you just want to try it out casually, I recommend trying it from Google AI Studio.
However, by trying it from the API, you can also retrieve multiple images simultaneously with the same image editing prompt, so I think there are ways to use it.
Additionally, the use with the Google Gen AI SDK is based on the Vertex AI Model Garden documentation below.
For how to use it with LangChain, as in the article I wrote before, I have described image generation based on the documentation, and image editing without references.
Therefore, if there are any errors, I would appreciate it if you could quietly let me know.
Repository
Please see the repository below.
Preparation
Clone the repository
Clone the repository with the following command.
git clone https://github.com/personabb/langchain_asap_sample.git
This article uses the following folder within the repository.
cd langchain_asap_sample/gemini2.5_image_generation
Environment variables
First, rename the .env.example file in the repository to .env.
Then, create a JSON for authentication with a Google Cloud service account, rename it to the path auth_json/auth.json specified in the .env file, and save it.
For how to obtain the authentication JSON key for a service account, please refer to the following article, etc.
Alternatively, you can obtain temporary authentication credentials using the following command.
In this case, the JSON key is not required.
gcloud auth application-default login
Set up a virtual environment
Set up the environment with the following command.
pip install -r requirements.txt
If you use uv, you can probably set up the virtual environment with the following.
uv sync
Execution
Image Generation
Google Gen AI SDK
Parameters
By modifying the following parts of the code, you can set the image to be created and how many times the API will be called.
MODEL_ID = "gemini-2.5-flash-image-preview"
# ========== Specify the number of images to generate at once ==========
generate_images = 1
# ========== Specify the generation content ==========
query = "Create a realistic image of my home PC desk. Place a laptop and a coffee cup on the desk. The desk should be black. Otherwise, create a general room scene that looks good."
# =================================
Execution Command
You can execute it with the following command.
python vertexai_image_generation_api.py
LangChain
Parameters
By modifying the following parts of the code, you can set the image to be created and how many times the API will be called.
model = ChatVertexAI(
model_name="gemini-2.5-flash-image-preview",
credentials=credentials,
project=project_id,
location="global",
temperature=0.7,
response_modalities=[Modality.IMAGE, Modality.TEXT]
)
......
# ========== Specify the number of images to generate at once ==========
generate_images = 1
# ========== Specify the generation content ==========
query = "Create a realistic image of my home PC desk. Place a laptop and a coffee cup on the desk. The desk should be black. Otherwise, create a general room scene that looks good."
# =================================
Execution Command
You can execute it with the following command.
python vertexai_image_generation_langchain.py
Image Editing
Google Gen AI SDK
Parameters
By modifying the following parts of the code, you can set the number of images to create, the image to reference, and how many times to call the API.
The reference image uses a locally stored image (also included in the GitHub repository).
This time, we encode the image to base64 and input it to the model.
MODEL_ID = "gemini-2.5-flash-image-preview"
# ========== Specify the number of images to generate at once ==========
generate_images = 1
# ========== Path to the image to load ==========
file_path = "inputs/images3.png"
# ========== Specify the editing content ==========
query = "Make it into a realistic 3D figure. However, change the pose to a cooler girl's pose."
# =================================
Execution Command
You can execute it with the following command.
python vertexai_image_editing_api.py
LangChain
Parameters
By modifying the following parts of the code, you can set the number of images to create, the image to reference, and how many times to call the API.
The reference image uses a locally stored image (also included in the GitHub repository).
This time, we encode the image to base64 and input it to the model.
model = ChatVertexAI(
model_name="gemini-2.5-flash-image-preview",
credentials=credentials,
project=project_id,
location="global",
temperature=0.7,
response_modalities=[Modality.IMAGE, Modality.TEXT]
)
......
# ========== Specify the number of images to generate at once ==========
generate_images = 1
# ========== Path to the image to load ==========
file_path = "inputs/images3.png"
# ========== Specify the editing content ==========
query = "Make it into a 3D figure. However, change the pose to a cooler girl's pose."
# =================================
Execution Command
You can execute it with the following command.
python vertexai_image_editing_langchain.py
Results
Although I haven't tested it extensively yet, I'll present some results of the image editing. As expected, the accuracy is high without needing to mention it here.
Input Image
My own character.

Edited Image 1
Prompt: "Create a manga based on the girl in the image. Make it a 4-panel manga where she is happily playing with friends."

Edited Image 2
Prompt: "Make it into a 3D figure. However, change the pose to a cooler girl's pose."

Issues
I'm not sure if this is a code issue or an API issue, but generated images are not returned about 30% to 80% of the time. (The probability varies depending on the prompt.)
With Nano Banana, when requesting image editing, text is output first, and then the image is generated. (This has been the same since gemini-2.0-flash-preview-image-generation.) Here, only text is output and the image is not output, which happens frequently.
It might be possible to increase the probability by using system prompts, but I'd appreciate it if you could let me know any best practices...
By setting the prompt to English, I was able to consistently obtain images 100% of the time.
Images are also returned with Japanese prompts, but if you are incorporating this into a system, I recommend using English!
(By the way, my impression is that the quality of the generated images themselves does not change significantly whether the prompt is in Japanese or English.)
Of course, this is only based on my limited testing, so it may not work with other prompts. If you find any prompts that don't work well, please let me know!
Summary
Thank you for reading!
(This article was written by hand.)
Discussion