Skip to content

fix: chat_template_kwargs keeps nulls in lists in place and refuses reserved names - #273

Merged
solderzzc merged 1 commit into
SharpAI:mainfrom
HamedShams:fix/template-kwargs-followups
Nov 26, 2026
Merged

solderzzc merged 1 commit into
SharpAI:mainfrom
HamedShams:fix/template-kwargs-followups

Conversation

@HamedShams

Copy link
Copy Markdown
Contributor

Closes #264.

The three follow-ups from the review of #260, one by one.

1. A null inside a list keeps its place

sanitizeForJinja ran lists through compactMap, so [1, null, 2] reached the template as [1, 2] and anything read by index was one off. Now the null stays where it is and the template reads it as none. swift-jinja can't convert NSNull, but it takes a plain nil, so that's what the element becomes.

I went with keeping the position rather than documenting the old behaviour, because dropping the element quietly changes what the client sent. Tool schemas go through the same function, so a nullable enum like ["c", null, "f"] now shows the model its null option too. Object entries with a null value are still dropped as before, since nothing else moves when a key is missing.

2. messages, tools and add_generation_prompt are refused

Kwargs are merged into the template context after these three, so a kwarg with one of those names replaced what the server had built for the request. That's now a 400 invalid_request_error that names the key, the same shape as the enable_thinking one in 17b. The special tokens like bos_token are filled in after the kwargs, so they were never affected.

3. Docs for a non-boolean enable_thinking

I kept the validation as it is and fixed the wording instead. The README and docs/api-response-formats.md now say that a non-boolean enable_thinking inside the kwargs is a 400 even when the top-level field is set. The same rows mention the reserved names and the null rule.

Same requests on main and on this branch (mlx-community/Qwen2.5-0.5B-Instruct-4bit, the request itself always says "Say hi."):

request main this PR
kwarg messages with a question about France 200, answers "Paris" 400
kwarg messages: [] 200, 24 prompt tokens instead of 32 400
kwarg add_generation_prompt: false 200, the reply starts with <|im_start|>user 400
one tool in the request + kwarg tools: [] 200, 32 prompt tokens, the tool is gone (158 with it) 400
tool enum ["c", "f"] 161 prompt tokens 161
tool enum ["c", null, "f"] 161, the null is dropped 163

Tests:

  • JinjaSanitizerTests: the old "drops null array elements" test now expects the null in place, plus 4 new ones. Two of them render through swift-jinja the same way swift-transformers does, to be sure a template really sees none at the right index.
  • ChatRequestParsingTests: 3 new ones for the reserved names.
  • tests/test-contract.sh block 20: the three reserved names are a 400, and the enum null shows up in prompt_tokens. All 4 checks fail on main.
  • swift test --filter SwiftLMTests: 376 tests, 0 failures. Contract suite against the CI model: 39 passed, 0 failed, 2 skipped (the two usual skips for the tiny model).

…eserved names

A null inside a list was dropped on the way to the chat template, so later elements moved up by one. It now stays in place and the template reads it as none. A kwarg named messages, tools or add_generation_prompt replaced the value the server had built, and is now a 400 that names the key. The docs say that a non-boolean enable_thinking kwarg is a 400 even when the top-level field is set.

Closes SharpAI#264.
@solderzzc
solderzzc merged commit a70bc45 into SharpAI:main Nov 26, 2026
27 of 30 checks passed
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.

chat_template_kwargs: null array elements dropped, reserved keys overwritten, top-level enable_thinking does not override a non-boolean kwarg

2 participants