fix: make the home location lookup work against Claude
- Send the location question as a user message. A request with only a
system message has no user content, which endpoints in front of Anthropic
models reject ("text content blocks must be non-empty").
- Leave unknown fields out of the web search user_location. After a failed
lookup there is no city or region, and the empty strings made every
request fail ("user_location.city: String should have at least 1
character").
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
235c536cab
commit
84f4bcda55
4 files changed
+63
-7
No files matched your search
+46
-1
@@ -3,14 +3,21 @@
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from homeassistant.components import conversation
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.helpers import llm
|
||||
|
||||
from custom_components.openai_compatible.const import (
|
||||
CONF_WEB_SEARCH,
|
||||
CONF_WEB_SEARCH_COUNTRY,
|
||||
CONF_WEB_SEARCH_TIMEZONE,
|
||||
CONF_WEB_SEARCH_USER_LOCATION,
|
||||
)
|
||||
from custom_components.openai_compatible.entity import (
|
||||
_convert_content_to_param,
|
||||
_transform_stream,
|
||||
)
|
||||
|
||||
from . import create_function_tool_call_item
|
||||
from . import create_function_tool_call_item, create_message_item
|
||||
|
||||
|
||||
async def _stream(events):
|
||||
@@ -90,3 +97,41 @@ def test_web_search_not_replayed() -> None:
|
||||
]
|
||||
assert messages[1]["content"] == "It ended 2 to 2."
|
||||
assert messages[2]["call_id"] == "call_1"
|
||||
|
||||
|
||||
async def test_web_search_location_leaves_out_empty_fields(
|
||||
hass, mock_config_entry, mock_init_component, mock_create_stream
|
||||
) -> None:
|
||||
"""Test unknown location fields are left out of the web search tool.
|
||||
|
||||
A failed location lookup stores no city or region; endpoints in front of
|
||||
Anthropic models reject empty strings there.
|
||||
"""
|
||||
subentry = next(iter(mock_config_entry.subentries.values()))
|
||||
hass.config_entries.async_update_subentry(
|
||||
mock_config_entry,
|
||||
subentry,
|
||||
data={
|
||||
**subentry.data,
|
||||
CONF_WEB_SEARCH: True,
|
||||
CONF_WEB_SEARCH_USER_LOCATION: True,
|
||||
CONF_WEB_SEARCH_COUNTRY: "DE",
|
||||
CONF_WEB_SEARCH_TIMEZONE: "Europe/Berlin",
|
||||
},
|
||||
)
|
||||
await hass.config_entries.async_reload(mock_config_entry.entry_id)
|
||||
mock_create_stream.return_value = [
|
||||
create_message_item(id="msg_A", text="Hi", output_index=0)
|
||||
]
|
||||
|
||||
await conversation.async_converse(
|
||||
hass, "hello", None, Context(), agent_id="conversation.openai_conversation"
|
||||
)
|
||||
|
||||
tools = mock_create_stream.mock_calls[0][2]["tools"]
|
||||
web_search = next(tool for tool in tools if tool["type"] == "web_search")
|
||||
assert web_search["user_location"] == {
|
||||
"type": "approximate",
|
||||
"country": "DE",
|
||||
"timezone": "Europe/Berlin",
|
||||
}
|
||||
Reference in new issue
Block a user