You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When trying to use get_num_tokens_from_messages with a ChatVertexAI model and multi-modal inputs, the token count from the Langchain method is wildly inflated (1369082) vs the GenAI SDK and Vertex AI Console token number (3358).
To run: uv run --env-file=.env img_token_test.py buddy-photo-pd61clsCVnY-unsplash.jpg
Notating this issue here - if I have time I'll open a PR to fix but figured I would open it in case someone else picks it up sooner. Seems like we need to implement an overloaded version of get_num_tokens_from_messages specific to ChatVertexAI
The text was updated successfully, but these errors were encountered:
Similar to #491
When trying to use
get_num_tokens_from_messages
with aChatVertexAI
model and multi-modal inputs, the token count from the Langchain method is wildly inflated (1369082) vs the GenAI SDK and Vertex AI Console token number (3358).I believe this is due to the use of the inherited
get_num_tokens_from_messages
method that BaseChatModel inherits from BaseModel. This method usesget_buffer_string
to convert everything in a message to a string https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/messages/utils.py#L82Unfortunately, that means the entire base64 string gets counted vs the image_bytes that the GenAI SDK (and presumably, Vertex AI console) uses.
This makes it very difficult to track token usage and debug token limit exceeded errors.
Here is an example, with a sample image to use.
You'll need the following .env to run the example:
To run:
uv run --env-file=.env img_token_test.py buddy-photo-pd61clsCVnY-unsplash.jpg
Notating this issue here - if I have time I'll open a PR to fix but figured I would open it in case someone else picks it up sooner. Seems like we need to implement an overloaded version of
get_num_tokens_from_messages
specific to ChatVertexAIThe text was updated successfully, but these errors were encountered: