When a model decides to use a tool it emits a tool call: the tool's name and its arguments, usually as JSON. Models are not parsers, so the JSON arrives wrapped in friendly prose ("Sure! I'll look that up:"), inside a markdown code fence, or with the arguments themselves encoded as a JSON string (the convention of some APIs). A robust agent has to find the call in this mess. A reliable method does not trust a regular expression to match braces, it asks a real JSON decoder to try parsing at every opening brace until one parse succeeds and looks like a tool call.
Implement parse_tool_call.
parse_tool_call(text) returns (name, args). Scan the text for { characters from left to right. At each one try json.JSONDecoder().raw_decode(text, index); skip positions where decoding fails.name and an arguments key is the call. If arguments is a string, parse it with json.loads into a dict; otherwise it must already be a dict.ValueError.raw_decode returns (object, end_index) and raises json.JSONDecodeError on bad input.
An earlier JSON object that is not a tool call (for example an example in the prose) must be skipped, not rejected.
No results yet
Run your code to check it against the examples, or submit to run every test.