the count_token feature in python gives different value than what the gemini input tokens
还没有人认领这个 Issue。
评估
调研方向
使用 Python SDK 的 count_tokens() 和 generate_content() 入口,通过上传的 PDF 重现该问题,并记录报告中显示的请求和 token 值。比较上传的文件在每次调用中的表示方式,并使用更小的输入验证行为。当这两个操作处理的是相同的上传内容,且生成不再报告虚高的 token 数量时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Description of the bug:
When I upload a PDF file (~33k tokens per count_tokens()), generate_content() fails with an INVALID_ARGUMENT error claiming the input token count exceeds the model limit (~1.2M tokens).
It seems the SDK is serializing or expanding the uploaded file differently between count_tokens() and generate_content().
2025-10-25 00:09:21,699 - httpx - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/upload/v1beta/files "HTTP/1.1 200 OK"
2025-10-25 00:09:24,932 - httpx - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/upload/v1beta/files?upload_id=AAwnv3LE0Tqc2iJbl8YuN8TXK6SUYhYJY4dMaVrLWd_WkLcDpvohyxjHCvtA1pHyN-vf9lLgVuoGtkjZ4VgZjPMxFWk_JSLo0GudQidxicPQ2y0&upload_protocol=resumable "HTTP/1.1 200 OK"
2025-10-25 00:09:27,655 - httpx - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:countTokens "HTTP/1.1 200 OK"
Uploaded file tokens: total_tokens=33541 cached_content_token_count=None
2025-10-25 00:09:27,658 - google_genai.models - INFO - AFC is enabled with max remote calls: 10.
2025-10-25 00:10:03,628 - httpx - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent "HTTP/1.1 400 Bad Request"
Traceback (most recent call last):
File "/Users/anshulkumar/backfin/tet.py", line 22, in
response = client.models.generate_content(model="gemini-2.5-flash-lite",
contents=[uploaded_files,prompt])
File "/Users/anshulkumar/backfin/.venv/lib/python3.13/site-packages/google/genai/models.py", line 5202, in generate_content
response = self._generate_content(
model=model, contents=contents, config=config
)
File "/Users/anshulkumar/backfin/.venv/lib/python3.13/site-packages/google/genai/models.py", line 4178, in _generate_content
response_dict = self._api_client.request(
'post', path, request_dict, http_options
)
File "/Users/anshulkumar/backfin/.venv/lib/python3.13/site-packages/google/genai/_api_client.py", line 755, in request
response = self._request(http_request, stream=False)
File "/Users/anshulkumar/backfin/.venv/lib/python3.13/site-packages/google/genai/_api_client.py", line 684, in _request
errors.APIError.raise_for_response(response)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "/Users/anshulkumar/backfin/.venv/lib/python3.13/site-packages/google/genai/errors.py", line 101, in raise_for_response
raise ClientError(status_code, response_json, response)
google.genai.errors.ClientError: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'The input token count exceeds the maximum number of tokens allowed 1237083.', 'status': 'INVALID_ARGUMENT'}}
Actual vs expected behavior:
No response
Any other information you'd like to share?
No response
- 主要语言
- Go
- 星标
- 143
- 派生
- 68
- 平均合并
- 10 小时 30 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
google-gemini/api-examples 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 48/100
google-gemini/api-examples#104 · 2 条评论 ·
-
难度 5/5 一周以上 新手友好度 25/100
-
难度 3/5 1-2 天 新手友好度 42/100
google-gemini/api-examples#89 · 1 条评论 ·
-
Tt未关闭
难度 5/5 一周以上 新手友好度 1/100
-
Soqce未关闭
难度 5/5 一周以上 新手友好度 10/100
查看 google-gemini/api-examples 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
google/osv-scanner#3327 ·
维护者通常 1 天内回复
-
tld-snapshot
难度 2/5 1-3 小时 新手友好度 72/100
awslabs/ferret-scan#1064 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
VictoriaMetrics/VictoriaTraces#335 ·
维护者通常 1 天内回复
-
enhancement
难度 1/5 1 小时以内 新手友好度 62/100
DaoCloud/DaoCloud-docs#7839 ·
维护者通常 1 天内回复