Skip to content

Commit 326ff98

Browse files
fix(llm): handle None response in total_token_count_from_response
- 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 #10933 Signed-off-by: Zhang Zhefang <[email protected]>
1 parent 5a88c01 commit 326ff98

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

common/token_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ def total_token_count_from_response(resp):
5050
except Exception:
5151
pass
5252

53-
if 'usage' in resp and 'total_tokens' in resp['usage']:
53+
if isinstance(resp, dict) and 'usage' in resp and 'total_tokens' in resp['usage']:
5454
try:
5555
return resp["usage"]["total_tokens"]
5656
except Exception:
5757
pass
5858

59-
if 'usage' in resp and 'input_tokens' in resp['usage'] and 'output_tokens' in resp['usage']:
59+
if isinstance(resp, dict) and 'usage' in resp and 'input_tokens' in resp['usage'] and 'output_tokens' in resp['usage']:
6060
try:
6161
return resp["usage"]["input_tokens"] + resp["usage"]["output_tokens"]
6262
except Exception:
6363
pass
6464

65-
if 'meta' in resp and 'tokens' in resp['meta'] and 'input_tokens' in resp['meta']['tokens'] and 'output_tokens' in resp['meta']['tokens']:
65+
if isinstance(resp, dict) and 'meta' in resp and 'tokens' in resp['meta'] and 'input_tokens' in resp['meta']['tokens'] and 'output_tokens' in resp['meta']['tokens']:
6666
try:
6767
return resp["meta"]["tokens"]["input_tokens"] + resp["meta"]["tokens"]["output_tokens"]
6868
except Exception:

0 commit comments

Comments
 (0)