Skip to content

fix callers of count_tokens#554

Open
GdoongMathew wants to merge 1 commit into
AsyncFuncAI:mainfrom
GdoongMathew:fix/count_token_embedder_type
Open

fix callers of count_tokens#554
GdoongMathew wants to merge 1 commit into
AsyncFuncAI:mainfrom
GdoongMathew:fix/count_token_embedder_type

Conversation

@GdoongMathew

Copy link
Copy Markdown
Contributor

Summary

Fix callers in simple_chat.py and websocket_wiki.py calling signature. Since the input signatures are as follows

def count_tokens(text: str, embedder_type: str = None, is_ollama_embedder: bool = None) -> int:

passing a bool value to the embedder_type is missleading. Change to pass provider string directly.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the token counting logic in both api/simple_chat.py and api/websocket_wiki.py to pass the raw request.provider string as the embedder_type argument to count_tokens. The reviewer suggests normalizing the provider string to lowercase using .lower() to ensure robust, case-insensitive matching and prevent potential issues with mixed-case provider names.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread api/simple_chat.py
last_message = request.messages[-1]
if hasattr(last_message, 'content') and last_message.content:
tokens = count_tokens(last_message.content, request.provider == "ollama")
tokens = count_tokens(last_message.content, embedder_type=request.provider)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since request.provider is defined as a general str without strict casing validation in ChatCompletionRequest, a user could potentially pass provider names with mixed or upper casing (e.g., "Ollama", "Google"). Since count_tokens performs exact string matching (e.g., embedder_type == 'ollama'), passing the raw provider string directly might bypass the correct tokenizer selection. Normalizing the provider string to lowercase using .lower() ensures robust and case-insensitive matching.

Suggested change
tokens = count_tokens(last_message.content, embedder_type=request.provider)
tokens = count_tokens(last_message.content, embedder_type=request.provider.lower())

Comment thread api/websocket_wiki.py
last_message = request.messages[-1]
if hasattr(last_message, 'content') and last_message.content:
tokens = count_tokens(last_message.content, request.provider == "ollama")
tokens = count_tokens(last_message.content, embedder_type=request.provider)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since request.provider is defined as a general str without strict casing validation in ChatCompletionRequest, a user could potentially pass provider names with mixed or upper casing (e.g., "Ollama", "Google"). Since count_tokens performs exact string matching (e.g., embedder_type == 'ollama'), passing the raw provider string directly might bypass the correct tokenizer selection. Normalizing the provider string to lowercase using .lower() ensures robust and case-insensitive matching.

Suggested change
tokens = count_tokens(last_message.content, embedder_type=request.provider)
tokens = count_tokens(last_message.content, embedder_type=request.provider.lower())

@GdoongMathew
GdoongMathew force-pushed the fix/count_token_embedder_type branch from 99ca4fc to 92a3c4e Compare July 21, 2026 10:25
@GdoongMathew
GdoongMathew force-pushed the fix/count_token_embedder_type branch from 92a3c4e to cb9b34a Compare July 21, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant