Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Non-streaming Chat Completions run fails when provider usage has null token counts (streaming handles it since #1179)

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

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

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

評価

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

調査の方向性

openai_chatcompletions.py の非ストリーミングの get_response 使用構築から始め、chatcmpl_stream_handler.py の null カウント処理と比較してください。any_llm_model.py の対応する Usage 構築を確認し、自己完結型の httpx.MockTransport 再現を実行してください。非ストリーミング実行が完了し、欠落しているトークン数についてゼロを報告すれば完了です。

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

説明

Describe the bug

With OpenAIChatCompletionsModel, a non-streaming run fails when the provider's usage has a null token count. This happens even though the model returned a valid answer. The streaming path already accepts the same payload: since #1179, chatcmpl_stream_handler.py maps None to 0 (usage.prompt_tokens or 0, …). The non-streaming get_response in openai_chatcompletions.py still passes response.usage.prompt_tokens / completion_tokens / total_tokens straight into Usage(...), so pydantic rejects None.

any_llm_model.py builds Usage the same way.

Debug information
  • Agents SDK version: main @ 8e435004 (0.22.3)
  • Python version: 3.13
  • Operating system: macOS
  • Model and model provider: OpenAIChatCompletionsModel against an OpenAI-compatible server (mocked below)
  • Does the issue reproduce with the latest Agents SDK release? Yes
  • Does the issue occur consistently or intermittently? Consistently, whenever a token count is null
pydantic_core._pydantic_core.ValidationError: 1 validation error for Usage
output_tokens
  Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
Repro steps

Self-contained; the HTTP call is mocked with httpx.MockTransport.

import asyncio
import httpx
from openai import AsyncOpenAI
from agents import Agent, Runner, OpenAIChatCompletionsModel, set_tracing_disabled

set_tracing_disabled(True)

def handler(request: httpx.Request) -> httpx.Response:
    return httpx.Response(200, json={
        "id": "r1", "object": "chat.completion", "created": 1, "model": "m",
        "choices": [{"index": 0, "finish_reason": "stop",
                     "message": {"role": "assistant", "content": "hi"}}],
        # Some OpenAI-compatible providers send null token counts
        "usage": {"prompt_tokens": 10, "completion_tokens": None, "total_tokens": 10},
    })

client = AsyncOpenAI(api_key="x", base_url="http://test/v1",
                     http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)))
agent = Agent(name="a", model=OpenAIChatCompletionsModel(model="m", openai_client=client))
print(asyncio.run(Runner.run(agent, "hi")).final_output)

The same payload streamed through Runner.run_streamed completes and reports input_tokens=10.

Expected behavior

The run should complete the same way as the streaming path: treat the missing counts as 0 (... or 0) in openai_chatcompletions.get_response and any_llm_model.

I used an AI assistant (Claude Code) to help find and reproduce this; I ran the repro myself on main.

主要言語
Python
スター
29.8k
フォーク
4.8k
平均マージ
2日 7時間
マージ済み PR(30日)
205

環境構築

はじめの一歩

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

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

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

似ている issue

Python の issue をもっと見る

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

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