Commit 285808b
authored
fix(proxy/openai): translate max_tokens -> max_completion_tokens on chat path (#1774)
## Description
GPT-5 / o-series chat models reject the legacy `max_tokens` —
`AI_APICallError: Unsupported parameter: 'max_tokens' is not supported
with this model. Use 'max_completion_tokens' instead.` — while
gpt-4o/4.1 accept `max_completion_tokens` too. openai-compatible clients
(opencode via `@ai-sdk/openai-compatible`, older SDKs) still send
`max_tokens`, so requests for GPT-5 models fail at the proxy's OpenAI
upstream. This is a blocker for any such client pointed at a GPT-5 model
through Headroom.
The proxy already owns the outbound `/v1/chat/completions` body (it
rewrites `messages` to compress them), so translate the token param
there: rename `max_tokens` → `max_completion_tokens` when the newer form
isn't already set, then drop the rejected legacy key. One-way, safe for
current OpenAI models; no-op when the client already sends
`max_completion_tokens`. The Responses path (`max_output_tokens`) is
unaffected.
Closes #
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
## Changes Made
- New `_normalize_openai_max_tokens(body)` helper + call in
`handle_openai_chat` after body finalization, before upstream forward.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added
### Test Output
```text
tests/test_openai_max_completion_tokens.py .... 6 passed
ruff check ... All checks passed!
mypy headroom/proxy/handlers/openai.py ... Success: no issues found
```
## Real Behavior Proof
- Environment: local worktree, Python 3.12.
- Exact command / steps: reproduced live — opencode
(`@ai-sdk/openai-compatible` → Headroom proxy) targeting
`gpt-5.3-chat-latest` failed with `Unsupported parameter: 'max_tokens'
... Use 'max_completion_tokens'` in the DEBUG stream log. The shim
renames the param on the outbound body.
- Observed result: unit tests confirm the rename/drop/no-op cases.
- Not tested: full live opencode completion (its headless `run` stalls
for unrelated reasons in this env — separate from this param fix).
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Additional Notes
Discovered while debugging why opencode wouldn't run through the proxy:
three layered blockers — (1) missing `models` map in the injected
provider config [PR #1716], (2) no `apiKey` in the injected config /
HTTP path doesn't inject `OPENAI_API_KEY` like the WS path does, (3)
this `max_tokens` vs `max_completion_tokens` mismatch. This PR addresses
(3).1 parent 37a12dd commit 285808b
2 files changed
Lines changed: 78 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
87 | 105 | | |
88 | 106 | | |
89 | 107 | | |
| |||
2593 | 2611 | | |
2594 | 2612 | | |
2595 | 2613 | | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
| 2617 | + | |
| 2618 | + | |
| 2619 | + | |
| 2620 | + | |
| 2621 | + | |
| 2622 | + | |
2596 | 2623 | | |
2597 | 2624 | | |
2598 | 2625 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
0 commit comments