PrefectHQ/fastmcp · error · ValueError

Expected number, got {raw!r}

Error message

Expected number, got {raw!r}

What it means

coerce_value handles schema type number by calling float(raw); on ValueError it raises this message including the raw input. Float parsing is stricter than the integer branch — '1.5' works but 'abc', empty strings, or locale-formatted numbers ('1,5') fail.

Source

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

# ---------------------------------------------------------------------------


def coerce_value(raw: str, schema: dict[str, Any]) -> Any:
    """Coerce a string CLI value according to a JSON-Schema type hint."""

    schema_type = schema.get("type", "string")

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

    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

View on GitHub (pinned to 1f02114297)

Solutions

  1. Provide a plain numeric string such as 3.14 or -0.5
  2. Strip units/formatting from input before passing it
  3. Pre-convert with float() in scripts to fail early with your own message

Example fix

# before
--price '3,50'   # ValueError
# after
--price 3.50
Defensive patterns

Strategy: validation

Validate before calling

def is_number_str(raw: str) -> bool:
    try:
        float(raw); return True
    except ValueError:
        return False

Type guard

def coerce_float(raw: str) -> float | None:
    try:
        return float(raw)
    except ValueError:
        return None

Try / catch

try:
    value = coerce_value(schema, raw)
except ValueError as exc:
    print(f'Invalid input: {exc}; expected a number')
    raw = input('> ')

Prevention

When it happens

Trigger: Supplying a non-numeric string for a number-typed tool parameter via parse_tool_arguments or the terminal elicitation handler.

Common situations: Locale decimal commas from international users; unit-suffixed input like '3.5kg'; empty prompt submissions.

Related errors


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