{"record":{"id":"8c08413bbf1044aa","repo":"PrefectHQ/fastmcp","slug":"expected-json-schema-type-got-raw-r","errorCode":null,"errorMessage":"Expected JSON {schema_type}, got {raw!r}","messagePattern":"Expected JSON (.+?), got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/cli/client.py","lineNumber":292,"sourceCode":"\n    if schema_type == \"number\":\n        try:\n            return float(raw)\n        except ValueError:\n            raise ValueError(f\"Expected number, got {raw!r}\") from None\n\n    if schema_type == \"boolean\":\n        if raw.lower() in (\"true\", \"1\", \"yes\"):\n            return True\n        if raw.lower() in (\"false\", \"0\", \"no\"):\n            return False\n        raise ValueError(f\"Expected boolean, got {raw!r}\")\n\n    if schema_type in (\"array\", \"object\"):\n        try:\n            return json.loads(raw)\n        except json.JSONDecodeError:\n            raise ValueError(f\"Expected JSON {schema_type}, got {raw!r}\") from None\n\n    # Default: treat as string\n    return raw\n\n\ndef parse_tool_arguments(\n    raw_args: tuple[str, ...],\n    input_json: str | None,\n    input_schema: dict[str, Any],\n) -> dict[str, Any]:\n    \"\"\"Build a tool-call argument dict from CLI inputs.\n\n    A single JSON object argument is treated as the full argument dict.\n    ``--input-json`` provides the base dict; ``key=value`` pairs override.\n    Values are coerced using the tool's ``inputSchema``.\n    \"\"\"\n\n    # A single positional arg that looks like JSON → treat as input-json","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/cli/client.py#L274-L310","documentation":"coerce_value converts raw CLI strings into typed tool arguments during terminal elicitation. When the schema declares an 'array' or 'object' parameter, the raw input must be valid JSON; otherwise a ValueError('Expected JSON {schema_type}, got {raw!r}') is raised to tell the caller the string could not be parsed.","triggerScenarios":"Calling coerce_value (via _terminal_elicitation_handler or parse_tool_arguments) with schema_type 'array' or 'object' and a raw string that json.loads rejects, e.g. '[1, 2' or \"{'a': 1}\" (single quotes).","commonSituations":"Users typing tool arguments at the terminal and forgetting to quote JSON, using shell-style single quotes, or pasting Python dict literals instead of JSON objects.","solutions":["Pass valid JSON for array/object parameters: double-quoted keys, no trailing commas, e.g. '{\"a\": 1}' or '[1, 2]'","Re-run the tool call and enter the argument as a single JSON value on one line","If the schema actually allows a plain string, ensure the schema_type is declared correctly on the tool parameter"],"exampleFix":"// before\nparse_tool_arguments(tool, ['items={1, 2}'])  # ValueError: Expected JSON array\n// after\nparse_tool_arguments(tool, ['items=[1, 2]'])","handlingStrategy":"validation","validationCode":"import json\ndef is_valid_json(raw: str) -> bool:\n    try:\n        json.loads(raw)\n        return True\n    except json.JSONDecodeError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    value = coerce_value(schema_type, raw)\nexcept ValueError as e:\n    print(f\"Invalid input: {e}; enter valid JSON for {schema_type}\")","preventionTips":["Quote JSON arguments with double quotes and valid JSON syntax","Validate JSON with python -m json.tool before pasting","Prefer string-typed parameters in tool schemas when free-form input is expected"],"tags":["cli","json-parsing","validation"],"backgroundTag":"invalid-json-input","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}