Skip to content

fix(vertex-ai): map billableCharacterCount to usage.prompt_tokens_details.character_count - #46028

Closed
aishwary-dongre wants to merge 2 commits into
BerriAI:mainfrom
aishwary-dongre:bugfix/vertex-ai-text-embedding-billable-character-count
Closed

aishwary-dongre wants to merge 2 commits into
BerriAI:mainfrom
aishwary-dongre:bugfix/vertex-ai-text-embedding-billable-character-count

Conversation

@aishwary-dongre

Copy link
Copy Markdown

What happened?

Vertex AI text embedding responses include metadata.billableCharacterCount for character-priced models (text-embedding-005, gemini-embedding-001, text-multilingual-embedding-002), but VertexTextEmbeddingConfig.transform_vertex_response_to_openai only summed token_count into prompt_tokens and discarded the character count.

The fix

Maps metadata.billableCharacterCount to usage.prompt_tokens_details.character_count when 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.py covering:

  • ✅ Character count mapping when metadata is present
  • ✅ No prompt_tokens_details when metadata is missing
  • ✅ No prompt_tokens_details when billableCharacterCount is absent

Fixes #45999

…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
@codspeed

codspeed Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing aishwary-dongre:bugfix/vertex-ai-text-embedding-billable-character-count (db6656d) with main (e136e91)

Open in CodSpeed

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@VANDRANKI VANDRANKI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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" gives ImportError: 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 model text-embedding-005: on current main it returns a Usage (with and without metadata). 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_model check, so it also runs for BGE models. tests/unit/llms/vertex_ai/test_bge_embedding.py passes on main (3 passed) and fails on this branch with the same ImportError (3 failed, surfaced as APIConnectionError).

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 .get returns 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
@greptile-apps

greptile-apps Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Automatic reviews are disabled for this repository.

@aishwary-dongre

Copy link
Copy Markdown
Author

@VANDRANKI Thank you for the thorough review! Fixed all three issues:

✅ Corrected import to from litellm.types.utils import PromptTokensDetailsWrapper
✅ Guarded against metadata: null with response.get("metadata") or {}
✅ Removed trailing whitespace

The tests should now pass. Regarding #46070, I'll defer to the maintainers on which approach they prefer.

@VANDRANKI VANDRANKI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.py and tests/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_openai with model text-embedding-005 and 5 input tokens: "metadata": None returns usage with 5 prompt tokens and no character count (no AttributeError), {"billableCharacterCount": 17} gives character_count 17, and no metadata key 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.

@mateo-berri

Copy link
Copy Markdown
Contributor

Closing since #47460 on main now maps Vertex billableCharacterCount into usage character_count. Reopen if it still happens or something is missing

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.

[Bug]: Vertex AI text embeddings drop metadata.billableCharacterCount

3 participants