d05 · Serialize a tool result (~6 min)
Serialization. Edit drills.py. Standard library only; no model calls, no API key.
def to_tool_result(tool_use_id, output, is_error=False):
Build a tool_result block: {"type", "tool_use_id", "content"[, "is_error"]}.
"content" is always a string:
- a str output is used as is (no added quotes);
- anything else becomes JSON text, where datetimes/dates become ISO 8601 strings, Decimals become their exact string ("0.10" stays "0.10"), and sets become sorted lists, at any nesting depth;
- non-ASCII text stays readable ("東京", not
"\u6771\u4eac").
Include "is_error": True only when is_error is true.
Failure messages are enough to go on, so leave the tests unread until you submit.