googleapis / googleapis/python-genai

How to count tokens for `gemini-embedding-001` model?

Open
#1,541 9 comments 5 reactions 1 assignee Claimed by @Venkaiahbabuneelam View on GitHub
priority: p3 type: feature request
Dominant language
Python
Stars
4k
Forks
1k
Avg merge
2d 11h
Merged PRs (30d)
40

Description

### What I'm trying to do
I need to count tokens before making requests to `gemini-embedding-001` because there's a limit of 20,000 tokens per request according to the API documentation:

> Each request can have up to 250 input texts (generating 1 embedding per input text) and 20,000 tokens per request.

### What code I've already tried
I attempted to use `count_tokens()` to calculate the token count before sending my request:

```python
total_tokens = client.models.count_tokens(
model="gemini-embedding-001",
contents=[
"How do I get a driver's license/learner's permit?",
"How long is my driver's license valid for?",
"Driver's knowledge test study guide",
],
)
```

### Error message I'm getting
```
ClientError: 400 INVALID_ARGUMENT
{'error': {'code': 400, 'message': 'Should provide instances for text model prediction.', 'status': 'INVALID_ARGUMENT'}}
```

### Additional context
According to the documentation, `gemini-embedding-001` is not listed among the [models that support `count_tokens()`](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/get-token-count#supported_models). When I test with `gemini-2.5-flash` (which is listed as supported), the code works without errors.

**Question:** Is there a way to count tokens for `gemini-embedding-001`? Without this functionality, I cannot ensure my requests stay within the 20,000 token limit. Should `gemini-embedding-001` be added to the list of supported models for `count_tokens()`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.