fix(llm): handle None response in total_token_count_from_response #10941
+9
−3
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 problem does this PR solve?
Fixes #10933
This PR fixes a
TypeErrorin the Gemini model provider where thetotal_token_count_from_response()function could receive aNoneresponse object, causing the error:TypeError: argument of type 'NoneType' is not iterable
Root Cause:
The function attempted to use the
inoperator to check dictionary keys (lines 48, 54, 60) without first validating thatrespwas notNone. When Gemini'schat_streamly()method returnsNone, this triggers the error.Solution:
0ifresp is Noneisinstance(resp, dict)checks before allinoperations to ensure type safetyType of change
Changes Made
File:
rag/utils/__init__.pyif resp is None: return 0checkisinstance(resp, dict)before'usage' in respisinstance(resp, dict)before'usage' in respisinstance(resp, dict)before'meta' in respTesting
Additional Notes
This fix ensures robust handling of various response types from LLM providers, particularly Gemini, w