Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open Beginner friendly
#5,229 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api

Research direction

Start in openai_chatcompletions.py at the non-streaming get_response usage construction, then compare it with the null-count handling in chatcmpl_stream_handler.py. Review the corresponding Usage construction in any_llm_model.py and run the self-contained httpx.MockTransport reproduction; done means the non-streaming run completes and reports zero for missing token counts.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
29.6k
Forks
4.8k
Avg merge
2d 11h
Merged PRs (30d)
203

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openai/openai-agents-python

All issues in openai/openai-agents-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.