flm serve OpenAI-compatible endpoint hardcodes max_tokens default = 4096 → long responses are silently truncated (finish_reason: "length") when the client omits max_tokens

未关闭
#749 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp
领域
api, backend

调研方向

Start in src/server/rest_handler.cpp and inspect the three request sites that use a 4096 default for max_tokens. Reproduce the omitted-parameter request with the supplied curl example, compare it with max_tokens=12000, and verify that omitted output limits no longer truncate responses that would otherwise stop naturally.

由索引模型根据 Issue 内容生成。

描述

Title

flm serve OpenAI-compatible endpoint hardcodes max_tokens default = 4096 → long responses are silently truncated (finish_reason: "length") when the client omits max_tokens

Environment
  • FLM version: 1.0.6 (Linux package fastflowlm 1.0.6-1, Arch Linux, x86_64)
  • Hardware: AMD Ryzen™ AI NPU (XDNA2, NPU2 kernels)
  • Model: qwen3.5:9b (Qwen3.5-9B-NPU2), server started with --ctx-len 64000 --pmode turbo
  • Client: OpenAI-compatible (opencode 2.0.12) + direct curl
Summary

The /v1/chat/completions handler falls back to a hardcoded max_tokens = 4096 when the request body does not include max_tokens/max_completion_tokens:

request.value("max_tokens", 4096)   // rest_handler.cpp, three request sites (~lines 655 / 1106 / 1420 in 1.0.6)

Several common OpenAI-compatible clients never send max_tokens (verified: opencode 2.0.12 sends only model/store/stream/stream_options). For those clients this 4096 cap becomes an invisible hard ceiling: any generation longer than 4096 tokens is cut mid-sentence with finish_reason: "length", even when the model would have stopped naturally. This truncates long outputs (webpages, code files) with no way for the client to detect the cause other than parsing finish_reason.

Reproduction
  1. Start: flm serve qwen3.5:9b --ctx-len 64000 -p 52626
  2. POST without max_tokens:
curl -s http://127.0.0.1:52626/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"qwen3.5:9b","messages":[{"role":"user","content":"Build a complete single-file HTML page with embedded JS (form, validation, dark mode)..."}]}'

Observed: "finish_reason":"length", "completion_tokens":4096, output cut mid-JS (at <section id="contact), never reaching </html>.

  1. Same message WITH "max_tokens":12000: "finish_reason":"stop", completed </html>, "completion_tokens":3772.
Evidence (same model, same server)
Request max_tokens finish_reason completion_tokens Result
A (absent) length 4096 Truncated mid-JS
B 12000 stop 3772 Completed </html>
C 12000 (+ think:true, reasoning_effort:"medium") stop 4940 Completed

Decode speed ≈ 9.2 tok/s → each truncated run wastes ~7 minutes of NPU time while producing a broken response.

Expected behavior

Following the OpenAI convention (and Ollama's), when the client omits the output budget, generation should be bounded only by the context window — not by a fixed 4096 ceiling. If a default is kept, it should be derived from the active context, e.g. min(16384, MAX_L - prompt_tokens), and ideally configurable (--max-tokens env/flag, or per-model default from model_list.json's default_context_length).

Related observations (same session, feel free to split)
  1. Default sampler for Qwen3.5 in 1.0.6: presence_penalty 1.5, frequency_penalty 1.0, temperature 0.7, top_p 0.8. The aggressive penalties visibly degrade long code generation; opencode also does not forward client-provided sampling options, so these defaults apply in practice.
  2. With "think": true + "reasoning_effort", FLM produced ~4.4K chars of reasoning but never emitted the response marker: reasoning was appended to content while reasoning_content stayed empty.
Suggested fix

In src/server/rest_handler.cpp, replace the three hardcoded defaults:

// before
request.value("max_tokens", 4096)

// after
request.value("max_tokens", std::min(16384, max_len - prompt_len))

Happy to open a PR if this behavior is confirmed as unintentional.

主要语言
C++
星标
2.1k
派生
184
平均合并
12 小时 22 分钟
30 天内合并 PR
239

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ROCm/FastFlowLM 的其他 Issue

查看 ROCm/FastFlowLM 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。