github/copilot-sdk · error · IllegalArgumentException

Invalid escape sequence \

Error message

Invalid escape sequence \<escaped> at position <pos-2>

What it means

parseEscape read the character following a backslash and it was not one of the valid JSON escape characters (" \ / b f n r t u). Strict JSON defines a closed set of escapes; anything else, such as \x or \', is rejected with this IllegalArgumentException, reporting the position of the backslash (pos-2).

Solutions

  1. Replace the invalid escape with the correct JSON one (\u0041 instead of \x41) or double the backslash (\\d) so it is a literal backslash
  2. Generate the JSON with a serializer rather than hand-concatenation so escaping is handled for you
  3. Search the input at the reported position to see which escape character triggered it
  4. If the content is meant to be a regex or non-JSON escaped string, escape it for JSON on top of its own syntax

Example fix

// before
String json = "{\"regex\":\"\\d+\"}"; // \d is invalid in JSON
// after
String json = "{\"regex\":\"\\\\d+\"}"; // \\d -> literal backslash + d
Defensive patterns

Strategy: try-catch

Validate before calling

java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\\\\\\\[^\"\\\\/bfnrtu]").matcher(json);
if (m.find()) {
    throw new IllegalArgumentException("Invalid JSON escape at index " + m.start());
}

Try / catch

try {
    String result = CopilotToolProcessor.jsonToMapOfSource(json);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("Invalid escape sequence")) {
        // fix escaping at reported position, e.g. replace \x with \u00xx or \\\\
    }
}

Prevention

When it happens

Trigger: Parsing JSON containing an invalid escape like '\x41' or '\e'; strings produced by non-JSON escape conventions (Python-style \x, regex escapes like \d) embedded directly into JSON strings.

Common situations: Hand-writing JSON with regex patterns ('\d+' unescaped), single-quoted-string habits ('\''), using string literals from another language without re-escaping; library mix-ups where a non-JSON serializer's output is fed to a strict JSON parser.

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/4100e791446499cc. Report an issue: GitHub.

Appendix: source

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

            }
            expect('"');
            return sb.toString();
        }

        private char parseEscape() {
            if (pos >= input.length()) {
                throw new IllegalArgumentException("Unterminated string escape at position " + pos);
            }
            char escaped = input.charAt(pos++);
            return switch (escaped) {
                case '"', '\\', '/' -> escaped;
                case 'b' -> '\b';
                case 'f' -> '\f';
                case 'n' -> '\n';
                case 'r' -> '\r';
                case 't' -> '\t';
                case 'u' -> parseUnicodeEscape();
                default -> throw new IllegalArgumentException(
                        "Invalid escape sequence \\" + escaped + " at position " + (pos - 2));
            };
        }

        private char parseUnicodeEscape() {
            if (pos + 4 > input.length()) {
                throw new IllegalArgumentException("Incomplete Unicode escape at position " + (pos - 2));
            }
            int value = 0;
            for (int i = 0; i < 4; i++) {
                char hex = input.charAt(pos++);
                if (!isAsciiHexDigit(hex)) {
                    throw new IllegalArgumentException("Invalid Unicode escape at position " + (pos - 1));
                }
                int digit = Character.digit(hex, 16);
                value = (value << 4) | digit;
            }
            return (char) value;

View on GitHub (pinned to cd8cf15dc3)