Repository navigation
fix(llm): coerce null token counts to 0 across backends - #4340
rtmalikian wants to merge 1 commit into
Conversation
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]>
|
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. |
There was a problem hiding this comment.
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— impacttests/test_backend_extras.py— impacttests/test_build.py— impacttests/test_build_located_semantic_identity.py— impacttests/test_build_merge_dedup_scope.py— impacttests/test_build_merge_hyperedges_and_prune.py— impacttests/test_build_merge_shrink_guard.py— impacttests/test_carried_hyperedge_remap.py— impacttests/test_charmap_encoding.py— impacttests/test_chunking.py— impacttests/test_claude_cli_backend.py— impacttests/test_corrupt_graph_json.py— impacttests/test_cross_extension_reexport_self_cycle.py— impacttests/test_dedup.py— impacttests/test_dedup_remaps_hyperedges.py— impacttests/test_dedup_shrink_refuses_force_write.py— impacttests/test_dedup_survivor_richness.py— impacttests/test_evidence_binding.py— impacttests/test_file_slice.py— impacttests/test_file_truncation_warning.py— impacttests/test_global_graph.py— impacttests/test_go_qualified_resolution.py— impacttests/test_hyperedge_member_shapes.py— impacttests/test_image_vision.py— impacttests/test_injection_sentinel_coverage.py— impacttests/test_issue_3472_source_file_collision.py— impacttests/test_label_retry.py— impacttests/test_labeling.py— impacttests/test_llm_backends.py— impacttests/test_llm_null_token_counts.py— impact, changed-testtests/test_llm_parser.py— impacttests/test_llm_parser_reasoning.py— impacttests/test_no_dedup_flag.py— impacttests/test_non_string_node_ids.py— impacttests/test_ollama.py— impacttests/test_ollama_retry_cap.py— impacttests/test_oversized_document_slicing.py— impacttests/test_partial_cache.py— impacttests/test_pdf_slicing.py— impacttests/test_pdf_token_estimate.py— impacttests/test_provider_registry.py— impacttests/test_prs.py— impacttests/test_prune_sweeps_orphans.py— impacttests/test_r_extractor.py— impacttests/test_semantic_fragment_sanitize.py— impacttests/test_source_file_encoding.py— impacttests/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)
CHANGELOG.md§ 0.9.77 (2026-10-05) (lines 45-64): references changed symbols__init__CHANGELOG.md§ 0.9.58 (2026-09-10) (lines 297-314): references changed symbols__init__CHANGELOG.md§ 0.9.44 (2026-08-15) (lines 494-506): references changed symbolscreateCHANGELOG.md§ 0.9.42 (2026-08-13) (lines 516-534): references changed symbols__init__CHANGELOG.md§ 0.9.23 (2026-07-21) (lines 729-738): references changed symbols__init__CHANGELOG.md§ 0.9.22 (2026-07-20) (lines 739-748): references changed symbols__init__CHANGELOG.md§ 0.9.6 (2026-07-04) (lines 990-1015): references changed symbolsclientCHANGELOG.md§ 0.9.4 (2026-07-01) (lines 1040-1057): references changed symbolscreateCHANGELOG.md§ 0.8.51 (2026-06-28) (lines 1101-1113): references changed symbolscreateCHANGELOG.md§ 0.7.12 (2026-05-09) (lines 1641-1651): references changed symbolsclient
…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).
|
Landed in 0.9.86 (now on PyPI) via an authorship-preserving cherry-pick, so your commit authorship is retained. Thank you, @rtmalikian! |
|
Thank you, safishamsi! Glad the null-token coercion fix made it into 0.9.86. Happy to help further if anything comes up. — Raphael |
What does this PR do?
Fixes #4325.
Some OpenAI-compatible gateways return a
usageobject whose token-count fields arenull. The backend readers only guarded againstusagebeing absent, not against its fields beingNone, soresult["input_tokens"]/result["output_tokens"]becameNoneand crashed extraction for the whole chunk:_call_openai_compat:TypeError: '<' not supported between 'NoneType' and 'int'at the ollama low-token warning (output_tokens < 50runs for every backend — thebackend == "ollama"check is second).Nonepropagated into the per-chunk token merge (merged["output_tokens"] += ...).The fix coerces null counts to
0at read time in all four SDK-object paths —_call_openai_compat,_call_azure,_call_claude(Anthropic), and_call_bedrock— matching theor 0idiom theclaude-clipath already uses. The_recaggregation sites were alreadyNone-safe (int(x or 0)) and are untouched.Type of change
Verification & Invariants
Invariant protected:
result["input_tokens"]/result["output_tokens"]are alwaysintafter any backend call, so downstream numeric comparisons and merges never seeNone.How was this tested?
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'atgraphify/llm.py:1500.ruff format --checkonllm.pyreports only pre-existing non-conformance at HEAD (unrelated regions; none on the changed lines).Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. — N/A, no skill fragments changed.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.