Fix Anthropic bug parsing a streaming response with no json (#142745)

This commit is contained in:
Allen Porter 2025-04-11 08:30:12 -07:00 committed by Franck Nijhof
parent 28d776a0b0
commit d868f39aea
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 19 additions and 3 deletions

View File

@ -266,7 +266,7 @@ async def _transform_stream(
raise ValueError("Unexpected stop event without a current block")
if current_block["type"] == "tool_use":
tool_block = cast(ToolUseBlockParam, current_block)
tool_args = json.loads(current_tool_args)
tool_args = json.loads(current_tool_args) if current_tool_args else {}
tool_block["input"] = tool_args
yield {
"tool_calls": [

View File

@ -303,11 +303,27 @@ async def test_conversation_agent(
@patch("homeassistant.components.anthropic.conversation.llm.AssistAPI._async_get_tools")
@pytest.mark.parametrize(
("tool_call_json_parts", "expected_call_tool_args"),
[
(
['{"param1": "test_value"}'],
{"param1": "test_value"},
),
(
['{"para', 'm1": "test_valu', 'e"}'],
{"param1": "test_value"},
),
([""], {}),
],
)
async def test_function_call(
mock_get_tools,
hass: HomeAssistant,
mock_config_entry_with_assist: MockConfigEntry,
mock_init_component,
tool_call_json_parts: list[str],
expected_call_tool_args: dict[str, Any],
) -> None:
"""Test function call from the assistant."""
agent_id = "conversation.claude"
@ -343,7 +359,7 @@ async def test_function_call(
1,
"toolu_0123456789AbCdEfGhIjKlM",
"test_tool",
['{"para', 'm1": "test_valu', 'e"}'],
tool_call_json_parts,
),
]
)
@ -387,7 +403,7 @@ async def test_function_call(
llm.ToolInput(
id="toolu_0123456789AbCdEfGhIjKlM",
tool_name="test_tool",
tool_args={"param1": "test_value"},
tool_args=expected_call_tool_args,
),
llm.LLMContext(
platform="anthropic",