Skip to content

fix(ai): send max_tokens on Model Studio compatible-mode endpoints - #168513

Closed
synthalorian wants to merge 1 commit into
openclaw:mainfrom
synthalorian:fix/issue-127119
Closed

synthalorian wants to merge 1 commit into
openclaw:mainfrom
synthalorian:fix/issue-127119

Conversation

@synthalorian

@synthalorian synthalorian commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Model Studio compatible-mode requests (qwen, qwencloud, modelstudio, dashscope, qwen-token-plan, and bailian-token-plan) send the output cap as max_completion_tokens. Alibaba's OpenAI-compatibility reference documents max_tokens for that surface and does not list max_completion_tokens, so the configured cap can be ignored or the request can be rejected.

Why This Change Was Made

resolveOpenAICompletionsCompatDefaults already treats this family as Model Studio for other flags, but usesMaxTokens omitted it and fell through to the OpenAI field. The fix adds that family only when the base URL is an Alibaba compatible-mode host (*.dashscope*.aliyuncs.com or *.maas.aliyuncs.com with a /compatible-mode/ path).

Coding Plan hosts stay on max_completion_tokens. That surface shares the modelstudio-native endpoint class, and the cited reference does not cover it. Explicit model.compat.maxTokensField still wins. OpenAI and the other vendors that already select max_tokens or max_completion_tokens are unchanged. Unrelated /compatible-mode/ proxy URLs are unchanged.

User Impact

Compatible-mode Model Studio calls now send the configured output budget as max_tokens. A budget stored as params.max_completion_tokens is still honored; only the wire field changes. Coding Plan, OpenAI, and other providers keep their current field. No config migration is required.

Evidence

On head ecdd7b81ce7a9740b097340d0b336fb64134040c (rebased onto current upstream/main):

node scripts/run-vitest.mjs run packages/ai/src/transports/openai-completions-params.test.ts packages/ai/src/providers/openai-completions.compat.test.ts packages/ai/src/transports/openai-completions-transport.test.ts --maxWorkers=1
Test Files  3 passed (3)
Tests  81 passed (81)

