Code example to match actual output

Open
#104 2 comments 0 reactions 0 assignees View on GitHub

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google-gemini/api-examples

All issues in google-gemini/api-examples

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.