Standard library only · runs in your browser
Every agent starts here: the model replies with text, and your code has to decide whether that text is a tool call or a final answer. Real models wrap JSON in prose and code fences, so a naive json.loads crashes on day one.
Implement parse_tool_call(text).
text does not contain the substring "tool" (with the double quotes), it's a final answer: return None.{ to the last }.{"tool": name, "arguments": args}. A missing arguments key means {}.ValueError (not json's own JSONDecodeError) when the JSON is invalid, when tool is missing or empty, or when arguments isn't an object.parse_tool_call(text: str) -> dict | None