#1697·llm

`--no-stream` crashes with AttributeError when an OpenAI-compatible endpoint omits `usage`

Author: tao-hpuCreated Sep 21, 2026Updated Sep 21, 2026

A non-streaming Chat Completions response without a usage key, or with "usage": null, makes llm exit with a traceback. This happens with OpenAI-compatible servers configured through extra-openai-models.yaml or llm openai-endpoint, for both the sync and the async model.

Streaming prompts against the same server work, because the streaming branches check if chunk.usage: first.

To reproduce

The script starts a local server that returns one chat completion without usage, then prompts it with stream=False. It needs no API key.

python
"""Serve one OpenAI-compatible chat completion without a "usage" key, then prompt it."""

import json
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer

from llm.default_plugins.openai_models import Chat


class Handler(BaseHTTPRequestHandler):
    def do_POST(self):
        self.rfile.read(int(self.headers["Content-Length"]))
        body = json.dumps(
            {
                "id": "chatcmpl-1",
                "object": "chat.completion",
                "created": 1,
                "model": "local-model",
                "choices": [
                    {
                        "index": 0,
                        "message": {"role": "assistant", "content": "hello"},
                        "finish_reason": "stop",
                    }
                ],
            }
        ).encode()
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def log_message(self, *args):
        pass


server = HTTPServer(("127.0.0.1", 0), Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()

model = Chat(
    "local-model",
    model_name="local-model",
    api_base=f"http://127.0.0.1:{server.server_port}/v1",
)
response = model.prompt("hi", stream=False, key="x")
print("text:", response.text())
print("usage:", response.usage())

On llm==0.35 from PyPI (Python 3.13.5):

  File ".../llm/default_plugins/openai_models.py", line 1489, in execute
    usage = completion.usage.model_dump()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'model_dump'

The same happens from the CLI with llm -m <model> --no-stream hi, and with --async.

Expected

text: hello
usage: Usage(input=None, output=None, details=None)

Cause

Chat.execute() (llm/default_plugins/openai_models.py:1491 on main at 508b4f8) and AsyncChat.execute() (:1608) call completion.usage.model_dump() without checking completion.usage. usage is already initialised to None at the top of both methods, and set_usage() returns early when it is empty, so a guard like the one in the streaming branches (:1442, :1559) is enough.

The test fixtures always include a usage object, so the suite does not reach this path.