Live transport proof did not call Model Studio, OpenAI, or Coding Plan. A local HTTP server on 127.0.0.1 received the raw POST body from streamOpenAICompletionsRequest in managed mode. Host classification came from resolveProviderRequestCapabilities (plugin metadata). The fetch hook refused every hostname except the three proof hosts and forwarded only the request body to that local server. No provider key was read or sent.

  1. Model Studio compatible-mode (qwen, https://dashscope.aliyuncs.com/compatible-mode/v1, classified modelstudio-native / modelstudio) sent max_tokens and not max_completion_tokens.
  2. OpenAI (openai, https://api.openai.com/v1, classified openai-public / openai-family) sent max_completion_tokens.
  3. Coding Plan (qwen, https://coding.dashscope.aliyuncs.com/v1, classified modelstudio-native / modelstudio) sent max_completion_tokens.

The redacted bodies are in the PR comment on this head. Vendor acceptance of the field was not observed. The field choice follows https://www.alibabacloud.com/help/en/model-studio/compatibility-of-openai-with-dashscope

Fixes #127119

@clawsweeper

clawsweeper Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@synthalorian

Copy link
Copy Markdown
Contributor Author

Proof for head 49c97ff399e944c1613bf75b7659e03ad8fa0bdd.

Before this commit, the new "Model Studio compatible-mode" cases failed: detectOpenAICompletionsCompat returned maxTokensField: "max_completion_tokens", and buildOpenAICompletionsParams did not set max_tokens. Coding Plan, OpenAI, Moonshot, explicit compat.maxTokensField, and non-Alibaba /compatible-mode/ URLs already passed.

After the fix:

node scripts/run-vitest.mjs run packages/ai/src/transports/openai-completions-params.test.ts packages/ai/src/providers/openai-completions.compat.test.ts --maxWorkers=1
Test Files  2 passed (2)
Tests  71 passed (71)

node scripts/run-vitest.mjs run extensions/qwen/index.test.ts --maxWorkers=1
Test Files  1 passed (1)
Tests  19 passed (19)

oxlint 0 errors on the two changed files. oxfmt --check clean. git diff --check clean.

Request-builder proof only. No Model Studio key was used, so live vendor handling of max_tokens vs max_completion_tokens was not observed.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 10, 2026
@clawsweeper

clawsweeper Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

The PR sends Model Studio compatible-mode output budgets as max_tokens while preserving Coding Plan behavior and explicit overrides.

Example: A qwen3.5-plus request to https://dashscope.aliyuncs.com/compatible-mode/v1 has an output budget of 1234 tokens.

  • Before: The request contains max_completion_tokens: 1234.
  • After: The request contains max_tokens: 1234.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The patch is focused and has no concrete correctness finding; partial transport evidence and unresolved product direction limit readiness.
Proof confidence 🦪 silver shellfish (2/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Needs a maintainer decision · Fix scope: Complete
User problem: Configured output limits are sent under a field Model Studio does not document, potentially leaving the budget ineffective or causing rejection.
Reason: The correction has clear contract value but changes an explicitly retained area-owner default.

Merge readiness

⛔ Blocked before merge - 2 items remain

This PR addresses a current provider-contract mismatch with a focused shared-owner fix; no concrete introduced bug was found.

Priority: P2
Reviewed head: ecdd7b81ce7a9740b097340d0b336fb64134040c
Owner decision: Required. See Decision needed.

Decision needed

  • Question: Should Alibaba compatible-mode routes now default to max_tokens, replacing the Model Studio default deliberately retained during compatibility consolidation?
  • Recommendation: Approve the scoped correction: Adopt the documented compatible-mode field while retaining Coding Plan behavior and explicit model overrides.
  • Why: The vendor reference supports this correction, but no owner approval of this default change appears in the discussion.

Before merge

  • Add real behavior proof - The current-head local HTTP capture exercises the real managed completions transport and verifies serialized field selection with OpenAI and Coding Plan controls. It does not exercise the shipped OpenClaw entry point against Model Studio or demonstrate vendor budget enforcement, fresh setup, or an upgrade from v2026.10.2. Provide redacted terminal output or request/response logs covering those scenarios; redact API keys, IPs, phone numbers, and non-public endpoints. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.

Findings

None.

Tests

  • Low-value test packages/ai/src/transports/openai-completions-params.test.ts: Remove the repeated qwen/modelstudio/dashscope detector-only cases already covered through the request builder; retain distinct alias coverage and the Coding Plan control.
  • Missing end-to-end proof: Supply a shipped OpenClaw-to-Model-Studio capped completion, including fresh setup and a latest-stable upgrade preserving stored budgets and explicit overrides.
Agent review details

How this fits together

The shared completions compatibility resolver combines provider metadata, endpoint identity, and explicit model overrides; both direct and managed transports consume its selected request field.

flowchart LR
  A[Model settings] --> C[Shared compatibility resolver]
  B[Provider metadata] --> C
  C --> D[Request builder]
  D --> E[Direct or managed transport]
  E --> F[Model Studio endpoint]
Loading

Technical review

Best possible solution:

Use the documented field through the existing shared resolver, with Coding Plan and explicit overrides preserved.

Do we have a high-confidence way to reproduce the issue?

Current-main source deterministically selects the undocumented field for the reported routes, matching the reporter's resolver output; vendor-side failure remains unobserved.

Is this the best way to solve the issue?

The scoped change uses the existing owner shared by both transport modes and avoids a broader plugin API expansion.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 2dd42a977aa9.

Provenance checked

  • Model Studio output-token field changes intended behavior with a stated reason (1759f4d: The compatibility consolidation explicitly retained Model Studio's canonical max_completion_tokens field; it recorded no vendor-acceptance rationale.)
  • Existing budget test expectations keeps the original intent (b2bdae9: The tests were consolidated during the low-value-test campaign and protect budget precedence and context clamping.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 1759f4d adds packages/ai/src/transports/openai-completions-compat.ts:121 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 1759f4d5e656; files: packages/ai/src/transports/openai-completions-compat.ts)
  • Vincent Koc: Raw commit ff68fd3 adds src/agents/openai-completions-compat.ts:73 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: ff68fd306013; files: src/agents/openai-completions-compat.ts)

PR surface

Source +26, Tests +109. Total +135 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 26 0 +26
Tests 1 112 3 +109
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 138 3 +135

Review metrics

Metric Value Why it matters
Production versus test delta Production +26/-0; tests +112/-3 Production growth scopes the endpoint contract; the test growth includes some repeated resolver-level coverage.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #127119
Summary: This PR implements the canonical field correction; the earlier implementation closed without merging.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • remove rating: 🧂 unranked krab: Current PR rating is rating: 🦪 silver shellfish, so this older rating label is no longer current.

Label justifications:

  • P2: A bounded provider request-contract mismatch remains on current main; actual vendor rejection or ignored limits have not been observed.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-10T16:13:56.886Z sha 49c97ff399e944c1613bf75b7659e03ad8fa0bdd :: needs real behavior proof before merge. :: none

Reviewed October 12, 2026, 9:19 PM ET / October 13, 2026, 01:19 UTC (Revision 2).

@synthalorian

Copy link
Copy Markdown
Contributor Author

Live request proof for head ecdd7b81ce7a9740b097340d0b336fb64134040c.

No Model Studio, OpenAI, or Coding Plan endpoint was contacted, and no provider API key was read or printed. streamOpenAICompletionsRequest ran in managed mode with maxTokens: 1234. Host classification came from resolveProviderRequestCapabilities (loaded plugin metadata). A local HTTP server on 127.0.0.1 captured the raw POST body. The transport fetch hook refused any hostname other than the three below and forwarded the body only to that local server.

Commands:

git fetch upstream main
git rebase upstream/main
node scripts/run-vitest.mjs run packages/ai/src/transports/openai-completions-params.test.ts packages/ai/src/providers/openai-completions.compat.test.ts packages/ai/src/transports/openai-completions-transport.test.ts --maxWorkers=1

That Vitest run: 3 files, 81 tests, all passed.

The local capture was a one-shot tsx script that called the transport three times. Captured bodies:

  1. Model Studio compatible-mode. Provider qwen, base URL https://dashscope.aliyuncs.com/compatible-mode/v1, classified modelstudio-native / modelstudio. Local path POST /compatible-mode/v1/chat/completions. Contains max_tokens and not max_completion_tokens.
{
  "model": "qwen3.5-plus",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "ping",
          "cache_control": { "type": "ephemeral" }
        }
      ]
    }
  ],
  "stream": true,
  "stream_options": { "include_usage": true },
  "max_tokens": 1234
}
  1. OpenAI. Provider openai, base URL https://api.openai.com/v1, classified openai-public / openai-family. Local path POST /v1/chat/completions. Contains max_completion_tokens and not max_tokens.
{
  "model": "gpt-5.4",
  "messages": [
    {
      "role": "user",
      "content": "ping"
    }
  ],
  "stream": true,
  "stream_options": { "include_usage": true },
  "store": false,
  "max_completion_tokens": 1234
}
  1. Coding Plan. Provider qwen, base URL https://coding.dashscope.aliyuncs.com/v1, classified modelstudio-native / modelstudio. Local path POST /v1/chat/completions. Contains max_completion_tokens and not max_tokens.
{
  "model": "qwen3.5-plus",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "ping",
          "cache_control": { "type": "ephemeral" }
        }
      ]
    }
  ],
  "stream": true,
  "stream_options": { "include_usage": true },
  "max_completion_tokens": 1234
}

Explicit compat.maxTokensField is unchanged and still wins. This does not show vendor enforcement, only the request body the completions transport sends.

@synthalorian

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Oct 13, 2026
Model Studio documents max_tokens for /compatible-mode/v1 and does not
document max_completion_tokens. The shared completions resolver already
recognizes the family, but usesMaxTokens omitted it, so compatible-mode
requests wrote the cap on the undocumented field.

Limit the switch to Alibaba compatible-mode hosts. Coding Plan keeps
max_completion_tokens, explicit compat.maxTokensField overrides still win,
and OpenAI plus other vendors stay unchanged.
@roboclaw-bot

Copy link
Copy Markdown
Collaborator

Thanks @synthalorian for identifying this and contributing the fix! Landed as credited replacement #189870 (2c2bf4b), using the existing compatibility flag with a regression proven red on main and green after. Coding Plan and explicit overrides remain unchanged.

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

Labels

P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Model Studio (Qwen/DashScope) requests send max_completion_tokens, but the vendor documents max_tokens

2 participants