-
Notifications
You must be signed in to change notification settings - Fork 7.4k
fix(llm): handle None response in total_token_count_from_response #10941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(llm): handle None response in total_token_count_from_response #10941
Conversation
|
Appreciations! |
|
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 <[email protected]>
32e39ff to
326ff98
Compare
|
Thank you for the feedback! I've updated the PR to address your comments: ✅ Resolved merge conflicts by rebasing on latest The upstream code already had the Please let me know if any further changes are needed. Thanks! |
- Clarify function purpose and return behavior - Document handling of None responses - Improve code maintainability
|
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. |
…1120 * main: (53 commits) Use array syntax for commands in docker-compose-base.yml (infiniflow#11391) Feature (canvas): Add mind tagging support (infiniflow#11359) locale en add russian language option (infiniflow#11392) Locale: update russian language (infiniflow#11393) Feat: Add TCADP parser for PPTX and spreadsheet document types. (infiniflow#11041) fix(llm): handle None response in total_token_count_from_response (infiniflow#10941) feat: add OceanBase doc engine (infiniflow#11228) fix cohere rerank base_url default (infiniflow#11353) Feat: Fixed an issue where modifying fields in the agent operator caused the loss of structured data. infiniflow#10427 (infiniflow#11388) Docs: minor (infiniflow#11385) Doc: Optimize read me (infiniflow#11386) Fix some multilingual issues (infiniflow#11382) Feat: If a query variable in a data manipulation operator is deleted, a warning message should be displayed to the user. infiniflow#10427 infiniflow#11255 (infiniflow#11384) Fix: refine error msg. (infiniflow#11380) Doc: Added v0.22.1 release notes (infiniflow#11383) Feat: The key for the begin operator can only contain alphanumeric characters and underscores. infiniflow#10427 (infiniflow#11377) Fix: circle imports issue. (infiniflow#11374) Feat: Structured data will still be stored in outputs for compatibility with older versions. infiniflow#10427 (infiniflow#11368) Add release notes (infiniflow#11372) Update README for supporting Gemini 3 Pro (infiniflow#11369) ... # Conflicts: # pyproject.toml # web/src/locales/ru.ts
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