Repository navigation
feat(genai): Add new samples for the Interactions API - #14640
holtskinner wants to merge 4 commits into
Conversation
|
Here is the summary of changes. You are about to add 49 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request updates the google-genai dependency to version 2.29.0 and introduces a comprehensive suite of new example scripts and tests demonstrating the interactions API across multiple capabilities, including bounding boxes, code execution, controlled generation, image generation, text generation, thinking, and tools. Feedback on the new code highlights a potential hang in headless environments due to the use of im.show() in the bounding box example, as well as several potential FileNotFoundError crashes across multiple image generation examples where output files are saved to a directory (output_folder) without first ensuring its creation.
| # Position the text at the top-left corner of the box | ||
| draw.text((abs_x_min + 8, abs_y_min + 6), bbox.label, fill=color) | ||
|
|
||
| im.show() |
There was a problem hiding this comment.
Using im.show() can cause issues or hang in headless environments (such as CI/CD pipelines) where no display server or default image viewer is available. To make this sample more robust and consistent with other image-processing samples in this repository, consider saving the output image to a file instead of calling im.show().
| im.show() | |
| import os | |
| output_dir = "output_folder" | |
| os.makedirs(output_dir, exist_ok=True) | |
| im.save(os.path.join(output_dir, "socks-bounding-box.png")) |
| if interaction.output_image: | ||
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | ||
| image.save("output_folder/bw-example-image.png") |
There was a problem hiding this comment.
Saving the image directly to "output_folder/bw-example-image.png" without ensuring that output_folder exists will raise a FileNotFoundError if the directory does not already exist. Ensure the directory is created before saving the image.
| if interaction.output_image: | |
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | |
| image.save("output_folder/bw-example-image.png") | |
| if interaction.output_image: | |
| import os | |
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | |
| output_dir = "output_folder" | |
| os.makedirs(output_dir, exist_ok=True) | |
| image.save(os.path.join(output_dir, "bw-example-image.png")) |
| if interaction.output_image: | ||
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | ||
| image.save("output_folder/video-image.png") |
There was a problem hiding this comment.
Saving the image directly to "output_folder/video-image.png" without ensuring that output_folder exists will raise a FileNotFoundError if the directory does not already exist. Ensure the directory is created before saving the image.
| if interaction.output_image: | |
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | |
| image.save("output_folder/video-image.png") | |
| if interaction.output_image: | |
| import os | |
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | |
| output_dir = "output_folder" | |
| os.makedirs(output_dir, exist_ok=True) | |
| image.save(os.path.join(output_dir, "video-image.png")) |
| if interaction.output_image: | ||
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | ||
| image.save("output_folder/example-breakfast-meal.png") |
There was a problem hiding this comment.
Saving the image directly to "output_folder/example-breakfast-meal.png" without ensuring that output_folder exists will raise a FileNotFoundError if the directory does not already exist. Ensure the directory is created before saving the image.
| if interaction.output_image: | |
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | |
| image.save("output_folder/example-breakfast-meal.png") | |
| if interaction.output_image: | |
| import os | |
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | |
| output_dir = "output_folder" | |
| os.makedirs(output_dir, exist_ok=True) | |
| image.save(os.path.join(output_dir, "example-breakfast-meal.png")) |
| saved_files = [] | ||
| image_counter = 1 | ||
|
|
||
| for step in interaction.steps: | ||
| if step.type == "model_output": | ||
| for content in step.content: | ||
| if content.type == "text": | ||
| print(content.text) | ||
| elif content.type == "image": | ||
| image = Image.open(BytesIO(base64.b64decode(content.data))) | ||
| filename = f"output_folder/example-cats-0{image_counter}.png" | ||
| image.save(filename) | ||
| saved_files.append(filename) | ||
| image_counter += 1 | ||
|
|
||
| # Fallback to output_image if no step-level images collected | ||
| if not saved_files and interaction.output_image: | ||
| image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data))) | ||
| filename = "output_folder/example-cats-01.png" | ||
| image.save(filename) | ||
| saved_files.append(filename) |
There was a problem hiding this comment.
Saving images directly to "output_folder/" without ensuring that the directory exists will raise a FileNotFoundError if the directory does not already exist. Ensure the directory is created before saving the images.
import os
output_dir = "output_folder"
os.makedirs(output_dir, exist_ok=True)
saved_files = []
image_counter = 1
for step in interaction.steps:
if step.type == "model_output":
for content in step.content:
if content.type == "text":
print(content.text)
elif content.type == "image":
image = Image.open(BytesIO(base64.b64decode(content.data)))
filename = os.path.join(output_dir, f"example-cats-0{image_counter}.png")
image.save(filename)
saved_files.append(filename)
image_counter += 1
# Fallback to output_image if no step-level images collected
if not saved_files and interaction.output_image:
image = Image.open(BytesIO(base64.b64decode(interaction.output_image.data)))
filename = os.path.join(output_dir, "example-cats-01.png")
image.save(filename)
saved_files.append(filename)
Description
Fixes #
Checklist
Testing
Compliance & Style
Post-Approval Actions