Files
semantic-kernel/python
ふぁー 93a14d5115 Python: feature: support gpt-image-1 (#12621)
This pull request was created in response to the issue
https://github.com/microsoft/semantic-kernel/issues/12500#issuecomment-2998387488.
The current AzureTextToImage implementation only works with DALL-E 3.
For gpt-image-1, the response format has changed from a URL to base64
only, which the current code does not support.

Additionally, gpt-image-1 introduces a new image editing feature that
also needs to be supported. Since breaking changes are required, new
methods have been added.

Minimal code that reproduces the problem
```python
service = AzureTextToImage(
        service_id=service_id,
        deployment_name="image-1",
        endpoint=AZURE_OPENAI_IMAGE_ENDPOINT,
        api_key=AZURE_OPENAI_IMAGE_API_KEY,
    )
)

settings = service.get_prompt_execution_settings_class()(service_id="image1")
settings.prompt = "sky"
settings.size = ImageSize(width=1024, height=1024)
settings.quality = "low"
r = await service.generate_image(settings=settings)
```

Example of use with newly added methods
```python
from semantic_kernel.connectors.ai.open_ai import AzureTextToImage
service = AzureTextToImage(
    service_id="image1",
    deployment_name="gpt-image-1",
    endpoint=AZURE_OPENAI_IMAGE_ENDPOINT,
    api_key=AZURE_OPENAI_IMAGE_API_KEY,
    api_version="2025-04-01-preview",
)
settings = service.get_prompt_execution_settings_class()(service_id="image1")
settings.n = 3
images_b64 = await service.generate_images("A cute cat wearing a whimsical striped hat", settings=settings)
```

```python
  from semantic_kernel.connectors.ai.open_ai import AzureTextToImage
  service = AzureTextToImage(
      service_id="image1",
      deployment_name="gpt-image-1",
      endpoint=AZURE_OPENAI_IMAGE_ENDPOINT,
      api_key=AZURE_OPENAI_IMAGE_API_KEY,
      api_version="2025-04-01-preview",
  )
  file_paths = ["./new_images/img_1.png", "./new_images/img_2.png"]
  settings = service.get_prompt_execution_settings_class()(service_id="image1")
  settings.n = 2
  results = await service.edit_image(
      prompt="Make the cat wear a wizard hat",
      image_paths=file_paths,
      settings=settings,
  )
```

## Problems Identified
1. Assumption of URL-based responses. The current implementation assumes
a response format that includes an image url, which is not the case for
gpt-image-1. See:
https://github.com/microsoft/semantic-kernel/blob/8d1b3fd55155bb65e9671383366d2672c4582fb0/python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_to_image_base.py#L69
2025-07-04 14:11:56 +00:00
..
2023-04-13 16:12:36 -07:00
2024-08-27 16:45:09 +00:00

Get Started with Semantic Kernel Python

Highlights

  • Flexible Agent Framework: build, orchestrate, and deploy AI agents and multi-agent systems
  • Multi-Agent Systems: Model workflows and collaboration between AI specialists
  • Plugin Ecosystem: Extend with Python, OpenAPI, Model Context Protocol (MCP), and more
  • LLM Support: OpenAI, Azure OpenAI, Hugging Face, Mistral, Vertex AI, ONNX, Ollama, NVIDIA NIM, and others
  • Vector DB Support: Azure AI Search, Elasticsearch, Chroma, and more
  • Process Framework: Build structured business processes with workflow modeling
  • Multimodal: Text, vision, audio

Quick Install

pip install --upgrade semantic-kernel
# Optional: Add integrations
pip install --upgrade semantic-kernel[hugging_face]
pip install --upgrade semantic-kernel[all]

Supported Platforms:

  • Python: 3.10+
  • OS: Windows, macOS, Linux

1. Setup API Keys

Set as environment variables, or create a .env file at your project root:

OPENAI_API_KEY=sk-...
OPENAI_CHAT_MODEL_ID=...
...
AZURE_OPENAI_API_KEY=...
AZURE_OPENAI_ENDPOINT=...
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=...
...

You can also override environment variables by explicitly passing configuration parameters to the AI service constructor:

chat_service = AzureChatCompletion(
    api_key=...,
    endpoint=...,
    deployment_name=...,
    api_version=...,
)

See the following setup guide for more information.

2. Use the Kernel for Prompt Engineering

Create prompt functions and invoke them via the Kernel:

import asyncio
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions import KernelArguments

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

prompt = """
1) A robot may not injure a human being...
2) A robot must obey orders given it by human beings...
3) A robot must protect its own existence...

Give me the TLDR in exactly {{$num_words}} words."""


async def main():
    result = await kernel.invoke_prompt(prompt, arguments=KernelArguments(num_words=5))
    print(result)


asyncio.run(main())
# Output: Protect humans, obey, self-preserve, prioritized.

3. Directly Use AI Services (No Kernel Required)

You can use the AI service classes directly for advanced workflows:

import asyncio
import asyncio

from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAIChatPromptExecutionSettings
from semantic_kernel.contents import ChatHistory


async def main():
    service = OpenAIChatCompletion()
    settings = OpenAIChatPromptExecutionSettings()

    chat_history = ChatHistory(system_message="You are a helpful assistant.")
    chat_history.add_user_message("Write a haiku about Semantic Kernel.")
    response = await service.get_chat_message_content(chat_history=chat_history, settings=settings)
    print(response.content)

    """
    Output:

    Thoughts weave through context,  
    Semantic threads interlace—  
    Kernel sparks meaning.
    """


asyncio.run(main())

4. Build an Agent with Plugins and Tools

Add Python functions as plugins or Pydantic models as structured outputs;

Enhance your agent with custom tools (plugins) and structured output:

import asyncio
from typing import Annotated
from pydantic import BaseModel
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatPromptExecutionSettings
from semantic_kernel.functions import kernel_function, KernelArguments

class MenuPlugin:
    @kernel_function(description="Provides a list of specials from the menu.")
    def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]:
        return """
        Special Soup: Clam Chowder
        Special Salad: Cobb Salad
        Special Drink: Chai Tea
        """

    @kernel_function(description="Provides the price of the requested menu item.")
    def get_item_price(
        self, menu_item: Annotated[str, "The name of the menu item."]
    ) -> Annotated[str, "Returns the price of the menu item."]:
        return "$9.99"

