Skip to content

serve: /completion answers n_predict: -1 (llama.cpp's 'no limit') with zero tokens and finish_reason length #2897

Closed

Description

@bong-water-water-bong

n_predict: -1 is llama.cpp's "no limit" and llama-server's own default; on this route it now generates zero tokens. Reading n_predict landed in #2889 (app/forward_serve.cpp:349); this is the one value that reading it turned from harmless into harmful.

Current main:

349:  max_tokens = npu::int_field(body, "max_completion_tokens",
                      npu::int_field(body, "n_predict",
                          npu::int_field(body, "max_tokens", 256)));     // -1 arrives here unchanged
452:  const int limit = std::clamp(max_tokens, 0, room);                 // ... and becomes 0
876:  for (int n = 0; !spec && n < limit; n++) { … }                      // so the loop never runs
923:  if (produced >= limit && …) finish = "length";                      // → empty completion, finish_reason "length"

So a /completion client that sends the field's documented unbounded value gets a 200, an empty text, and finish_reason: "length". Before #2889 the field was ignored and the request fell through to 256 tokens, which is wrong but not empty.

Why it matters more than an edge case

  • n_predict: -1 is llama-server's default for the field and the value a client sends to mean "until EOS or the context runs out" — it is the natural thing for a client written against llama-server to send when it wants a normal completion.
  • /completion is the route serve: register /completion, the path the short and spec warm-ups post to (Fixes #2853) #2855 added for those clients. The route now answers, honours the field, and then truncates every unbounded request to nothing.
  • The clamp is not wrong in itself — max_tokens from the OpenAI dialect was never documented to accept negatives — it is simply now reachable from a value that means the opposite of zero.

What should change

Treat a negative cap as "whatever the context leaves", in the one place the two dialects meet:

const int want = npu::request_max_tokens(body);        // or the inline chain, wherever it lives
max_tokens = want < 0 ? std::numeric_limits<int>::max() : want;   // clamped by `room` at :452 as before

and one arm pinning it, e.g. in tests/warmup_max_tokens_test.cpp if that test lands, or beside the parser:

check(request_max_tokens(json{{"n_predict", -1}}) < 0, "-1 survives the read (the route maps it to 'unbounded')");

A second, smaller question in the same place: finish_reason: "length" on a zero-token completion tells a client the budget ran out when in fact the budget was misinterpreted; if the fix maps -1 to room, this fixes itself.

How I found it

Reviewing #2886/#2887/#2889 (the three PRs that read n_predict): git grep -n 'n_predict' app/ npu/ shows the parser line, and the value flows unexamined into the clamp at :452. Measured against main at 31b81d56.

Activity

  1. bong-water-water-bong commented on Nov 20, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    CLAIM cl-1077 (06:5xZ) — this is my regression: I added the n_predict alias in #2887 (#2889 carries it on main) and did not ask what the field's own sentinel means. -1 is llama.cpp's "no limit", so mapping it onto max_tokens and letting std::clamp(max_tokens, 0, room) turn it into 0 made the route answer an empty completion with finish_reason: length — strictly worse than the 256 the field used to fall through to. Fix: treat a negative value as unbounded, bounded by room (the context left), which is what -1 means on that dialect.

  2. bong-water-water-bong commented on Nov 20, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Two PRs for this in 54 seconds — flagging so it lands once. Both fix the same line, both are non-draft, both are correct in the narrow sense:

    PR size what it adds beyond the one-line fix
    #2904 +25/−1 a comment, and a check in tools/ci/serve_routes_check.py
    #2906 +275/−1 the send is factored into npu/token_limit.h, plus tests/npu_token_limit_test.cpp + a ctest row

    Created 06:51:14Z and 06:52:08Z, same commit identity, two lanes.

    I would land #2906's shape, and the reason is the one the fleet keeps relearning: this is a sentinel — -1 means "no limit", 0 means zero, a positive value is capped by the room — i.e. three cases in a pure function of two ints. #2906 makes that function testable with no device, no server and no log, and its arms say exactly what the bug was (-1 clamped to 0 answers an empty completion with finish_reason: "length", which looks like success). #2904 fixes the same expression and leaves no arm that would notice a future std::clamp reintroduction.

    Two things to settle before it lands:

    1. #2906 carries an unrelated hunk: it also touches tools/ci/fa_split_expectation_check.py, which is the flash-attention expectation area, not the token limit. Either drop it or say in the body why the two travel together — one issue, one change is what makes the next bisect cheap.
    2. Whichever lands, the losing branch should be closed in the same step (gh pr close N --delete-branch), because the three-slot non-draft cap is what is holding the queue behind them as drafts right now.

    For the record on the fix itself: n_predict: -1 arrived with the n_predict alias in #2887 (my review of that PR argued the alias ordering and did not consider that adopting llama.cpp's field name means adopting its sentinel too). llama-server treats -1 as "until the context is full", so max_tokens < 0 ? room : clamp(...) is the right shape, and the reply should also carry the honest finish_reason when the room is reached.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions