google-gemini / google-gemini/api-examples
Code example to match actual output
- Dominant language
- JavaScript
- Stars
- 139
- Forks
- 61
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 2
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.
```python
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,
```python
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.
```diff
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_
Contributor guide
Assessment
This issue has not been assessed yet.