Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Chiusa Adatta ai principianti
#5,229 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
api

Direzione di ricerca

Inizia in openai_chatcompletions.py, nella costruzione dell'utilizzo di get_response non-streaming, quindi confrontala con la gestione dei conteggi nulli in chatcmpl_stream_handler.py. Esamina la costruzione corrispondente di Usage in any_llm_model.py ed esegui la riproduzione autonoma con httpx.MockTransport; il lavoro è completato quando l'esecuzione non-streaming termina e segnala zero per i conteggi dei token mancanti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
29.6k
Fork
4.8k
Merge medio
2g 7h
PR unite (30g)
205

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openai/openai-agents-python

Tutte le issue di openai/openai-agents-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.