Skip to content

npu: three fields answer 500 with an empty body instead of a 400, and a wrong-typed tools is accepted in the default mode #1106

Closed

Description

@bong-water-water-bong

Three malformed-request shapes on /v1/chat/completions answer HTTP 500 with an empty body, and one answers
200 with the field silently dropped — the route's own rule is "a field the route cannot honour is a 400 naming it,
never a silently ignored key, never a 500" (docs/npu.md, and the logprobs/top_logprobs refusals already do this).

Measured on ad47253 (main), Qwen3-0.6B, NPU_INFER_HOST_GEMM=1 (no device), one serve, max_tokens 2 unless the
field under test is max_tokens itself:

request today why
{"max_tokens": "8"} 500, empty body body.value("max_tokens", 256) at app/forward_serve.cpp:186 runs before the try at :191, so nlohmann::json::type_error escapes the guard and httplib answers 500 with no message
{"max_completion_tokens": "8"} 500, empty body same site
{"stream": "yes"} 500, empty body body.value("stream", false) at :213, same reason
{"tools": "a string"} (no tool_choice, i.e. the default auto) 200, no constraint installed app/npu_tool_grammar.h:104 starts `if (!tools.is_array()

For contrast, fields parsed inside the guard are already right: logprobs: "yes" → 400 logprobs: must be true or false; top_logprobs: "3" → 400 top_logprobs: must be an integer; stop: 5 → 400; tool_choice: 7 → 400.

Why it matters

A 500 with an empty body is indistinguishable from a server fault to a client like Lemonade's backend watchdog, and it
is the exact shape that makes an operator restart a healthy backend; a 200 for tools with a wrong type is worse — the
answer is unconstrained and the caller believes it asked for tool calls.

Fix

  1. app/forward_serve.cpp: parse those two fields inside the guard (new npu::int_field/npu::bool_field in
    app/npu_sampling.h, unit-testable without a server), so a wrong type is a 400 naming the field.
  2. app/npu_tool_grammar.h: split the short-circuit — !tools.is_array() throws tools: must be an array; only an
    empty array means "no tools".
  3. tests/npu_sampling_test.cpp: assertions for both helpers and for tools of the wrong type in the auto mode
    (today: no throw).
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