fix(skills): auto-prefix upload paths with skill name directory
Context
beta.skills.create requires every uploaded file path to be prefixed with a top-level directory whose name matches the name: field in SKILL.md (e.g. my-skill/SKILL.md, my-skill/scripts/run.py). This constraint is not documented in the method's docstring or parameter descriptions. Callers who pass bare paths ("SKILL.md") receive a cryptic 400 response:
SKILL.md file must be exactly in the top-level folder
folder name scripts must match skill name my-skill
There is no hint in the error that the fix is a path prefix, and the required layout is only discoverable by trial-and-error or reading the download-side code (_archive_top_dir in lib/tools/_skills.py). Tracked in #1575.
Change
src/anthropic/lib/tools/_skills.py — three new helpers:
_parse_skill_name_from_frontmatter(content)— extracts thename:value from a SKILL.md YAML front-matter block._read_file_entry_bytes(content)— reads raw bytes from anyFileContentvalue (bytes,PathLike,IOBase), rewinding IO streams after reading.normalize_skill_upload_paths(files, *, display_title)— locatesSKILL.mdin the file list, resolves the skill name (front-matter → first already-prefixed path →display_titlefallback), then prefixes every bare path. Already-prefixed paths pass through unchanged; returns original sequence if name cannot be determined.
src/anthropic/resources/beta/skills/skills.py — wires normalize_skill_upload_paths into both Skills.create and AsyncSkills.create before the multipart body is built.
Why this approach
The fix is purely additive: it normalises paths before they reach the HTTP layer, so existing callers who already use the correct layout are unaffected (idempotent). The skill name is derived from the same SKILL.md front-matter field that the server validates against, keeping the client and server in sync without hard-coding any naming rules. The approach mirrors what _archive_top_dir + _extract_skill_archive already do on the download side, making upload/download symmetric.
Alternative considered: document the constraint in the docstring and raise a ValueError for bare paths. Rejected — a ValueError would break callers silently passing valid-looking paths, and documentation alone doesn't help interactive callers who don't read docstrings before hitting the 400.
Tests
Added 10 new tests in tests/lib/tools/test_skills.py covering:
_parse_skill_name_from_frontmatterfound / missing- Bare paths prefixed from SKILL.md
name:field - Already-prefixed paths unchanged (idempotent)
normalize_skill_upload_pathsidempotency (apply twice → same result)display_titlefallback when SKILL.md has noname:field- Special character normalisation in
display_title - IO stream (
BytesIO) read + rewind - No SKILL.md + no
display_title→ passthrough unchanged - 2-tuple entries (filename, content) without MIME type
All 22 tests pass (uv run pytest tests/lib/tools/test_skills.py -v).
Note:
tests/api_resources/beta/test_webhooks.pyfails withModuleNotFoundError: No module named 'standardwebhooks'on the upstreammainbranch before any changes in this PR. Unrelated to this fix.
Adjacent gaps
- The
SKILL.mdfront-mattername:constraint on the server side is undocumented inSkills.create's docstring and in theSkillCreateParamstyped dict. A follow-up could add a one-line note to both. Left out of this PR to keep the diff focused on the behavioural fix. normalize_skill_upload_pathsis exported from__all__in_skills.pyso callers who build the file list outsideskills.createcan normalise manually if needed.
@avalyset good catch — you're right that the old regex matched name: anywhere, so a body occurrence (code example/prose) without a leading --- block would parse the wrong value and surface the same 400 from a different trigger.
Pushed a fix scoping the match to the leading --- ... --- front-matter block (BOM and CRLF tolerated), plus two regression tests: a body-only name: with no front-matter (now returns None) and a body decoy after a real front-matter block (returns the front-matter value). Thanks for the careful review.
This PR addresses #1575 but only mentions it — adding Closes #1575 to the description would auto-close and link the issue on merge.