Non-streaming Chat Completions run fails when provider usage has null token counts (streaming handles it since #1179)
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 84/100
Línea de trabajo
Comienza en openai_chatcompletions.py, en la construcción del uso de get_response sin streaming, y compárala después con el manejo de conteos nulos en chatcmpl_stream_handler.py. Revisa la construcción correspondiente de Usage en any_llm_model.py y ejecuta la reproducción autocontenida con httpx.MockTransport; se considera terminado cuando la ejecución sin streaming finaliza e informa de cero para los conteos de tokens que faltan.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 29.8k
- Forks
- 4.8k
- Merge medio
- 2 d 7 h
- PR fusionados (30 d)
- 205
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de openai/openai-agents-python
-
feature:sessions
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
openai/openai-agents-python#4827 · 12 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de openai/openai-agents-python
Issues similares
-
Claiming namespace `apoint`Abiertonamespace operations
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
EclipseFdn/open-vsx.org#13573 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
collective/icalendar#1854 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
rancher/rancher-ai-agent#412 ·
Los mantenedores suelen responder en 6 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
TUDelftGeodesy/DePSI#134 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
HenriquesLab/rxiv-maker#335 ·