Skip to content

Commit 5681f4f

Browse files
IvanLHfrenck
authored andcommittedMar 21, 2025
Ensure file is correctly uploaded by the GenAI SDK (#140969)
Opened the file outside of the SDK
1 parent 8a63fa3 commit 5681f4f

File tree

2 files changed

+10
-2
lines changed
  • homeassistant/components/google_generative_ai_conversation
  • tests/components/google_generative_ai_conversation

2 files changed

+10
-2
lines changed
 

‎homeassistant/components/google_generative_ai_conversation/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import mimetypes
56
from pathlib import Path
67

78
from google import genai # type: ignore[attr-defined]
@@ -83,7 +84,12 @@ def append_files_to_prompt():
8384
)
8485
if not Path(filename).exists():
8586
raise HomeAssistantError(f"`{filename}` does not exist")
86-
prompt_parts.append(client.files.upload(file=filename))
87+
mimetype = mimetypes.guess_type(filename)[0]
88+
with open(filename, "rb") as file:
89+
uploaded_file = client.files.upload(
90+
file=file, config={"mime_type": mimetype}
91+
)
92+
prompt_parts.append(uploaded_file)
8793

8894
await hass.async_add_executor_job(append_files_to_prompt)
8995

‎tests/components/google_generative_ai_conversation/test_init.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for the Google Generative AI Conversation integration."""
22

3-
from unittest.mock import AsyncMock, Mock, patch
3+
from unittest.mock import AsyncMock, Mock, mock_open, patch
44

55
import pytest
66
from requests.exceptions import Timeout
@@ -71,6 +71,8 @@ async def test_generate_content_service_with_image(
7171
),
7272
patch("pathlib.Path.exists", return_value=True),
7373
patch.object(hass.config, "is_allowed_path", return_value=True),
74+
patch("builtins.open", mock_open(read_data="this is an image")),
75+
patch("mimetypes.guess_type", return_value=["image/jpeg"]),
7476
):
7577
response = await hass.services.async_call(
7678
"google_generative_ai_conversation",

0 commit comments

Comments
 (0)