Conversation
Add a `truncation` field to /v1/chat/completions requests: - "off" (default): reject an over-length prompt with 400 (unchanged behavior) - "auto": drop oldest whole messages, keeping the system message and the newest turn, until the prompt fits maxContextLength - positive integer N: as auto, targeting a budget of min(N, maxContextLength) Truncation runs at the message boundary before the chat template is applied, so it needs no engine or KV-cache changes. A keep-last token trim is the final safety net so an opt-in request never errors. /v1/completions is unchanged. ContextTruncation.apply is a pure policy (render injected as a closure) with unit tests for the decode and every mode; the two chat paths share a resolvePromptTokens helper.
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.
Summary
Adds an opt-in
truncationfield to/v1/chat/completionsso an over-length conversation can be gracefully fit to the model's context window instead of being rejected outright. The default is unchanged (reject with400), so this is purely additive and eval-safe.API
One field, three modes:
truncation"off"(default, also"disabled"/"none")400— unchanged behavior"auto"maxContextLengthN(e.g.500)"auto", but target a budget ofmin(N, maxContextLength)tokensInvalid values (non-positive integer, unknown string) return
400.Design
ContextTruncation.applyis a pure policy — the prompt renderer is injected as a closure — so it is unit-tested in isolation with no tokenizer or server state.resolvePromptTokenshelper./v1/completions(the loglikelihood/eval path, which resets per prompt) is deliberately untouched — truncating there would corrupt scoring.This is the mechanical, lossless-at-the-boundary layer of context-window management. It mirrors the
truncate_prompt_tokensrequest extension found in other local serving engines and the stateless "truncate to fit" convention, without changing the default.Testing
ContextTruncationTests— every mode (off fits/overflows, auto no-op/drops-oldest,tokensAtbudget cap, safety-net trim).ServerAPITruncationTests— decode of the union field (absent → off, string forms, positive integer, rejection of non-positive/unknown).CoreAILMCommonTeststarget green (89 tests);swift build --product llm-serverclean;swift formatclean.