Non-streaming Chat Completions run fails when provider usage has null token counts (streaming handles it since #1179)
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
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:
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.
- Lingua principale
- Python
- Stelle
- 29.6k
- Fork
- 4.8k
- Merge medio
- 2g 7h
- PR unite (30g)
- 205
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-agents-python
-
feature:sessions
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
openai/openai-agents-python#4827 · 12 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-agents-python
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
I maintainer di solito rispondono entro 1 giorno
-
https://search.utilibre.orgApertainstance instance add
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
searxng/searx-instances#941 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
FluidNumerics/fluid-walk-blocker#89 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno