"""Response formatting utilities for tools."""
import json
from datetime import datetime
from typing import Any
def serialize_response(data: dict[str, Any]) -> str:
"""
Serialize response with proper datetime handling.
Args:
data: Response data dictionary
Returns:
JSON string with proper formatting
"""
def default(obj: Any) -> str:
"""Handle non-serializable objects."""
if isinstance(obj, datetime):
return obj.isoformat()
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
return json.dumps(data, indent=2, default=default)
def create_error_response(
error_message: str, error_code: str | None = None
) -> dict[str, Any]:
"""
Create standardized error response.
Args:
error_message: Human-readable error message
error_code: Machine-readable error code
Returns:
Standardized error response dictionary
"""
return {
"success": False,
"error": error_message,
"error_code": error_code,
}
__all__ = ["serialize_response", "create_error_response"]