Repository navigation
fix(vertex-ai): map billableCharacterCount to usage.prompt_tokens_details.character_count - #46028
aishwary-dongre wants to merge 2 commits into
Conversation
…ails.character_count For character-priced Vertex AI text embedding models (text-embedding-005, gemini-embedding-001, text-multilingual-embedding-002), the response includes metadata.billableCharacterCount but VertexTextEmbeddingConfig.transform_vertex_response_to_openai only summed token_count into prompt_tokens and discarded the character count. This maps metadata.billableCharacterCount to usage.prompt_tokens_details.character_count so callers can read the provider-reported billable character count. Fixes BerriAI#45999
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
VANDRANKI
left a comment
There was a problem hiding this comment.
Community review (not a maintainer, does not clear the merge gate). I read the full diff and ran it. As written, this change breaks Vertex text embeddings.
The new line in transform_vertex_response_to_openai is from litellm.types.llms.base import PromptTokensDetailsWrapper. PromptTokensDetailsWrapper is not defined in litellm/types/llms/base.py. It lives in litellm/types/utils.py. I ran on this branch:
python -c "from litellm.types.llms.base import PromptTokensDetailsWrapper"givesImportError: cannot import name 'PromptTokensDetailsWrapper' from 'litellm.types.llms.base'.pytest tests/unit/llms/vertex_ai/test_vertex_text_embedding_billable_character_count.py: 3 failed, all with that ImportError. So the PR's own new tests do not pass.- Calling
VertexAITextEmbeddingConfig().transform_vertex_response_to_openai(...)with modeltext-embedding-005: on current main it returns aUsage(with and withoutmetadata). On this branch it raises ImportError in all three cases I tried (no metadata,{"billableCharacterCount": 17},"metadata": None). - The import sits at the top of the method, before the
VertexBGEConfig.is_bge_modelcheck, so it also runs for BGE models.tests/unit/llms/vertex_ai/test_bge_embedding.pypasses on main (3 passed) and fails on this branch with the same ImportError (3 failed, surfaced asAPIConnectionError).
So every Vertex text and BGE embedding response would raise instead of returning, including responses that never had billable characters. The fix for the import is to use from litellm.types.utils import PromptTokensDetailsWrapper (it is already importable there; Usage is imported from that module at the top of the file).
Two smaller points, once the import is fixed:
response.get("metadata", {}).get("billableCharacterCount")raises AttributeError if Vertex sends"metadata": null, because.getreturns None rather than the default. I did not confirm that Vertex ever sends null there. It is cheap to guard.- There are a few trailing-whitespace lines in the added block.
Also, #46070 (opened later today by another contributor) targets the same issue #45999 with the same mapping to prompt_tokens_details.character_count. I did not run #46070, only read its diff. The maintainers will want to pick one.
- Import PromptTokensDetailsWrapper from litellm.types.utils (not base) - Guard against metadata: null to avoid AttributeError - Remove trailing whitespace Fixes review feedback from @VANDRANKI
|
Automatic reviews are disabled for this repository. |
|
@VANDRANKI Thank you for the thorough review! Fixed all three issues: ✅ Corrected import to The tests should now pass. Regarding #46070, I'll defer to the maintainers on which approach they prefer. |
VANDRANKI
left a comment
There was a problem hiding this comment.
Community re-review of db6656d (not a maintainer, does not clear the merge gate). I read the new commit and ran it on Python 3.13 with the local model cost map.
What I ran on this head:
tests/unit/llms/vertex_ai/test_vertex_text_embedding_billable_character_count.pyandtests/unit/llms/vertex_ai/test_bge_embedding.py: 6 passed. Before this commit the first file failed 3 of 3 and the BGE file 3 of 3 with the ImportError, so the import fix works.transform_vertex_response_to_openaiwith modeltext-embedding-005and 5 input tokens:"metadata": Nonereturns usage with 5 prompt tokens and no character count (no AttributeError),{"billableCharacterCount": 17}givescharacter_count17, and nometadatakey gives none.
One small thing is left. Compared with main, the diff still adds a whitespace-only line (8 spaces) just above setattr(model_response, "usage", usage), so the trailing whitespace is not fully gone. A formatter run would clear it. The new tests also have no case for "metadata": None, so the null guard is not pinned.
#46070 still targets the same issue, so a maintainer will need to pick one. I have not run #46070.
|
Closing since #47460 on main now maps Vertex billableCharacterCount into usage character_count. Reopen if it still happens or something is missing |
What happened?
Vertex AI text embedding responses include
metadata.billableCharacterCountfor character-priced models (text-embedding-005, gemini-embedding-001, text-multilingual-embedding-002), butVertexTextEmbeddingConfig.transform_vertex_response_to_openaionly summedtoken_countintoprompt_tokensand discarded the character count.The fix
Maps
metadata.billableCharacterCounttousage.prompt_tokens_details.character_countwhen present in the Vertex response, giving callers access to the provider-reported billable character count.Test coverage
Added unit tests in
tests/unit/llms/vertex_ai/test_vertex_text_embedding_billable_character_count.pycovering:Fixes #45999