Nullable token counts still raise on three usage paths #5238 did not cover

Author: Denimworld12Created Sep 30, 2026Updated Sep 30, 2026

Please read this first

  • Have you read the docs? Yes.
  • Have you searched for related issues? Yes — this is the same defect as #5238, in three paths that fix did not reach.

Describe the bug

#5238 normalized nullable token counts for Chat Completions. Three sibling paths that build Usage from a provider's usage object were not covered and still raise on the same input:

path result
usage.py _response_usage_to_usage — the shared Responses converter, used by the OpenAI Responses model, the any-LLM streaming path and compaction ValidationError
extensions/models/any_llm_model.py:426 — the responses branch of the same file #5238 fixed ValidationError
extensions/models/litellm_model.py:277 — the same prompt_tokens/completion_tokens shape as the fixed sites ValidationError
models/openai_chatcompletions.py:291 — fixed by #5238 0

As #5238's own test notes, "provider SDK parsing can construct typed responses without validating null counts" — model_construct bypasses validation, so None reaches a field declared int.

The failure happens after the provider returned a usable answer: the run raises while recording usage, so a completed response is lost. The missing usage object is already handled (else Usage(requests=1)); a present object carrying nulls is not.

litellm_model.py is the widest provider surface of the three, and it already writes or 0 for the details fields — only the top-level counts were left.

Reproduction

python
from openai.types.responses.response_usage import ResponseUsage
from openai.types.completion_usage import CompletionUsage
from agents.usage import _response_usage_to_usage, Usage

ru = ResponseUsage.model_construct(
    input_tokens=None, output_tokens=5, total_tokens=12,
    input_tokens_details=None, output_tokens_details=None,
)
cu = CompletionUsage.model_construct(prompt_tokens=None, completion_tokens=5, total_tokens=12)

_response_usage_to_usage(ru)                                    # ValidationError
Usage(requests=1, input_tokens=ru.input_tokens,                 # any_llm responses branch
      output_tokens=ru.output_tokens, total_tokens=ru.total_tokens)
Usage(requests=1, input_tokens=cu.prompt_tokens,                # litellm branch
      output_tokens=cu.completion_tokens, total_tokens=cu.total_tokens)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Usage
input_tokens
  Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]

The same construction with or 0, as at the sites #5238 fixed, returns 0.

Debug information

  • Agents SDK version: 0.22.3 (reproduces on main at c513dd1)
  • Related library versions: litellm 1.83.0, pydantic 2.13.5, openai 3.22.1
  • Python version: 3.12.13
  • Operating system: macOS (Darwin 25.5.0)
  • Model and model provider: any provider whose SDK builds usage without validation; reproduced directly against the conversion functions
  • Does the issue reproduce with the latest Agents SDK release? Yes
  • Does the issue occur consistently or intermittently? Consistently, whenever a provider reports a null count

Fix

A branch with the change and tests is here, if it is useful to cherry-pick:

https://github.com/Denimworld12/openai-agents-python/tree/fix/nullable-usage-remaining-paths

It reads the counts the way the fixed sites do — three lines per field — and adds coverage mirroring #5238's parametrization: the shared converter in tests/test_usage.py, the responses branch in tests/models/test_any_llm_model.py (the existing test runs supports_responses=False, so that branch was unreached), and a null-counts case in tests/models/test_litellm_usage_requests.py beside the existing "no usage at all" test. With the source reverted, 9 of the new cases fail.

Verification run locally: 958 passed, 16 skipped across tests/test_usage.py and tests/models/, ruff format/ruff check clean on the changed files, mypy clean, and check_optional_truthiness.py clean (0 or 0 is 0, so the guard is value-preserving here as at the #5238 sites).

I could not open this as a pull request — the repository limits that to collaborators — so I am raising it here instead.

Source: openai/openai-agents-python