cloudflare / cloudflare/ai

Unified provider sends `max_tokens` to OpenAI reasoning models

Open
#653 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.2k
Forks
345
Avg merge
13h 31m
Merged PRs (30d)
1

Description

`ai-gateway-provider`’s Unified route currently creates its models via `@ai-sdk/openai-compatible`:

```ts
const unified = createUnified();
const model = aigateway(unified('openai/o1-mini'));
```

When used with AI SDK’s standardized `maxOutputTokens`, the outgoing Chat Completions request contains:

```json
{
"model": "openai/o1-mini",
"max_tokens": 1000
}
```

OpenAI reasoning models reject this field and require `max_completion_tokens` instead:

```text
Unsupported parameter: 'max_tokens' is not supported with this model.
Use 'max_completion_tokens' instead.
```

The native Cloudflare OpenAI adapter does not have the issue:

```ts
const openai = createOpenAI();
const model = aigateway(openai.chat('o1-mini'));
```

That route uses `@ai-sdk/openai`, which performs the correct OpenAI-specific parameter mapping.

Suggested fix: have `createUnified()` supply `transformRequestBody` to its `createOpenAICompatible` instance. When Cloudflare’s model-capability metadata identifies an OpenAI reasoning model, move `max_tokens` to `max_completion_tokens`.

Contributor guide

Open the contributing guide

Research direction

Start at createUnified() and its createOpenAICompatible instance, then trace how transformRequestBody receives Cloudflare’s model-capability metadata and maps standardized maxOutputTokens. Reproduce the openai/o1-mini request and verify that reasoning models send max_completion_tokens while other models retain their existing parameter behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
ai, api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.