Skip to content

[Bug]: OpenAI-compatible backends crash on null token counts in usage ('<' not supported between 'NoneType' and 'int') #4325

Closed

Description

@mhosseinab

What happened?

Semantic extraction through an OpenAI-compatible endpoint fails on every chunk when the endpoint returns a usage object whose token counts are null. The model's response itself is fine, but the whole chunk is discarded:

[graphify] chunk 1/1 failed: '<' not supported between instances of 'NoneType' and 'int'

_call_openai_compat only guards against usage being absent, not against its fields being None:

# graphify/llm.py (v8, lines 1486-1487)
result["input_tokens"] = resp.usage.prompt_tokens if resp.usage else 0
result["output_tokens"] = resp.usage.completion_tokens if resp.usage else 0
...
output_tokens = result["output_tokens"]
if output_tokens < 50 and backend == "ollama":   # line 1500 -> TypeError

The comparison runs for every backend (the backend == "ollama" test is second), so any OpenAI-compatible gateway that reports "completion_tokens": null breaks, including custom providers from ~/.graphify/providers.json. Even without that comparison, the None would fail later in the per-chunk merge (merged["output_tokens"] += result.get("output_tokens", 0)).

_call_azure has the same two lines (1888-1889).

Steps to reproduce

No network needed; a fake client that returns null token counts reproduces it:

import json
from types import SimpleNamespace as NS
import openai
from graphify import llm

body = json.dumps({"nodes": [{"id": "a", "label": "A"}], "edges": [], "hyperedges": []})

class Fake:
    def __init__(self, **kw):
        create = lambda **k: NS(
            choices=[NS(message=NS(content=body), finish_reason="stop")],
            usage=NS(prompt_tokens=None, completion_tokens=None),
        )
        self.chat = NS(completions=NS(create=create))

openai.OpenAI = Fake
llm._call_openai_compat("https://example.invalid/v1", "k", "m", "text", backend="openai")
# TypeError: '<' not supported between instances of 'NoneType' and 'int'

In real use: add a custom provider pointing at a gateway that omits token counts, then run graphify extract <docs-folder> --backend <that-provider>.

Error output or graph output

[graphify extract] semantic extraction on 30 files via openai-custom...
[graphify] chunk 1/1 failed: '<' not supported between instances of 'NoneType' and 'int'
[graphify] WARNING: 1/1 semantic chunk(s) failed — see errors above. Partial results returned.
[graphify] WARNING: 30/30 dispatched file(s) produced no nodes and are absent from the graph: ...
[graphify extract] error: all semantic chunks failed for backend 'openai-custom' (30 uncached files) - see per-chunk errors above.

The second warning ("The model returned a response but omitted them") is misleading here: the model did return content; graphify dropped it while reading token counts.

Proposed solution

Coerce null counts to 0 where the response is read, in both OpenAI-shaped paths:

-    result["input_tokens"] = resp.usage.prompt_tokens if resp.usage else 0
-    result["output_tokens"] = resp.usage.completion_tokens if resp.usage else 0
+    result["input_tokens"] = (resp.usage.prompt_tokens or 0) if resp.usage else 0
+    result["output_tokens"] = (resp.usage.completion_tokens or 0) if resp.usage else 0

This matches what the claude-cli path already does (int(usage.get("output_tokens", 0) or 0)). The Anthropic (resp.usage.input_tokens) and Bedrock (usage.get("inputTokens", 0)) paths have the same shape and may deserve the same guard, though I have not seen them fail.

With this patch applied locally to _call_openai_compat and _call_azure, a parametrized test over usage = (None, None), (120, None), (120, 80) and usage=None passes (it fails with the TypeError above before the patch). Happy to open a PR with the fix and the test.

One side effect worth noting: cost tracking reports 0 tokens for such endpoints. That seems acceptable (the data is not available), but a one-time warning might be friendlier than silent zeros.

Graphify version

0.9.85

Operating System

macOS (Darwin 25.6.0)

Python Version

3.14

Installation Method

uv tool install "graphifyy[openai]"

Additional Environment Details

Custom provider defined in ~/.graphify/providers.json (OpenAI-compatible gateway), openai Python package 3.28.0.

Activity

  1. github-actions commented on Oct 11, 2026

    @github-actions

    Thanks for opening this issue, @mhosseinab. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. added a commit that references this issue on Oct 11, 2026
    1b21e04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions