Parse a Tool-Call JSON Payload

Basics
BasicsFunction CallingJSONOpenAIAnthropicGoogle

Problem

Implement parse_tool_call(raw_json) that parses a JSON string containing a required 'name' field and an optional 'arguments' object (defaulting to {} if omitted) and returns (name, arguments).

Why It Matters: Every function-calling agent runtime must decode structured LLM tool-call payloads cleanly before invoking local Python functions or MCP tools.

Function Signature

import json

def parse_tool_call(raw_json: str) -> tuple[str, dict]:
    """Parse JSON tool call and return (name, arguments_dict)."""

Sample Input & Output

Example 1: Full Tool-Call Payload

Input:

raw_json = '{"name": "get_weather", "arguments": {"city": "Tokyo"}}'

Expected Output:

("get_weather", {"city": "Tokyo"})

Explanation: Extracts both 'name' and the nested 'arguments' dictionary.

Example 2: Zero-Argument Tool (Omitted arguments Key)

Input:

raw_json = '{"name": "list_open_tabs"}'

Expected Output:

("list_open_tabs", {})

Explanation: When 'arguments' is absent, safely defaults to an empty dictionary {} instead of raising KeyError.

Constraints & Edge Cases

  • Return (tool_name, arguments_dict).
  • Assume raw_json is valid JSON containing a string 'name' key.
  • Return a 2-tuple (str, dict).

Constraints & Evaluation Criteria

  • Return `(tool_name, arguments_dict)`.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
get_weather(city='Tokyo')
Expected Output / Criteria
('get_weather', {'city':'Tokyo'})