Code example to match actual output
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 48/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- python
- Domain
- documentation
Research direction
Start with python/count_tokens.py and compare the documented expected output with the output produced by the current example. Review whether the proposed count_tokens and usage_metadata presentation should be applied only there or across other documentation examples. Done means the example and its displayed output agree, with the intended scope confirmed.
Written by the indexing model from the issue text.
Description
Description of the feature request:
Hi everyone.
Issues
On the document, the code and expected output does not much.
For example, on python/count_tokens.py explains below.
def test_tokens_text_only(self):
# [START tokens_text_only]
from google import genai
client = genai.Client()
prompt = "The quick brown fox jumps over the lazy dog."
# Count tokens using the new client method.
total_tokens = client.models.count_tokens(
model="gemini-3.5-flash", contents=prompt
)
print("total_tokens: ", total_tokens)
# ( e.g., total_tokens: 10 )
response = client.models.generate_content(
model="gemini-3.5-flash", contents=prompt
)
# The usage_metadata provides detailed token counts.
print(response.usage_metadata)
# ( e.g., prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )
# [END tokens_text_only]
However,
print("total_tokens: ", total_tokens)
# ( e.g., total_tokens: 10 )
does not reply as ( e.g. ) says.
Question and Proposal
Are ( e.g.)s intentionally written like above on the document?
If not and if we try to nicely write the document for beginners, we should write like below code.
def test_tokens_text_only(self):
# [START tokens_text_only]
from google import genai
client = genai.Client()
prompt = "The quick brown fox jumps over the lazy dog."
# Count tokens using the new client method.
- total_tokens = client.models.count_tokens(
- model="gemini-3.5-flash", contents=prompt
- )
- print("total_tokens: ", total_tokens)
+ response = client.models.count_tokens(
+ model="gemini-3.5-flash", contents=prompt
+ )
+ print("total_tokens: ", response.total_tokens)
# ( e.g., total_tokens: 10 )
response = client.models.generate_content(
model="gemini-3.5-flash", contents=prompt
)
# The usage_metadata provides detailed token counts.
- print(response.usage_metadata)
+ metadata = response.usage_metadata
+ print(f"prompt_token_count: {metadata.prompt_token_count}, candidates_token_count: {metadata.candidates_token_count}, total_token_count: {metadata.total_token_count}")
# ( e.g., prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )
# [END tokens_text_only]
My concern
If we conduct this change, we have to change many parts on the document. Thus, I need your advices.
Thank you.
What problem are you trying to solve with this feature?
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 5/5 Over a week Newbie friendliness 25/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/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