Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Bug: Type Definition Error ImageContent #10395

Merged
merged 9 commits into from
Feb 5, 2025

Conversation

ymuichiro
Copy link
Contributor

@ymuichiro ymuichiro commented Feb 4, 2025

Motivation and Context

  1. This change is required because IDE type-checking throws an error when manually passing values (e.g., Base64 strings) to ImageContent’s constructor.
  2. It fixes the missing argument definitions (data, data_uri, data_format, mime_type), which caused the type error in IDEs like VS Code.
  3. It resolves the issue reported where users observed IDE-type errors despite code functioning at runtime.

Description

• Added explicit type definitions (data, data_uri, data_format, mime_type) in the ImageContent class constructor and fields.
• Updated the docstring and init to reflect the new arguments.
• Ensured the code maintains backward compatibility by preserving existing methods like from_image_file.
• Verified that type-checking in major Python IDEs now passes without warnings.

Example

import asyncio
import base64

from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion,
from semantic_kernel.contents import AuthorRole, ChatHistory, ImageContent
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.text_content import TextContent


async def main():
    service = AzureChatCompletion(
        api_key="************************",
        deployment_name="gpt-4o-mini",
        endpoint="https://************************.openai.azure.com",
        api_version="2024-12-01-preview",
    )

    chat_history = ChatHistory()

    # example of base64 image
    # or `ImageContent.from_image_file`
    with open("weather_icon.png", "rb") as image_file:
        b64_string = base64.b64encode(image_file.read()).decode("utf-8")

    chat_history.add_message(
        ChatMessageContent(
            role=AuthorRole.USER,
            items=[
                ImageContent(
                    data_uri=f"data:image/png;base64,{b64_string}",
                    data_format="base64",
                    mime_type="image/png",
                ),
                TextContent(text="what is the weather"),
            ],
        )
    )
    response = await service.get_chat_message_contents(
        chat_history=chat_history,
        settings=service.get_prompt_execution_settings_class()(),
    )

    print(response[0].content)
    # >> The icon you provided typically represents sunny or clear weather.
    # >> If you need specific weather information for a location,
    # >> please provide the location or check a reliable weather service for the latest updates.


asyncio.run(main())

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the SK Contribution Guidelines and the pre-submission formatting script raises no violations
  • All unit tests pass, and I have added new tests where possible
  • I didn’t break anyone ☺

@ymuichiro ymuichiro requested a review from a team as a code owner February 4, 2025 15:09
@markwallace-microsoft markwallace-microsoft added the python Pull requests for the Python Semantic Kernel label Feb 4, 2025
@github-actions github-actions bot changed the title Issue 10392 Python: Issue 10392 Feb 4, 2025
@ymuichiro ymuichiro changed the title Python: Issue 10392 Python: Bug: Type Definition Error ImageContent Feb 4, 2025
@ymuichiro
Copy link
Contributor Author

#10392

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Feb 5, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
TOTAL17607194589% 
report-only-changed-files is enabled. No files were changed during this commit :)

Python Unit Test Overview

Tests Skipped Failures Errors Time
3125 4 💤 0 ❌ 0 🔥 1m 23s ⏱️

@moonbox3 moonbox3 added this pull request to the merge queue Feb 5, 2025
Merged via the queue into microsoft:main with commit 4d2b2be Feb 5, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Pull requests for the Python Semantic Kernel
Projects
Status: Sprint: Done
Development

Successfully merging this pull request may close these issues.

Python: Bug: Type Definition Error ImageContent
4 participants