Repository navigation
fix(chat): return the completion when a chat response has no usage - #503
Merged
ASuresh0524 merged 2 commits intoDec 3, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
In the OpenAI SDK (
ChatCompletion.usage),usageisOptional[CompletionUsage], so a valid chat completion doesn't have to include it. Right nowOpenAIChat,MiniMaxChat,NovitaChat,AtlasCloudChat, andLiteLLMChatall readresponse.usage.total_tokensinside the sametryblock as the request, so whenusageis missing theAttributeErrorgets caught by the genericexcept Exceptionandask()returns"Error: Could not get a response from ..."instead of the actual reply. Underleann react, that string has noAction:in it, soReActAgenthands it back as the final answer.Minimal reproducer using the real
openaiclient over anhttpx.MockTransport:On main (
494ef3c):With this PR:
Since that token-logging line was added separately across each provider (#147, #282, #294, #388, #395), I moved it into one
_report_token_usage(usage, emit)helper inchat.pythat returns early whenusage is None. Each caller passes its own emitter (printfor OpenAI, MiniMax, and LiteLLM;logger.infofor Novita and Atlas Cloud) so nothing bindsprintat import time.AnthropicChathas a different, required usage shape and is untouched.The new tests in
tests/test_chat_missing_usage.pyexercise the realopenaiclient againsthttpx.MockTransportfor missing usage (four providers) and normal usage printing/logging (including a runtime-patchedprint), andtests/test_litellm_provider.pycovers both cases against the existing LiteLLM stub.Related Issues
No existing issue found (checked open/closed issues and PRs for
usage/total_tokens; #498 only touchesmax_completion_tokens).Checklist
python -m pytest tests, excludingtest_embedding_server_manager_e2e.py) has 14 failures and 2 collection errors on both main and this branch with identical node ids, mostly from the ivf/diskann backends not being installed locally.ruff formatandruff checkon the changed files)pre-commit run --from-ref upstream/main --to-ref HEAD)