OpenAIChatCompletionsModel sends max_tokens, which reasoning models reject

Đang mở Phù hợp với người mới
#5,455 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
ai, backend

Hướng nghiên cứu

Kiểm tra src/agents/models/openai_chatcompletions.py xung quanh dòng 708 trong _fetch_response, nơi max_tokens được đặt. Thêm một kiểm tra sử dụng ChatCmplHelpers.is_openai để gửi max_completion_tokens cho API chính thức. Cập nhật xác nhận max_tokens == 64 trong tests/models/test_openai_chatcompletions_stream.py. Hoàn thành khi script tái hiện in ra max_completion_tokens và bộ test pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
Python
Star
30.4k
Fork
4.9k
Merge trung bình
16 giờ 32 phút
Pull request đã merge (30 ngày)
85

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của openai/openai-agents-python

Tất cả issue của openai/openai-agents-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.