Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions python/packages/azure-ai/agent_framework_azure_ai/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
HostedCodeInterpreterTool,
HostedFileContent,
HostedFileSearchTool,
HostedImageGenerationTool,
HostedMCPTool,
HostedVectorStoreContent,
HostedWebSearchTool,
Expand All @@ -31,6 +32,7 @@
CodeInterpreterTool,
CodeInterpreterToolAuto,
FunctionTool,
ImageGenTool,
MCPTool,
ResponseTextFormatConfigurationJsonObject,
ResponseTextFormatConfigurationJsonSchema,
Expand Down Expand Up @@ -442,6 +444,20 @@ def to_azure_ai_tools(
timezone=location.get("timezone"),
)
azure_tools.append(ws_tool)
case HostedImageGenerationTool():
opts = tool.options or {}
# Azure ImageGenTool requires the constant model "gpt-image-1"
# Cast optional fields to expected Literal unions for mypy compliance
azure_tools.append(
ImageGenTool(
model="gpt-image-1",
size=cast(
Literal["1024x1024", "1024x1536", "1536x1024", "auto"] | None, opts.get("image_size")
),
output_format=cast(Literal["png", "webp", "jpeg"] | None, opts.get("media_type")),
partial_images=opts.get("streaming_count"),
)
)
case _:
logger.debug("Unsupported tool passed (type: %s)", type(tool))
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import tempfile
from pathlib import Path

import aiofiles
from agent_framework import DataContent, HostedImageGenerationTool
from agent_framework import HostedImageGenerationTool, ImageGenerationToolResultContent
from agent_framework.azure import AzureAIProjectAgentProvider
from azure.identity.aio import AzureCliCredential

Expand Down Expand Up @@ -32,8 +33,7 @@ async def main() -> None:
tools=[
HostedImageGenerationTool(
options={
"model": "gpt-image-1-mini",
"quality": "low",
"model": "gpt-image-1",
"size": "1024x1024",
}
)
Expand All @@ -54,15 +54,14 @@ async def main() -> None:
# Save the image to a file
print("Downloading generated image...")
image_data = [
content
content.outputs
for content in result.messages[0].contents
if isinstance(content, DataContent) and content.media_type == "image/png"
if isinstance(content, ImageGenerationToolResultContent) and content.outputs is not None
]
if image_data and image_data[0]:
# Save to the same directory as this script
# Save to the OS temporary directory
filename = "microsoft.png"
current_dir = Path(__file__).parent.resolve()
file_path = current_dir / filename
file_path = Path(tempfile.gettempdir()) / filename
async with aiofiles.open(file_path, "wb") as f:
await f.write(image_data[0].get_data_bytes())
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that image_data[0] (which is content.outputs) will always be a DataContent object with a get_data_bytes() method. However, according to the ImageGenerationToolResultContent type definition, outputs can be either DataContent | UriContent | None. The UriContent class does not have a get_data_bytes() method, which would cause an AttributeError at runtime if a UriContent is returned.

Consider adding a type check or handling for both DataContent (with get_data_bytes()) and UriContent (which would need a different approach, such as downloading from the URI).

Copilot uses AI. Check for mistakes.

Expand Down
Loading