PrefectHQ/fastmcp · error · ValueError

Expected JSON {schema_type}, got {raw!r}

Error message

Expected JSON {schema_type}, got {raw!r}

What it means

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.

Source

Thrown at fastmcp_slim/fastmcp/cli/client.py:292

    if schema_type == "number":
        try:
            return float(raw)
        except ValueError:
            raise ValueError(f"Expected number, got {raw!r}") from None

    if schema_type == "boolean":
        if raw.lower() in ("true", "1", "yes"):
            return True
        if raw.lower() in ("false", "0", "no"):
            return False
        raise ValueError(f"Expected boolean, got {raw!r}")

    if schema_type in ("array", "object"):
        try:
            return json.loads(raw)
        except json.JSONDecodeError:
            raise ValueError(f"Expected JSON {schema_type}, got {raw!r}") from None

    # Default: treat as string
    return raw


def parse_tool_arguments(
    raw_args: tuple[str, ...],
    input_json: str | None,
    input_schema: dict[str, Any],
) -> dict[str, Any]:
    """Build a tool-call argument dict from CLI inputs.

    A single JSON object argument is treated as the full argument dict.
    ``--input-json`` provides the base dict; ``key=value`` pairs override.
    Values are coerced using the tool's ``inputSchema``.
    """

    # A single positional arg that looks like JSON → treat as input-json

View on GitHub (pinned to 1f02114297)

Solutions

  1. Pass valid JSON for array/object parameters: double-quoted keys, no trailing commas, e.g. '{"a": 1}' or '[1, 2]'
  2. Re-run the tool call and enter the argument as a single JSON value on one line
  3. If the schema actually allows a plain string, ensure the schema_type is declared correctly on the tool parameter

Example fix

// before
parse_tool_arguments(tool, ['items={1, 2}'])  # ValueError: Expected JSON array
// after
parse_tool_arguments(tool, ['items=[1, 2]'])
Defensive patterns

Strategy: validation

Validate before calling

import json
def is_valid_json(raw: str) -> bool:
    try:
        json.loads(raw)
        return True
    except json.JSONDecodeError:
        return False

Try / catch

try:
    value = coerce_value(schema_type, raw)
except ValueError as e:
    print(f"Invalid input: {e}; enter valid JSON for {schema_type}")

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/8c08413bbf1044aa. Report an issue: GitHub.