Parse a Tool Call
When a model wants to act, it emits a tool call like search(query=cats, limit=5). Before your harness can run it, you have to parse that string into a name and typed arguments. Build the little parser that turns a model's intent into a dispatchable call.
The problem
Implement parse_tool_call(s). The input is a call of the form name(k1=v1, k2=v2, …). Return a dict with the function name and an args dict of its keyword arguments. Strip surrounding quotes from string values, and convert values that are whole integers (optionally negative) to int. A call with no arguments — now() — has an empty args dict.
search(query=cats, limit=5){'name': 'search', 'args': {'query': 'cats', 'limit': 5}}now(){'name': 'now', 'args': {}}calc(x=-3){'name': 'calc', 'args': {'x': -3}}- The name is everything before the first
(; the arguments are between the outer parentheses. - Split arguments on commas and each
key=valueon the first=; trim whitespace. - Strip a single layer of surrounding single or double quotes from values.
- Convert values matching an optional
-followed by digits to an integer; leave everything else as a string. (Test inputs have no commas inside values.)
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement parse_tool_call(s) — split a name(k=v, …) call string into {name, args}, unquoting strings and converting integer values.
- name = the text before the first "("; inner = the text between the first "(" and last ")".
- If inner is empty, args is {} — return early-ish.
- For each comma-separated part, split once on "=" into key and value, and strip whitespace.
- Strip surrounding quotes; if the value is all digits (allowing a leading "-"), int() it.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.