Skip to content

fix(llm): coerce null token counts to 0 across backends - #4340

Closed
rtmalikian wants to merge 1 commit into
Graphify-Labs:v8from
rtmalikian:fix/4325-null-token-counts
Closed

rtmalikian wants to merge 1 commit into
Graphify-Labs:v8from
rtmalikian:fix/4325-null-token-counts

Conversation

@rtmalikian

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4325.

Some OpenAI-compatible gateways return a usage object whose token-count fields are null. The backend readers only guarded against usage being absent, not against its fields being None, so result["input_tokens"] / result["output_tokens"] became None and crashed extraction for the whole chunk:

  • _call_openai_compat: TypeError: '<' not supported between 'NoneType' and 'int' at the ollama low-token warning (output_tokens < 50 runs for every backend — the backend == "ollama" check is second).
  • Every backend: None propagated into the per-chunk token merge (merged["output_tokens"] += ...).

The fix coerces null counts to 0 at read time in all four SDK-object paths — _call_openai_compat, _call_azure, _call_claude (Anthropic), and _call_bedrock — matching the or 0 idiom the claude-cli path already uses. The _rec aggregation sites were already None-safe (int(x or 0)) and are untouched.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Invariant protected: result["input_tokens"] / result["output_tokens"] are always int after any backend call, so downstream numeric comparisons and merges never see None.

How was this tested?

uv run pytest tests/test_llm_null_token_counts.py tests/test_llm_backends.py -q
# 112 passed (4 new regression tests + 108 existing backend tests)

uv run ruff check graphify/llm.py tests/test_llm_null_token_counts.py
# All checks passed!

Negative control: all 4 new tests fail on the pre-fix code — the openai-compat test reproduces the exact issue error TypeError: '<' not supported between instances of 'NoneType' and 'int' at graphify/llm.py:1500. ruff format --check on llm.py reports only pre-existing non-conformance at HEAD (unrelated regions; none on the changed lines).

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. — N/A, no skill fragments changed.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages. — see below.

Additional Notes

Author: Raphael Malikian — [email protected]. If you'd like similar help with your own project or codebase, I'm open to collaborate — feel free to reach out.

AI assistance disclosure: this PR was prepared with AI assistance.

Some OpenAI-compatible gateways return a usage object whose token-count
fields are null. Reading them verbatim left None in result["input_tokens"] /
result["output_tokens"], which crashed extraction for the whole chunk
(TypeError: '<' not supported between 'NoneType' and 'int' in the ollama
low-token warning; None also propagated through the per-chunk merge).

Coerce null counts to 0 at read time in _call_openai_compat, _call_azure,
_call_claude, and _call_bedrock, matching the existing `or 0` idiom already
used on the claude-cli path.

Fixes Graphify-Labs#4325

Signed-off-by: Raphael Malikian <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @rtmalikian. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 11, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 4 changed function(s) (4 not verified).

Formal verification. PR-changed functions: 0/4 verified (0 proven, 0 may-equivalent, 0 distinguished) · 4 not verified (4 error).

Not verified on this run: \_call\_azure (error), \_call\_bedrock (error), \_call\_claude (error), \_call\_openai\_compat (error).


Graphify review — findings

Fixes crashes when a backend's usage object reports null token counts (#4325): _call_openai_compat, _call_azure, _call_claude, and _call_bedrock now coerce None to 0. This keeps the ollama low-token warning and the per-chunk token merge from comparing or adding None. New regression tests stub each SDK with null usage and check that both counts come back as 0.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 899 functions depend on the 186 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: _call_openai_compat() — 26 callers, 8 callees
  • worse: _call_bedrock() — 6 callers, 8 callees
  • worse: _call_claude() — 4 callers, 8 callees
  • worse: _call_azure() — 3 callers, 4 callees

Verification — 899 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 535 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

47 of 345 test file(s) selected (14%) via static blast radius.

  • tests/test_backend_env_isolation.py — impact
  • tests/test_backend_extras.py — impact
  • tests/test_build.py — impact
  • tests/test_build_located_semantic_identity.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_claude_cli_backend.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_dedup.py — impact
  • tests/test_dedup_remaps_hyperedges.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_dedup_survivor_richness.py — impact
  • tests/test_evidence_binding.py — impact
  • tests/test_file_slice.py — impact
  • tests/test_file_truncation_warning.py — impact
  • tests/test_global_graph.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_hyperedge_member_shapes.py — impact
  • tests/test_image_vision.py — impact
  • tests/test_injection_sentinel_coverage.py — impact
  • tests/test_issue_3472_source_file_collision.py — impact
  • tests/test_label_retry.py — impact
  • tests/test_labeling.py — impact
  • tests/test_llm_backends.py — impact
  • tests/test_llm_null_token_counts.py — impact, changed-test
  • tests/test_llm_parser.py — impact
  • tests/test_llm_parser_reasoning.py — impact
  • tests/test_no_dedup_flag.py — impact
  • tests/test_non_string_node_ids.py — impact
  • tests/test_ollama.py — impact
  • tests/test_ollama_retry_cap.py — impact
  • tests/test_oversized_document_slicing.py — impact
  • tests/test_partial_cache.py — impact
  • tests/test_pdf_slicing.py — impact
  • tests/test_pdf_token_estimate.py — impact
  • tests/test_provider_registry.py — impact
  • tests/test_prs.py — impact
  • tests/test_prune_sweeps_orphans.py — impact
  • tests/test_r_extractor.py — impact
  • tests/test_semantic_fragment_sanitize.py — impact
  • tests/test_source_file_encoding.py — impact
  • tests/test_unverified_semantic_shrink.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Docs that may be stale (advisory)

…and 10 more.

Formal verification

Verification did not run: Verification did not run for \_call\_azure.

The verification could not execute (an environment/toolchain issue, not a statement about the code).

Guarantee: No guarantee, the check itself did not complete.

Note: Detail: harness produced no verdict (rc=124): timeout after 30s

Verification did not run: Verification did not run for \_call\_bedrock.

The verification could not execute (an environment/toolchain issue, not a statement about the code).

Guarantee: No guarantee, the check itself did not complete.

Note: Detail: harness produced no verdict (rc=124): timeout after 30s

Verification did not run: Verification did not run for \_call\_claude.

The verification could not execute (an environment/toolchain issue, not a statement about the code).

Guarantee: No guarantee, the check itself did not complete.

Note: Detail: harness produced no verdict (rc=124): timeout after 30s

Verification did not run: Verification did not run for \_call\_openai\_compat.

The verification could not execute (an environment/toolchain issue, not a statement about the code).

Guarantee: No guarantee, the check itself did not complete.

Note: Detail: harness produced no verdict (rc=124): timeout after 30s

· 4 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.86 (now on PyPI) via an authorship-preserving cherry-pick, so your commit authorship is retained. Thank you, @rtmalikian!

@safishamsi safishamsi closed this Oct 11, 2026
@rtmalikian

Copy link
Copy Markdown
Contributor Author

Thank you, safishamsi! Glad the null-token coercion fix made it into 0.9.86. Happy to help further if anything comes up. — Raphael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants