Google Generative AI: Handle response with empty parts in generate_content (#117908)

Handle response with empty parts in generate_content
pull/117999/head
tronikos 2024-05-22 07:47:16 -07:00 committed by GitHub
parent 5b1677ccb7
commit e4130480c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View File

@ -73,6 +73,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
) as err:
raise HomeAssistantError(f"Error generating content: {err}") from err
if not response.parts:
raise HomeAssistantError("Error generating content")
return {"text": response.text}
hass.services.async_register(

View File

@ -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(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,