Skip to content

Commit aeaa1d4

Browse files
authored
Merge branch 'dev' into init-fix
2 parents 6ddb207 + 56cc404 commit aeaa1d4

File tree

3 files changed

+77
-75
lines changed

3 files changed

+77
-75
lines changed

homeassistant/components/google_generative_ai_conversation/conversation.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,25 @@ def _escape_decode(value: Any) -> Any:
171171
return value
172172

173173

174+
def _create_google_tool_response_parts(
175+
parts: list[conversation.ToolResultContent],
176+
) -> list[Part]:
177+
"""Create Google tool response parts."""
178+
return [
179+
Part.from_function_response(
180+
name=tool_result.tool_name, response=tool_result.tool_result
181+
)
182+
for tool_result in parts
183+
]
184+
185+
174186
def _create_google_tool_response_content(
175187
content: list[conversation.ToolResultContent],
176188
) -> Content:
177189
"""Create a Google tool response content."""
178190
return Content(
179-
parts=[
180-
Part.from_function_response(
181-
name=tool_result.tool_name, response=tool_result.tool_result
182-
)
183-
for tool_result in content
184-
]
191+
role="user",
192+
parts=_create_google_tool_response_parts(content),
185193
)
186194

187195

@@ -402,7 +410,7 @@ async def _async_handle_message(
402410
chat = self._genai_client.aio.chats.create(
403411
model=model_name, history=messages, config=generateContentConfig
404412
)
405-
chat_request: str | Content = user_input.text
413+
chat_request: str | list[Part] = user_input.text
406414
# To prevent infinite loops, we limit the number of iterations
407415
for _iteration in range(MAX_TOOL_ITERATIONS):
408416
try:
@@ -456,7 +464,7 @@ async def _async_handle_message(
456464
)
457465
)
458466

459-
chat_request = _create_google_tool_response_content(
467+
chat_request = _create_google_tool_response_parts(
460468
[
461469
tool_response
462470
async for tool_response in chat_log.async_add_assistant_content(

tests/components/google_generative_ai_conversation/snapshots/test_conversation.ambr

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
tuple(
2626
),
2727
dict({
28-
'message': Content(parts=[Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=FunctionResponse(id=None, name='test_tool', response={'result': 'Test response'}), inline_data=None, text=None)], role=None),
28+
'message': list([
29+
Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=FunctionResponse(id=None, name='test_tool', response={'result': 'Test response'}), inline_data=None, text=None),
30+
]),
2931
}),
3032
),
3133
])
@@ -56,7 +58,9 @@
5658
tuple(
5759
),
5860
dict({
59-
'message': Content(parts=[Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=FunctionResponse(id=None, name='test_tool', response={'result': 'Test response'}), inline_data=None, text=None)], role=None),
61+
'message': list([
62+
Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=FunctionResponse(id=None, name='test_tool', response={'result': 'Test response'}), inline_data=None, text=None),
63+
]),
6064
}),
6165
),
6266
])
@@ -87,7 +91,9 @@
8791
tuple(
8892
),
8993
dict({
90-
'message': Content(parts=[Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=FunctionResponse(id=None, name='test_tool', response={'result': 'Test response'}), inline_data=None, text=None)], role=None),
94+
'message': list([
95+
Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=FunctionResponse(id=None, name='test_tool', response={'result': 'Test response'}), inline_data=None, text=None),
96+
]),
9197
}),
9298
),
9399
])

tests/components/google_generative_ai_conversation/test_conversation.py

