Non-streaming Chat Completions run fails when provider usage has null token counts (streaming handles it since #1179)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 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:
OpenAIChatCompletionsModelagainst 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
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openai/openai-agents-python 的其他 Issue
-
feature:sessions
难度 4/5 3-5 天 新手友好度 35/100
openai/openai-agents-python#4827 · 12 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-agents-python 的全部 Issue
相似的 Issue
-
ACK_WAITING HELP_WANTED UPDATE_CS
难度 2/5 1-3 小时 新手友好度 78/100
OWASP/CheatSheetSeries#2458 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 90/100
BasedHardware/omi#19711 ·
维护者通常 1 天内回复
-
Qwen3_5MoeModel no longer returns router_logits, breaking aux loss with output_router_logits=True未关闭
难度 2/5 1-3 小时 新手友好度 78/100
huggingface/transformers#49172 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
vllm-project/vllm-metal#885 ·
维护者通常 1 天内回复