Ensure file is correctly uploaded by the GenAI SDK (#140969)

Opened the file outside of the SDK
This commit is contained in:
Ivan Lopez Hernandez 2025-03-19 22:58:19 -07:00 committed by GitHub
parent a600bc5e57
commit d9cf2750d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
import mimetypes
from pathlib import Path
from google import genai # type: ignore[attr-defined]
@ -83,7 +84,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
)
if not Path(filename).exists():
raise HomeAssistantError(f"`{filename}` does not exist")
prompt_parts.append(client.files.upload(file=filename))
mimetype = mimetypes.guess_type(filename)[0]
with open(filename, "rb") as file:
uploaded_file = client.files.upload(
file=file, config={"mime_type": mimetype}
)
prompt_parts.append(uploaded_file)
await hass.async_add_executor_job(append_files_to_prompt)

View File

@ -1,6 +1,6 @@
"""Tests for the Google Generative AI Conversation integration."""
from unittest.mock import AsyncMock, Mock, patch
from unittest.mock import AsyncMock, Mock, mock_open, patch
import pytest
from requests.exceptions import Timeout
@ -71,6 +71,8 @@ async def test_generate_content_service_with_image(
),
patch("pathlib.Path.exists", return_value=True),
patch.object(hass.config, "is_allowed_path", return_value=True),
patch("builtins.open", mock_open(read_data="this is an image")),
patch("mimetypes.guess_type", return_value=["image/jpeg"]),
):
response = await hass.services.async_call(
"google_generative_ai_conversation",