class MenuItem(BaseModel):
    # Used for structured outputs
    price: float
    name: str

async def main():
    # Configure structured outputs format
    settings = OpenAIChatPromptExecutionSettings()
    settings.response_format = MenuItem

    # Create agent with plugin and settings
    agent = ChatCompletionAgent(
        service=AzureChatCompletion(),
        name="SK-Assistant",
        instructions="You are a helpful assistant.",
        plugins=[MenuPlugin()],
        arguments=KernelArguments(settings)
    )

    response = await agent.get_response("What is the price of the soup special?")
    print(response.content)

    # Output:
    # The price of the Clam Chowder, which is the soup special, is $9.99.

asyncio.run(main()) 

You can explore additional getting started agent samples here.

5. Multi-Agent Orchestration

Coordinate a group of agents to iteratively solve a problem or refine content together:

import asyncio
from semantic_kernel.agents import ChatCompletionAgent, GroupChatOrchestration, RoundRobinGroupChatManager
from semantic_kernel.agents.runtime import InProcessRuntime
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

def get_agents():
    return [
        ChatCompletionAgent(
            name="Writer",
            instructions="You are a creative content writer. Generate and refine slogans based on feedback.",
            service=AzureChatCompletion(),
        ),
        ChatCompletionAgent(
            name="Reviewer",
            instructions="You are a critical reviewer. Provide detailed feedback on proposed slogans.",
            service=AzureChatCompletion(),
        ),
    ]

async def main():
    agents = get_agents()
    group_chat = GroupChatOrchestration(
        members=agents,
        manager=RoundRobinGroupChatManager(max_rounds=5),
    )
    runtime = InProcessRuntime()
    runtime.start()
    result = await group_chat.invoke(
        task="Create a slogan for a new electric SUV that is affordable and fun to drive.",
        runtime=runtime,
    )
    value = await result.get()
    print(f"Final Slogan: {value}")

    # Example Output:
    # Final Slogan: "Feel the Charge: Adventure Meets Affordability in Your New Electric SUV!"

    await runtime.stop_when_idle()

if __name__ == "__main__":
    asyncio.run(main())

For orchestration-focused examples, see these orchestration samples.

More Examples & Notebooks

Semantic Kernel Documentation