+52-64
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,24 @@ def tool_call(
104104

105105
assert result.response.response_type == intent.IntentResponseType.ACTION_DONE
106106
assert result.response.as_dict()["speech"]["plain"]["speech"] == "Hi there!"
107-
mock_tool_call = mock_create.mock_calls[2][2]["message"]
108-
assert mock_tool_call.model_dump() == {
109-
"parts": [
110-
{
111-
"code_execution_result": None,
112-
"executable_code": None,
113-
"file_data": None,
114-
"function_call": None,
115-
"function_response": {
116-
"id": None,
117-
"name": "test_tool",
118-
"response": {
119-
"result": "Test response",
120-
},
121-
},
122-
"inline_data": None,
123-
"text": None,
124-
"thought": None,
125-
"video_metadata": None,
107+
mock_tool_response_parts = mock_create.mock_calls[2][2]["message"]
108+
assert len(mock_tool_response_parts) == 1
109+
assert mock_tool_response_parts[0].model_dump() == {
110+
"code_execution_result": None,
111+
"executable_code": None,
112+
"file_data": None,
113+
"function_call": None,
114+
"function_response": {
115+
"id": None,
116+
"name": "test_tool",
117+
"response": {
118+
"result": "Test response",
126119
},
127-
],
128-
"role": None,
120+
},
121+
"inline_data": None,
122+
"text": None,
123+
"thought": None,
124+
"video_metadata": None,
129125
}
130126

131127
mock_tool.async_call.assert_awaited_once_with(
@@ -292,28 +288,24 @@ def tool_call(
292288

293289
assert result.response.response_type == intent.IntentResponseType.ACTION_DONE
294290
assert result.response.as_dict()["speech"]["plain"]["speech"] == "Hi there!"
295-
mock_tool_call = mock_create.mock_calls[2][2]["message"]
296-
assert mock_tool_call.model_dump() == {
297-
"parts": [
298-
{
299-
"code_execution_result": None,
300-
"executable_code": None,
301-
"file_data": None,
302-
"function_call": None,
303-
"function_response": {
304-
"id": None,
305-
"name": "test_tool",
306-
"response": {
307-
"result": "Test response",
308-
},
309-
},
310-
"inline_data": None,
311-
"text": None,
312-
"thought": None,
313-
"video_metadata": None,
291+
mock_tool_response_parts = mock_create.mock_calls[2][2]["message"]
292+
assert len(mock_tool_response_parts) == 1
293+
assert mock_tool_response_parts[0].model_dump() == {
294+
"code_execution_result": None,
295+
"executable_code": None,
296+
"file_data": None,
297+
"function_call": None,
298+
"function_response": {
299+
"id": None,
300+
"name": "test_tool",
301+
"response": {
302+
"result": "Test response",
314303
},
315-
],
316-
"role": None,
304+
},
305+
"inline_data": None,
306+
"text": None,
307+
"thought": None,
308+
"video_metadata": None,
317309
}
318310

319311
mock_tool.async_call.assert_awaited_once_with(
@@ -390,29 +382,25 @@ def tool_call(
390382

391383
assert result.response.response_type == intent.IntentResponseType.ACTION_DONE
392384
assert result.response.as_dict()["speech"]["plain"]["speech"] == "Hi there!"
393-
mock_tool_call = mock_create.mock_calls[2][2]["message"]
394-
assert mock_tool_call.model_dump() == {
395-
"parts": [
396-
{
397-
"code_execution_result": None,
398-
"executable_code": None,
399-
"file_data": None,
400-
"function_call": None,
401-
"function_response": {
402-
"id": None,
403-
"name": "test_tool",
404-
"response": {
405-
"error": "HomeAssistantError",
406-
"error_text": "Test tool exception",
407-
},
408-
},
409-
"inline_data": None,
410-
"text": None,
411-
"thought": None,
412-
"video_metadata": None,
385+
mock_tool_response_parts = mock_create.mock_calls[2][2]["message"]
386+
assert len(mock_tool_response_parts) == 1
387+
assert mock_tool_response_parts[0].model_dump() == {
388+
"code_execution_result": None,
389+
"executable_code": None,
390+
"file_data": None,
391+
"function_call": None,
392+
"function_response": {
393+
"id": None,
394+
"name": "test_tool",
395+
"response": {
396+
"error": "HomeAssistantError",
397+
"error_text": "Test tool exception",
413398
},
414-
],
415-
"role": None,
399+
},
400+
"inline_data": None,
401+
"text": None,
402+
"thought": None,
403+
"video_metadata": None,
416404
}
417405
mock_tool.async_call.assert_awaited_once_with(
418406
hass,

0 commit comments

Comments
 (0)