Fix JSON serialization of time objects in anthropic tool results (#160459)

Co-authored-by: Artur Pragacz <49985303+arturpragacz@users.noreply.github.com>
This commit is contained in:
Dan Čermák
2026-01-09 12:06:36 +01:00
committed by GitHub
co-authored by Artur Pragacz
parent 72a3523193
commit 9d8dc9ec06
3 changed files with 90 additions and 4 deletions
@@ -309,12 +309,12 @@
'type': 'text',
}),
dict({
'content': '{"success": true, "response": "Lights are off."}',
'content': '{"success":true,"response":"Lights are off."}',
'tool_use_id': 'mock-tool-call-id',
'type': 'tool_result',
}),
dict({
'content': '{"success": false, "response": "Not enough milk."}',
'content': '{"success":false,"response":"Not enough milk."}',
'tool_use_id': 'mock-tool-call-id-2',
'type': 'tool_result',
}),
@@ -462,6 +462,62 @@
}),
])
# ---
# name: test_history_conversion[content6]
list([
dict({
'content': 'What time is it?',
'role': 'user',
}),
dict({
'content': list([
dict({
'text': 'Let me check the time for you.',
'type': 'text',
}),
dict({
'id': 'mock-tool-call-id',
'input': dict({
}),
'name': 'GetCurrentTime',
'type': 'tool_use',
}),
]),
'role': 'assistant',
}),
dict({
'content': list([
dict({
'content': '{"speech_slots":{"time":"14:30:00"},"message":"Current time retrieved"}',
'tool_use_id': 'mock-tool-call-id',
'type': 'tool_result',
}),
]),
'role': 'user',
}),
dict({
'content': list([
dict({
'text': 'It is currently 2:30 PM.',
'type': 'text',
}),
]),
'role': 'assistant',
}),
dict({
'content': 'Are you sure?',
'role': 'user',
}),
dict({
'content': list([
dict({
'text': 'Yes, I am sure!',
'type': 'text',
}),
]),
'role': 'assistant',
}),
])
# ---
# name: test_redacted_thinking
list([
dict({
@@ -1,5 +1,6 @@
"""Tests for the Anthropic integration."""
import datetime
from typing import Any
from unittest.mock import AsyncMock, Mock, patch
@@ -317,7 +318,7 @@ async def test_function_exception(
"role": "user",
"content": [
{
"content": '{"error": "HomeAssistantError", "error_text": "Test tool exception"}',
"content": '{"error":"HomeAssistantError","error_text":"Test tool exception"}',
"tool_use_id": "toolu_0123456789AbCdEfGhIjKlM",
"type": "tool_result",
}
@@ -893,6 +894,34 @@ async def test_web_search(
),
),
],
[
conversation.chat_log.SystemContent("You are a helpful assistant."),
conversation.chat_log.UserContent("What time is it?"),
conversation.chat_log.AssistantContent(
agent_id="conversation.claude_conversation",
content="Let me check the time for you.",
tool_calls=[
llm.ToolInput(
id="mock-tool-call-id",
tool_name="GetCurrentTime",
tool_args={},
),
],
),
conversation.chat_log.ToolResultContent(
agent_id="conversation.claude_conversation",
tool_call_id="mock-tool-call-id",
tool_name="GetCurrentTime",
tool_result={
"speech_slots": {"time": datetime.time(14, 30, 0)},
"message": "Current time retrieved",
},
),
conversation.chat_log.AssistantContent(
agent_id="conversation.claude_conversation",
content="It is currently 2:30 PM.",
),
],
],
)
async def test_history_conversion(