Skip to content

Conversation

@zhangzhefang-github
Copy link

@zhangzhefang-github zhangzhefang-github commented Nov 2, 2025

What problem does this PR solve?

Fixes #10933

This PR fixes a TypeError in the Gemini model provider where the total_token_count_from_response() function could receive a None response object, causing the error:

TypeError: argument of type 'NoneType' is not iterable

Root Cause:
The function attempted to use the in operator to check dictionary keys (lines 48, 54, 60) without first validating that resp was not None. When Gemini's chat_streamly() method returns None, this triggers the error.

Solution:

  1. Added a null check at the beginning of the function to return 0 if resp is None
  2. Added isinstance(resp, dict) checks before all in operations to ensure type safety
  3. This defensive programming approach prevents the TypeError while maintaining backward compatibility

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

Changes Made

File: rag/utils/__init__.py

  • Line 36-38: Added if resp is None: return 0 check
  • Line 52: Added isinstance(resp, dict) before 'usage' in resp
  • Line 58: Added isinstance(resp, dict) before 'usage' in resp
  • Line 64: Added isinstance(resp, dict) before 'meta' in resp

Testing

  • Code compiles without errors
  • Follows existing code style and conventions
  • Change is minimal and focused on the specific issue

Additional Notes

This fix ensures robust handling of various response types from LLM providers, particularly Gemini, w

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Nov 2, 2025
@KevinHuSh
Copy link
Collaborator

Appreciations!
Please resolve the conflicts.

@JinHai-CN
Copy link
Contributor

The function has already been moved to common/token_utils.py. Would you please also update the PR? @zhangzhefang-github

- Add isinstance(resp, dict) checks before using 'in' operator
- Prevents TypeError when resp is not a dictionary
- Applies fix to new location: common/token_utils.py

Fixes infiniflow#10933

Signed-off-by: Zhang Zhefang <zhangzhefang@example.com>
@zhangzhefang-github zhangzhefang-github force-pushed the fix/issue-10933-gemini-nonetype-error branch from 32e39ff to 326ff98 Compare November 3, 2025 07:12
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Nov 3, 2025
@zhangzhefang-github
Copy link
Author

@KevinHuSh @JinHai-CN

Thank you for the feedback! I've updated the PR to address your comments:

✅ Resolved merge conflicts by rebasing on latest main
✅ Applied the fix to the correct location: common/token_utils.py (lines 53, 59, 65)
✅ All CI checks passing
✅ Ready for final review

The upstream code already had the None check (line 38-39), so I only added the isinstance(resp, dict) checks before the in operators to fully fix issue #10933.

Please let me know if any further changes are needed. Thanks!

@KevinHuSh KevinHuSh added the ci Continue Integration label Nov 3, 2025
- Clarify function purpose and return behavior
- Document handling of None responses
- Improve code maintainability
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Nov 4, 2025
@zhangzhefang-github
Copy link
Author

I've added a docstring to improve code documentation and re-triggered the CI tests.

The previous CI failure was caused by a MySQL container health check issue in the test environment (unrelated to the code changes). Hopefully this run will complete successfully.

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

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: gemini-2.5-flash ERROR: argument of type 'NoneType' is not iterable

3 participants