OpenAIChatCompletionsModel sends max_tokens, which reasoning models reject

オープン 初心者向け
#5,455 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
85/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
ai, backend

調査の方向性

src/agents/models/openai_chatcompletions.py の _fetch_response 内の708行目付近を調べ、max_tokens が設定されている箇所を確認する。ChatCmplHelpers.is_openai を使用して公式APIに max_completion_tokens を送信するチェックを追加する。tests/models/test_openai_chatcompletions_stream.py 内の max_tokens == 64 アサーションを更新する。reproスクリプトが max_completion_tokens を出力し、テストスイートが通過したら完了。

索引モデルが issue の本文から書いたものです。

説明

Found while testing LLM provider integrations in agent frameworks: an automated tool flags candidates and each one is reproduced before it is reported. Reproduced on cd26bbb.

Please read this first
  • Have you read the docs? Yes, Models.
  • Have you searched for related issues? Yes. #679 proposed switching to max_completion_tokens for every endpoint and was closed as stale without review.
Describe the bug

OpenAIChatCompletionsModel sends ModelSettings.max_tokens as the Chat Completions max_tokens parameter, also when the client points at api.openai.com. OpenAI deprecated max_tokens in favor of max_completion_tokens, and its reasoning models (o-series, and the GPT-5 family the same way) reject max_tokens with a 400. So an agent that sets ModelSettings(max_tokens=...) and runs a reasoning model through Chat Completions (OpenAIChatCompletionsModel, set_default_openai_api("chat_completions") or OpenAIProvider(use_responses=False)) fails on its first model call.

The other paths already translate the setting: OpenAIResponsesModel sends it as max_output_tokens, and LiteLLM and Any-LLM remap it to max_completion_tokens themselves. In OpenAIChatCompletionsModel._fetch_response the key is fixed: https://github.com/openai/openai-agents-python/blob/cd26bbbc52847845fde73b9952b51e62e2bcbdc0/src/agents/models/openai_chatcompletions.py#L708

Debug information
  • Agents SDK version: 0.24.0 (main at cd26bbb)
  • Related library versions (optional, e.g. any-llm, litellm, or pydantic): openai 3.29.0
  • Python version: 3.12.4
  • Operating system: Linux
  • Model and model provider: OpenAI o4-mini through Chat Completions (also o3, the gpt-5 family and similar)
  • Does the issue reproduce with the latest Agents SDK release? Yes, the code path is unchanged on main.
  • Does the issue occur consistently or intermittently? Consistently.

Output of the script below:

https://api.openai.com/v1/chat/completions o4-mini {'max_tokens': 256}

The mock transport only records the request; I did not call the live API. The Chat Completions reference says max_tokens "is now deprecated in favor of max_completion_tokens, and is not compatible with o-series models".

Repro steps
import asyncio, json
try:
    import httpx2 as httpx  # openai>=3 ships its own httpx fork
except ImportError:
    import httpx
from openai import AsyncOpenAI
from agents import Agent, ModelSettings, OpenAIChatCompletionsModel, Runner, set_tracing_disabled

set_tracing_disabled(True)

def handler(request):
    body = json.loads(request.content)
    print(request.url, body["model"], {k: body[k] for k in ("max_tokens", "max_completion_tokens") if k in body})
    return httpx.Response(200, json={"id": "c1", "object": "chat.completion", "created": 0, "model": body["model"],
        "choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "Hello!"}}]})

client = AsyncOpenAI(api_key="sk-test", http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)))
agent = Agent(name="assistant", model=OpenAIChatCompletionsModel(model="o4-mini", openai_client=client),
              model_settings=ModelSettings(max_tokens=256))
asyncio.run(Runner.run(agent, "Say hello."))
Expected behavior

When the client targets the official endpoint, ModelSettings.max_tokens is sent as max_completion_tokens and the run works on reasoning models.

Something like this in _fetch_response would do it, keeping max_tokens for OpenAI-compatible servers that only know the older name and letting an explicit extra_args["max_completion_tokens"] win. The max_tokens == 64 assertion in tests/models/test_openai_chatcompletions_stream.py (official base URL, gpt-5.4-mini) would change with it.

+        if model_settings.max_tokens is not None and ChatCmplHelpers.is_openai(self._get_client()):
+            create_kwargs["max_tokens"] = omit
+            if "max_completion_tokens" not in (model_settings.extra_args or {}):
+                create_kwargs["max_completion_tokens"] = model_settings.max_tokens
主要言語
Python
スター
30.4k
フォーク
4.9k
平均マージ
19時間 5分
マージ済み PR(30日)
90

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openai/openai-agents-python のほかの issue

openai/openai-agents-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。