Google Generative AI: Handle response with empty parts in generate_content (#117908)
Handle response with empty parts in generate_contentpull/117999/head
parent
5b1677ccb7
commit
e4130480c3
|
@ -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(
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue