github/copilot-sdk · error · IllegalArgumentException

Expected number at position

Error message

Expected number at position <start>

What it means

parseNumber requires that after an optional leading minus there is at least one digit forming a valid JSON number. If the input ends right after the '-' or the next character is not a digit, this IllegalArgumentException is thrown reporting the start position of the would-be number. Note the reported position is the start of the number, not where parsing stopped.

Solutions

  1. Supply a valid JSON number (integer or decimal) at the reported position — JSON numbers must start with a digit after the optional minus
  2. If the value is genuinely not a number, use null instead of a blank/placeholder
  3. Check templates/interpolation that left a blank where a number should be; JSON forbids NaN/Infinity — omit the field or use null
  4. Validate the full JSON with a standard parser to find all malformed numbers

Example fix

// before
String json = "{\"count\":-}"; // empty number
// after
String json = "{\"count\":0}"; // or null if absent
Defensive patterns

Strategy: validation

Validate before calling

java.util.regex.Matcher m = java.util.regex.Pattern.compile(":\\s*-\\s*[,}\\]]").matcher(json);
if (m.find()) {
    throw new IllegalArgumentException("Empty numeric value at index " + m.start());
}

Try / catch

try {
    String result = CopilotToolProcessor.jsonToMapOfSource(json);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("Expected number")) {
        // fill in a valid number or use null at the reported start position
    }
}

Prevention

When it happens

Trigger: Parsing JSON with '-' or '-x' where a number is expected, e.g. '{"n":-}' or '{"n":-abc}'; also truncated payloads ending in '-'.

Common situations: Hand-built JSON with an empty numeric value or a placeholder ('-{value}' where the template variable was blank); NaN/Infinity written literally (JSON forbids them) so the parser sees 'N'/'I' after nothing valid; truncated streams.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/b51fc63a4c8b6926. Report an issue: GitHub.

Appendix: source

Thrown at java/sdk/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java:1106

            }
            throw new IllegalArgumentException("Expected boolean at position " + pos);
        }

        private String parseNull() {
            if (input.startsWith("null", pos)) {
                pos += 4;
                return "(Object) null";
            }
            throw new IllegalArgumentException("Expected null at position " + pos);
        }

        private String parseNumber() {
            int start = pos;
            if (pos < input.length() && input.charAt(pos) == '-') {
                pos++;
            }
            if (pos >= input.length()) {
                throw new IllegalArgumentException("Expected number at position " + start);
            }
            if (input.charAt(pos) == '0') {
                pos++;
            } else if (isDigitOneToNine(input.charAt(pos))) {
                consumeDigits();
            } else {
                throw new IllegalArgumentException("Expected number at position " + pos);
            }
            if (pos < input.length() && input.charAt(pos) == '.') {
                pos++;
                requireDigit("fraction");
                consumeDigits();
            }
            if (pos < input.length() && (input.charAt(pos) == 'e' || input.charAt(pos) == 'E')) {
                pos++;
                if (pos < input.length() && (input.charAt(pos) == '+' || input.charAt(pos) == '-')) {
                    pos++;
                }

View on GitHub (pinned to cd8cf15dc3)