Repository navigation
fix: chat_template_kwargs keeps nulls in lists in place and refuses reserved names - #273
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #264.
The three follow-ups from the review of #260, one by one.
1. A null inside a list keeps its place
sanitizeForJinjaran lists throughcompactMap, 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 asnone. swift-jinja can't convertNSNull, but it takes a plainnil, 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 itsnulloption too. Object entries with a null value are still dropped as before, since nothing else moves when a key is missing.2.
messages,toolsandadd_generation_promptare refusedKwargs 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_errorthat names the key, the same shape as theenable_thinkingone in 17b. The special tokens likebos_tokenare filled in after the kwargs, so they were never affected.3. Docs for a non-boolean
enable_thinkingI kept the validation as it is and fixed the wording instead. The README and
docs/api-response-formats.mdnow say that a non-booleanenable_thinkinginside 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."):messageswith a question about Francemessages: []add_generation_prompt: false<|im_start|>usertools: []["c", "f"]["c", null, "f"]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 seesnoneat the right index.ChatRequestParsingTests: 3 new ones for the reserved names.tests/test-contract.shblock 20: the three reserved names are a 400, and the enum null shows up inprompt_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).