Skip to content

Fix ENAMETOOLONG crash when chat_template is an inline Jinja string - #698

Open
javierdejesusda wants to merge 1 commit into
arcee-ai:mainfrom
javierdejesusda:fix/629-chat-template-enametoolong
Open

Fix ENAMETOOLONG crash when chat_template is an inline Jinja string#698
javierdejesusda wants to merge 1 commit into
arcee-ai:mainfrom
javierdejesusda:fix/629-chat-template-enametoolong

Conversation

@javierdejesusda

@javierdejesusda javierdejesusda commented Jul 8, 2026

Copy link
Copy Markdown

Fixes #629.

When chat_template is set to an inline Jinja string rather than the name of a bundled template, the merge crashes before it can finish. _set_chat_template in mergekit/merge.py treats the value as a template name first, building a path with importlib.resources.files(chat_templates).joinpath(chat_template + ".jinja") and calling .is_file() on it. For a long inline template that path exceeds NAME_MAX, so .is_file() raises OSError (ENAMETOOLONG) and the whole merge aborts.

The fix only treats chat_template as a bundled-template name when it actually looks like one, guarding the .is_file() check with len(chat_template) < 256 and "\n" not in chat_template and "{" not in chat_template. Inline templates no longer hit the filesystem lookup and fall through to the existing literal-Jinja branch, which already handles them correctly.

Added test_template_long_literal_jinja in tests/test_chat_template.py, which merges with a >300-character inline Jinja template and asserts the merge succeeds and the template round-trips. It reproduces the ENAMETOOLONG failure on main and passes with this change.


Note

Low Risk
Small conditional change in tokenizer chat-template resolution with a targeted test; no auth, data, or merge-math impact.

Overview
Fixes merges that crash when chat_template is a long inline Jinja string instead of a bundled template name like chatml.

_set_chat_template in merge.py used to always probe the filesystem with chat_template + ".jinja". That path can exceed NAME_MAX for long literals, so .is_file() raised OSError (ENAMETOOLONG) and aborted the merge.

The bundled-template branch now runs only when the value looks like a name: length under 256, no newlines, and no {. Inline Jinja skips the lookup and uses the existing literal branch. A regression test merges with a 300+ character inline template and checks it round-trips.

Reviewed by Cursor Bugbot for commit 0102916. Bugbot is set up for automated code reviews on this repo. Configure here.

A long literal chat_template made the .jinja resource lookup build a
path whose final component exceeds NAME_MAX, so .is_file() raised
OSError(ENAMETOOLONG) instead of returning False. Only attempt the
template-name lookup when chat_template looks like a bare name, falling
through to treat the value as an inline template otherwise.
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@javierdejesusda

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Chat Template File name too long

1 participant