Hacktoberfest 2026:维护者为十月标记出来的 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 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
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 复现;非流式运行完成并针对缺失的 token 计数报告零值,即表示完成。

由索引模型根据 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.6k
派生
4.8k
平均合并
2 天 11 小时
30 天内合并 PR
203

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openai/openai-agents-python 的其他 Issue

查看 openai/openai-agents-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。