mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 07:17:12 +00:00
Google Generative AI: Handle response with empty parts in generate_content (#117908)
Handle response with empty parts in generate_content
This commit is contained in:
parent
5b1677ccb7
commit
e4130480c3
@ -73,6 +73,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
) as err:
|
) as err:
|
||||||
raise HomeAssistantError(f"Error generating content: {err}") from err
|
raise HomeAssistantError(f"Error generating content: {err}") from err
|
||||||
|
|
||||||
|
if not response.parts:
|
||||||
|
raise HomeAssistantError("Error generating content")
|
||||||
|
|
||||||
return {"text": response.text}
|
return {"text": response.text}
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
|
@ -110,6 +110,30 @@ async def test_generate_content_service_error(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_init_component")
|
||||||
|
async def test_generate_content_response_has_empty_parts(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test generate content service handles response with empty parts."""
|
||||||
|
with (
|
||||||
|
patch("google.generativeai.GenerativeModel") as mock_model,
|
||||||
|
pytest.raises(HomeAssistantError, match="Error generating content"),
|
||||||
|
):
|
||||||
|
mock_response = MagicMock()
|
||||||
|
mock_response.parts = []
|
||||||
|
mock_model.return_value.generate_content_async = AsyncMock(
|
||||||
|
return_value=mock_response
|
||||||
|
)
|
||||||
|
await hass.services.async_call(
|
||||||
|
"google_generative_ai_conversation",
|
||||||
|
"generate_content",
|
||||||
|
{"prompt": "write a story about an epic fail"},
|
||||||
|
blocking=True,
|
||||||
|
return_response=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_generate_content_service_with_image_not_allowed_path(
|
async def test_generate_content_service_with_image_not_allowed_path(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user