openclaw - ✅(Solved) Fix [Bug]: OpenClaw sends deprecated max_tokens parameter to GPT-5.x models, causing persistent 400 errors [1 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#62130Fetched 2026-04-08 03:08:37
View on GitHub
Comments
2
Participants
2
Timeline
2
Reactions
0
Timeline (top)
commented ×2

OpenClaw 2026.4.5 sends the deprecated max_tokens parameter when making API calls to OpenAI GPT-5.x models (tested with gpt-5.2). OpenAI's GPT-5.x family requires max_completion_tokens instead. This causes every request to fail with a 400 error:

{
  "error": {
    "message": "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.",
    "type": "invalid_request_error",
    "param": "max_tokens",
    "code": "unsupported_parameter"
  }
}

Error Message

OpenClaw 2026.4.5 sends the deprecated max_tokens parameter when making API calls to OpenAI GPT-5.x models (tested with gpt-5.2). OpenAI's GPT-5.x family requires max_completion_tokens instead. This causes every request to fail with a 400 error: "error": { All requests to GPT-5.x models fail with 400 errors. OpenClaw surfaces this as a timeout or generic failure — no clear error message is shown to the user. Debugging required direct curl testing to isolate the root cause. Adding "compat": {"maxTokensField": "max_completion_tokens"} to the model definition in models.providers.openai.models[] (as referenced in #25794) was tested and does not work. OpenClaw 2026.4.5 ignores the compat.maxTokensField config entirely. GPT-5.x requests continue to fail with the same 404/400 error after applying this config and restarting the gateway.

Root Cause

Actual behavior

All requests to GPT-5.x models fail with 400 errors. OpenClaw surfaces this as a timeout or generic failure — no clear error message is shown to the user. Debugging required direct curl testing to isolate the root cause.

Fix Action

Fix / Workaround

Workaround attempted (FAILED)

Adding "compat": {"maxTokensField": "max_completion_tokens"} to the model definition in models.providers.openai.models[] (as referenced in #25794) was tested and does not work. OpenClaw 2026.4.5 ignores the compat.maxTokensField config entirely. GPT-5.x requests continue to fail with the same 404/400 error after applying this config and restarting the gateway.

PR fix notes

PR #66146: fix(agents): forward model maxTokens as default stream option

Description (problem / solution / changelog)

Summary

When options.maxTokens is not explicitly provided, the OpenAI-compatible transport functions (buildOpenAIResponsesParams and buildOpenAICompletionsParams) now fall back to model.maxTokens instead of omitting the max_tokens / max_completion_tokens / max_output_tokens field entirely.

Problem

Requests sent to OpenRouter (and other OpenAI-compatible providers) without an explicit max_tokens parameter hit provider-side defaults — often 8192 tokens for Anthropic models via OpenRouter. This caused:

  1. Silent output truncation on large generations (HTML documents, long markdown files, comprehensive tool-call arguments)
  2. Infinite retry loops when a write tool call exceeded 8192 output tokens — the content field was truncated away, OpenClaw rejected the tool call with must have required property 'content', and the model retried identically
  3. Sub-agent timeouts — agents gathered research successfully but timed out generating the deliverable because each attempt was truncated

Evidence from session 36425fd6: 9 consecutive write attempts, each hitting exactly output: 8192, stopReason: "length".

Fix

Both transport paths now resolve an effectiveMaxTokens from options?.maxTokens || model.maxTokens before building the API payload. The model object already carries the correct limit from the provider capability cache (e.g. 128K for Claude Opus 4.6 via OpenRouter's top_provider.max_completion_tokens).

This aligns the OpenAI-compatible transport with the existing Anthropic direct transport behavior at anthropic-transport-stream.ts:493, which already defaults to Math.min(model.maxTokens, 32_000).

Changes

  • src/agents/openai-transport-stream.tsbuildOpenAIResponsesParams: fall back to model.maxTokens for max_output_tokens
  • src/agents/openai-transport-stream.tsbuildOpenAICompletionsParams: fall back to model.maxTokens for max_tokens / max_completion_tokens (respecting compat.maxTokensField)

Test Plan

  • Existing 48 transport stream tests pass
  • Build passes
  • Gateway restart + manual verification on live OpenRouter traffic

Related Issues

  • Partially addresses #63210 (output truncation detection) — fixes the root cause of truncation for OpenRouter/OpenAI-compatible providers by sending the correct max_tokens
  • Related to #49173 and #62130 (max_tokens vs max_completion_tokens) — ensures the value is always sent using the correct field name
  • Reduces urgency of #44468 (per-model maxTokens config overrides) — the correct value now flows automatically from the provider capability cache

Changed files

  • src/agents/openai-transport-stream.ts (modified, +11/-7)
  • src/tui/tui-event-handlers.test.ts (modified, +187/-40)
  • src/tui/tui-event-handlers.ts (modified, +55/-30)

Code Example

{
  "error": {
    "message": "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.",
    "type": "invalid_request_error",
    "param": "max_tokens",
    "code": "unsupported_parameter"
  }
}

---

curl -s https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.2","messages":[{"role":"user","content":"say hello"}],"max_completion_tokens":10}'
# Returns 200 OK with valid response

---

curl -s https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.2","messages":[{"role":"user","content":"say hello"}],"max_tokens":10}'
# Returns 400: "Unsupported parameter: 'max_tokens' is not supported with this model."
RAW_BUFFERClick to expand / collapse

[Bug]: OpenClaw sends deprecated max_tokens parameter to GPT-5.x models, causing persistent 400 errors

Bug type

Behavior bug (incorrect output/state without crash)

Summary

OpenClaw 2026.4.5 sends the deprecated max_tokens parameter when making API calls to OpenAI GPT-5.x models (tested with gpt-5.2). OpenAI's GPT-5.x family requires max_completion_tokens instead. This causes every request to fail with a 400 error:

{
  "error": {
    "message": "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.",
    "type": "invalid_request_error",
    "param": "max_tokens",
    "code": "unsupported_parameter"
  }
}

Steps to reproduce

  1. Configure OpenAI provider with a valid API key and gpt-5.2 model in openclaw.json
  2. Set gpt-5.2 as default or switch to it via model override
  3. Send any message — request fails silently or times out
  4. Direct curl to OpenAI API with max_completion_tokens succeeds immediately:
curl -s https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.2","messages":[{"role":"user","content":"say hello"}],"max_completion_tokens":10}'
# Returns 200 OK with valid response
  1. Same request with max_tokens returns 400:
curl -s https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.2","messages":[{"role":"user","content":"say hello"}],"max_tokens":10}'
# Returns 400: "Unsupported parameter: 'max_tokens' is not supported with this model."

Expected behavior

OpenClaw should detect GPT-5.x models and use max_completion_tokens instead of max_tokens in the API request body. Alternatively, support the compat.maxTokensField model config option (referenced in issue #25794) to allow users to override this per-model.

Actual behavior

All requests to GPT-5.x models fail with 400 errors. OpenClaw surfaces this as a timeout or generic failure — no clear error message is shown to the user. Debugging required direct curl testing to isolate the root cause.

Environment

  • OpenClaw version: 2026.4.5 (3e72c03)
  • OS: macOS 26.3.1 (arm64)
  • Node: v22.22.1
  • Install method: pnpm (global)
  • Gateway: local, LaunchAgent
  • OpenAI provider: Direct API key auth, baseUrl https://api.openai.com/v1
  • Affected models: gpt-5.2, likely gpt-5.4 and all GPT-5.x family
  • Unaffected models: gpt-4.1, gpt-4.1-mini, all Anthropic models, all Google models, all Ollama models

Workaround attempted (FAILED)

Adding "compat": {"maxTokensField": "max_completion_tokens"} to the model definition in models.providers.openai.models[] (as referenced in #25794) was tested and does not work. OpenClaw 2026.4.5 ignores the compat.maxTokensField config entirely. GPT-5.x requests continue to fail with the same 404/400 error after applying this config and restarting the gateway.

Suggested fix

OpenClaw's OpenAI provider adapter should automatically use max_completion_tokens for models in the GPT-5.x family, matching OpenAI's current API requirements.

extent analysis

TL;DR

Update OpenClaw's OpenAI provider adapter to use max_completion_tokens instead of max_tokens for GPT-5.x models.

Guidance

  • Verify the OpenClaw version is 2026.4.5 and the issue is specific to GPT-5.x models.
  • Check the OpenAI API documentation to confirm the required parameter for GPT-5.x models is indeed max_completion_tokens.
  • Consider updating the OpenClaw configuration to use a custom adapter or patch the existing adapter to use the correct parameter for GPT-5.x models.
  • Test the fix by sending a request to the OpenAI API with the updated parameter using a tool like curl.

Example

// Example API request with correct parameter
{
  "model": "gpt-5.2",
  "messages": [{"role": "user", "content": "say hello"}],
  "max_completion_tokens": 10
}

Notes

The suggested fix assumes that updating the OpenClaw adapter to use max_completion_tokens will resolve the issue. However, the compat.maxTokensField config option is not working as expected in OpenClaw 2026.4.5, so a custom solution may be required.

Recommendation

Apply a workaround by patching the OpenClaw adapter to use max_completion_tokens for GPT-5.x models, as updating to a fixed version is not currently available.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

OpenClaw should detect GPT-5.x models and use max_completion_tokens instead of max_tokens in the API request body. Alternatively, support the compat.maxTokensField model config option (referenced in issue #25794) to allow users to override this per-model.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING