mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 17:57:55 +00:00
Handle None on the response candidates in Google Generative AI (#142497)
* Added type checking on the candidates list * Made error message a constant
This commit is contained in:
parent
f702f3efcd
commit
c540acf2bd
@ -55,6 +55,10 @@ from .const import (
|
|||||||
# Max number of back and forth with the LLM to generate a response
|
# Max number of back and forth with the LLM to generate a response
|
||||||
MAX_TOOL_ITERATIONS = 10
|
MAX_TOOL_ITERATIONS = 10
|
||||||
|
|
||||||
|
ERROR_GETTING_RESPONSE = (
|
||||||
|
"Sorry, I had a problem getting a response from Google Generative AI."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -429,6 +433,12 @@ class GoogleGenerativeAIConversationEntity(
|
|||||||
raise HomeAssistantError(
|
raise HomeAssistantError(
|
||||||
f"The message got blocked due to content violations, reason: {chat_response.prompt_feedback.block_reason_message}"
|
f"The message got blocked due to content violations, reason: {chat_response.prompt_feedback.block_reason_message}"
|
||||||
)
|
)
|
||||||
|
if not chat_response.candidates:
|
||||||
|
LOGGER.error(
|
||||||
|
"No candidates found in the response: %s",
|
||||||
|
chat_response,
|
||||||
|
)
|
||||||
|
raise HomeAssistantError(ERROR_GETTING_RESPONSE)
|
||||||
|
|
||||||
except (
|
except (
|
||||||
APIError,
|
APIError,
|
||||||
@ -452,9 +462,7 @@ class GoogleGenerativeAIConversationEntity(
|
|||||||
|
|
||||||
response_parts = chat_response.candidates[0].content.parts
|
response_parts = chat_response.candidates[0].content.parts
|
||||||
if not response_parts:
|
if not response_parts:
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(ERROR_GETTING_RESPONSE)
|
||||||
"Sorry, I had a problem getting a response from Google Generative AI."
|
|
||||||
)
|
|
||||||
content = " ".join(
|
content = " ".join(
|
||||||
[part.text.strip() for part in response_parts if part.text]
|
[part.text.strip() for part in response_parts if part.text]
|
||||||
)
|
)
|
||||||
|
@ -12,6 +12,7 @@ import voluptuous as vol
|
|||||||
from homeassistant.components import conversation
|
from homeassistant.components import conversation
|
||||||
from homeassistant.components.conversation import UserContent, async_get_chat_log, trace
|
from homeassistant.components.conversation import UserContent, async_get_chat_log, trace
|
||||||
from homeassistant.components.google_generative_ai_conversation.conversation import (
|
from homeassistant.components.google_generative_ai_conversation.conversation import (
|
||||||
|
ERROR_GETTING_RESPONSE,
|
||||||
_escape_decode,
|
_escape_decode,
|
||||||
_format_schema,
|
_format_schema,
|
||||||
)
|
)
|
||||||
@ -492,7 +493,33 @@ async def test_empty_response(
|
|||||||
assert result.response.response_type == intent.IntentResponseType.ERROR, result
|
assert result.response.response_type == intent.IntentResponseType.ERROR, result
|
||||||
assert result.response.error_code == "unknown", result
|
assert result.response.error_code == "unknown", result
|
||||||
assert result.response.as_dict()["speech"]["plain"]["speech"] == (
|
assert result.response.as_dict()["speech"]["plain"]["speech"] == (
|
||||||
"Sorry, I had a problem getting a response from Google Generative AI."
|
ERROR_GETTING_RESPONSE
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_init_component")
|
||||||
|
async def test_none_response(
|
||||||
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||||
|
) -> None:
|
||||||
|
"""Test empty response."""
|
||||||
|
with patch("google.genai.chats.AsyncChats.create") as mock_create:
|
||||||
|
mock_chat = AsyncMock()
|
||||||
|
mock_create.return_value.send_message = mock_chat
|
||||||
|
chat_response = Mock(prompt_feedback=None)
|
||||||
|
mock_chat.return_value = chat_response
|
||||||
|
chat_response.candidates = None
|
||||||
|
result = await conversation.async_converse(
|
||||||
|
hass,
|
||||||
|
"hello",
|
||||||
|
None,
|
||||||
|
Context(),
|
||||||
|
agent_id="conversation.google_generative_ai_conversation",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result.response.response_type == intent.IntentResponseType.ERROR, result
|
||||||
|
assert result.response.error_code == "unknown", result
|
||||||
|
assert result.response.as_dict()["speech"]["plain"]["speech"] == (
|
||||||
|
ERROR_GETTING_RESPONSE
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user