Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix(core): handle malformed tool_call args in Anthropic serialization (#10185)
Sarah Wooders9d3d7d30288f
diff --git a/letta/schemas/message.py b/letta/schemas/message.pyindex 957997991..2496f5968 100644--- a/letta/schemas/message.py+++ b/letta/schemas/message.py@@ -62,7 +62,7 @@ from letta.schemas.letta_message_content import (get_letta_message_content_union_str_json_schema,)from letta.system import unpack_message-from letta.utils import parse_json, sanitize_tool_call_id, validate_function_response+from letta.utils import parse_json, parse_json_or_wrap_raw, sanitize_tool_call_id, validate_function_responsedef truncate_tool_return(content: Optional[str], limit: Optional[int]) -> Optional[str]:@@ -2012,7 +2012,18 @@ class Message(BaseMessage):inner_thoughts_key=INNER_THOUGHTS_KWARG,).model_dump()else:- tool_call_input = parse_json(tool_call.function.arguments)+ tool_call_input = parse_json_or_wrap_raw(+ tool_call.function.arguments,+ context={+ "serializer": "anthropic",+ "message_id": self.id,+ "agent_id": self.agent_id,+ "run_id": self.run_id,+ "step_id": self.step_id,+ "tool_name": tool_call.function.name,+ "tool_call_id": tool_call.id,+ },+ )if strip_request_heartbeat:tool_call_input.pop(REQUEST_HEARTBEAT_PARAM, None)@@ -2244,12 +2255,19 @@ class Message(BaseMessage):for tool_call in self.tool_calls:function_name = tool_call.function.namefunction_args = tool_call.function.arguments- try:- # NOTE: Google AI wants actual JSON objects, not strings- function_args = parse_json(function_args)- except Exception:- raise UserWarning(f"Failed to parse JSON function args: {function_args}")- function_args = {"args": function_args}+ # NOTE: Google AI wants actual JSON objects, not strings+ function_args = parse_json_or_wrap_raw(+ function_args,+ context={+ "serializer": "google",+ "message_id": self.id,+ "agent_id": self.agent_id,+ "run_id": self.run_id,+ "step_id": self.step_id,+ "tool_name": function_name,+ "tool_call_id": tool_call.id,+ },+ )if put_inner_thoughts_in_kwargs and text_content is not None:assert INNER_THOUGHTS_KWARG not in function_args, function_argsdiff --git a/letta/utils.py b/letta/utils.pyindex 852b46708..410553d1f 100644--- a/letta/utils.py+++ b/letta/utils.py@@ -872,6 +872,29 @@ def parse_json(string) -> dict:raise e+def parse_json_or_wrap_raw(+ string: str,+ wrapper_key: str = "_malformed_tool_arguments",+ context: Optional[dict[str, Any]] = None,+) -> dict:+ """Parse JSON into a dict, returning a wrapped raw payload on parse failure.++ This is intended for serialization paths where we prefer degraded, non-fatal behavior+ over failing an entire run because historical tool-call args are malformed.+ """+ try:+ return parse_json(string)+ except Exception as e:+ context = context or {}+ logger.warning(+ "Failed to parse JSON payload, falling back to wrapped raw payload. wrapper_key=%s error=%s context=%s",+ wrapper_key,+ e,+ context,+ )+ return {wrapper_key: string}++def validate_function_response(function_response: Any, return_char_limit: int, strict: bool = False, truncate: bool = True) -> str | dict[str, Any]:diff --git a/tests/test_message_serialization.py b/tests/test_message_serialization.pyindex c0124a257..23992f396 100644--- a/tests/test_message_serialization.py+++ b/tests/test_message_serialization.py@@ -1,3 +1,5 @@+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function+from letta.llm_api.openai_client import fill_image_content_in_responses_inputfrom letta.schemas.enums import MessageRolefrom letta.schemas.letta_message_content import Base64Image, ImageContent, TextContent@@ -30,3 +32,51 @@ def test_to_openai_responses_dicts_handles_image_only_content():serialized = Message.to_openai_responses_dicts_from_list([message])parts = serialized[0]["content"]assert parts[0]["type"] == "input_image"+++def test_to_anthropic_dict_falls_back_for_malformed_tool_call_arguments():+ malformed_args = '{"message": "unterminated}'+ msg = Message(+ role=MessageRole.assistant,+ content=[TextContent(text="thinking")],+ tool_calls=[+ ChatCompletionMessageToolCall(+ id="call_test_malformed",+ type="function",+ function=Function(name="send_message", arguments=malformed_args),+ )+ ],+ )++ serialized = msg.to_anthropic_dict(+ current_model="anthropic/claude-sonnet-4-5-20250929",+ inner_thoughts_xml_tag="thinking",+ put_inner_thoughts_in_kwargs=False,+ )++ tool_use_items = [item for item in serialized["content"] if item.get("type") == "tool_use"]+ assert len(tool_use_items) == 1+ assert tool_use_items[0]["input"] == {"_malformed_tool_arguments": malformed_args}+++def test_to_google_dict_falls_back_for_malformed_tool_call_arguments():+ malformed_args = '{"message": "unterminated}'+ msg = Message(+ role=MessageRole.assistant,+ content=[],+ tool_calls=[+ ChatCompletionMessageToolCall(+ id="call_test_malformed_google",+ type="function",+ function=Function(name="send_message", arguments=malformed_args),+ )+ ],+ )++ serialized = msg.to_google_dict(+ current_model="google/gemini-2.5-pro",+ )++ function_calls = [item for item in serialized["parts"] if item.get("functionCall")]+ assert len(function_calls) == 1+ assert function_calls[0]["functionCall"]["args"] == {"_malformed_tool_arguments": malformed_args}