the count_token feature in python gives different value than what the gemini input tokens
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the issue with the uploaded PDF using the Python SDK's count_tokens() and generate_content() entry points, recording the requests and token values shown in the report. Compare how the uploaded file is represented in each call and verify the behavior with a smaller input. Done means the two operations account for the same uploaded content and generation no longer reports an inflated token count.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- Go
- Stars
- 143
- Forks
- 68
- Avg merge
- 10h 30m
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google-gemini/api-examples
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
google-gemini/api-examples#104 · 2 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
google-gemini/api-examples#89 · 1 comment ·
-
TtOpen
Difficulty 5/5 Over a week Newbie friendliness 1/100
-
SoqceOpen
Difficulty 5/5 Over a week Newbie friendliness 10/100
All issues in google-gemini/api-examples
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
google/osv-scanner#3327 ·
Maintainers usually reply within 1 day
-
tld-snapshot
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
awslabs/ferret-scan#1064 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
VictoriaMetrics/VictoriaTraces#335 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 62/100
DaoCloud/DaoCloud-docs#7839 ·
Maintainers usually reply within 1 day