Learn / GitHub Actions / Cache key invalid chars
GitHub Actions "cache key contains invalid characters"
By Daniel Zoghalchali · Latchkey
Cache keys must avoid certain reserved characters - notably commas, which separate restore-keys, and control characters. A key containing them is rejected before any cache operation runs.
What this error means A cache step fails validation because the key contains disallowed characters.
github-actions
Error: Key Validation Error: ${{ matrix.os }},node-20 cannot contain commas.
Cache key contains invalid characters.Common causes Comma in the key Commas are reserved as the restore-keys separator and are not allowed inside a single key.
Newlines or control characters A key built from multiline content can include disallowed characters.
How to fix it Sanitize the key
Replace commas with hyphens or another safe separator.
Trim newlines; build keys from single-line, predictable values like hashFiles.
.github/workflows/ci.yml
- uses : actions/cache@v4
with :
path : ~/.npm
key : ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} This will hit your next GitHub Actions build too
The fix you just applied is mechanical, and nothing about it needed a human. On Latchkey managed runners this failure is detected, repaired, and the job retried automatically, so the time you just spent is not spent again. Start free - 30-day trial, no credit card required - or see how self-healing works .
How to prevent it
Compose keys from os, a label, and hashFiles - no commas.
Avoid interpolating raw multiline content into keys.
Frequently asked questions What causes ""cache key contains invalid characters""? Commas are reserved as the restore-keys separator and are not allowed inside a single key.
How do I fix "cache key contains invalid characters"? Sanitize the key
Related guides References
Start free on self-healing runners
Workflow YAML breaks in ways that only show up on the runner. Latchkey runs your GitHub Actions on managed runners that detect, fix, and retry transient failures automatically. Switching is one line: change runs-on: and the rest of the workflow is untouched. Up to 70% cheaper than GitHub-hosted, with free minutes on every plan.
30-day free trial · No credit card required · Set up in minutes
Previous Cache immutableNext Cache not found for keys