ragflow/common
张哲芳 ecf0322165
fix(llm): handle None response in total_token_count_from_response (#10941)
### 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

- [x] 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

- [x] Code compiles without errors
- [x] Follows existing code style and conventions
- [x] 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

---------

Signed-off-by: Zhang Zhefang <zhangzhefang@example.com>
2025-11-20 10:04:03 +08:00
..
data_source Feat: add s3-compatible storage boxes (#11313) 2025-11-18 09:39:25 +08:00
__init__.py Refactor: rename rmSpace to remove_redundant_spaces (#10796) 2025-10-28 09:46:32 +08:00
config_utils.py Move some enumerate type to constants.py (#10998) 2025-11-04 19:25:25 +08:00
connection_utils.py Fix: refine error msg. (#11380) 2025-11-19 19:10:45 +08:00
constants.py Feat: add initial Google Drive connector support (#11147) 2025-11-10 19:15:02 +08:00
decorator.py Move singleton to common directory (#10935) 2025-11-02 12:24:08 +08:00
exceptions.py Feat: GraphRAG handle cancel gracefully (#11061) 2025-11-06 16:12:20 +08:00
file_utils.py Refactor file utils (#10970) 2025-11-03 18:54:55 +08:00
float_utils.py Refactor: rename rmSpace to remove_redundant_spaces (#10796) 2025-10-28 09:46:32 +08:00
log_utils.py Feat: add Jira connector (#11285) 2025-11-17 09:38:04 +08:00
mcp_tool_call_conn.py Refactor: move mcp connection utilities to common (#11304) 2025-11-17 15:34:17 +08:00
misc_utils.py Move some functions out of 'api/utils/common.py' (#10948) 2025-11-03 12:34:47 +08:00
settings.py feat: add OceanBase doc engine (#11228) 2025-11-20 10:00:14 +08:00
signal_utils.py Fix and refactor imports (#11010) 2025-11-05 11:07:54 +08:00
string_utils.py Refactor: rename rmSpace to remove_redundant_spaces (#10796) 2025-10-28 09:46:32 +08:00
time_utils.py Add time utils (#10849) 2025-10-28 19:09:14 +08:00
token_utils.py fix(llm): handle None response in total_token_count_from_response (#10941) 2025-11-20 10:04:03 +08:00
versions.py Admin: add 'show version' (#11079) 2025-11-06 19:24:46 +08:00