Building AI agents, atomically
fix: use Pydantic JSON serialization for tool arguments (#312)
ningyixidb1674e26157
diff --git a/atomic-agents/atomic_agents/utils/format_tool_message.py b/atomic-agents/atomic_agents/utils/format_tool_message.pyindex c3dd947..3de8285 100644--- a/atomic-agents/atomic_agents/utils/format_tool_message.py+++ b/atomic-agents/atomic_agents/utils/format_tool_message.py@@ -6,7 +6,7 @@ from typing import Dict, Optional, Typedef format_tool_message(tool_call: Type[BaseModel], tool_id: Optional[str] = None) -> Dict:"""- Formats a message for a tool call.+ Formats a message for a tool call, using Pydantic's JSON representations for arguments.Args:tool_call (Type[BaseModel]): The Pydantic model instance representing the tool call.@@ -24,6 +24,6 @@ def format_tool_message(tool_call: Type[BaseModel], tool_id: Optional[str] = Non"type": "function","function": {"name": tool_call.__class__.__name__,- "arguments": json.dumps(tool_call.model_dump(), separators=(", ", ": ")),+ "arguments": json.dumps(tool_call.model_dump(mode="json"), separators=(", ", ": ")),},}diff --git a/atomic-agents/tests/utils/test_format_tool_message.py b/atomic-agents/tests/utils/test_format_tool_message.pyindex 6f75ff1..b43f290 100644--- a/atomic-agents/tests/utils/test_format_tool_message.py+++ b/atomic-agents/tests/utils/test_format_tool_message.py@@ -1,5 +1,11 @@import uuid-from pydantic import BaseModel+import json+from datetime import date, datetime, timezone+from decimal import Decimal+from enum import Enum+from pathlib import Path++from pydantic import BaseModel, field_serializerimport pytestfrom atomic_agents import BaseIOSchemafrom atomic_agents.utils import format_tool_message@@ -90,5 +96,59 @@ def test_format_tool_message_with_complex_model():assert result["function"]["arguments"] == '{"nested": {"key": "value"}, "list_field": [1, 2, 3]}'+def test_format_tool_message_serializes_pydantic_json_types():+ class Priority(Enum):+ HIGH = "high"++ class Details(BaseModel):+ created: datetime+ due: date+ request_id: uuid.UUID+ amount: Decimal+ path: Path+ priority: Priority++ class RichToolCall(BaseModel):+ details: Details++ tool_call = RichToolCall(+ details=Details(+ created=datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone.utc),+ due=date(2026, 1, 3),+ request_id=uuid.UUID("12345678-1234-5678-1234-567812345678"),+ amount=Decimal("12.50"),+ path=Path("report.txt"),+ priority=Priority.HIGH,+ )+ )++ result = format_tool_message(tool_call, "request-1")++ assert result["function"]["name"] == "RichToolCall"+ assert json.loads(result["function"]["arguments"]) == {+ "details": {+ "created": "2026-01-02T03:04:05Z",+ "due": "2026-01-03",+ "request_id": "12345678-1234-5678-1234-567812345678",+ "amount": "12.50",+ "path": "report.txt",+ "priority": "high",+ }+ }+++def test_format_tool_message_honors_json_field_serializers():+ class SerializedToolCall(BaseModel):+ value: int++ @field_serializer("value", when_used="json")+ def serialize_value(self, value):+ return f"item-{value}"++ result = format_tool_message(SerializedToolCall(value=42))++ assert json.loads(result["function"]["arguments"]) == {"value": "item-42"}++if __name__ == "__main__":pytest.main()diff --git a/docs/api/utils.md b/docs/api/utils.mdindex 81cb918..8446040 100644--- a/docs/api/utils.md+++ b/docs/api/utils.md@@ -152,6 +152,10 @@ serialized request.## Tool Message Formatting+Tool arguments use Pydantic's JSON representations for values such as dates,+UUIDs, decimals, paths, and enums. JSON-only field serializers are honored when+formatting the arguments.+```{eval-rst}.. automodule:: atomic_agents.utils.format_tool_message:members: