-
Notifications
You must be signed in to change notification settings - Fork 248
fix(google-genai): include cached_content_token_count in streaming responses #3177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5a4ea26
6e50bdc
4824395
38d85ee
efeb348
a091b57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -625,6 +625,14 @@ def _convert_google_chunk_to_streaming_chunk( | |||||||||||||||||||
| if usage_metadata and hasattr(usage_metadata, "thoughts_token_count") and usage_metadata.thoughts_token_count: | ||||||||||||||||||||
| usage["thoughts_token_count"] = usage_metadata.thoughts_token_count | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Add cached content token count if available (context caching) | ||||||||||||||||||||
| if ( | ||||||||||||||||||||
| usage_metadata | ||||||||||||||||||||
| and hasattr(usage_metadata, "cached_content_token_count") | ||||||||||||||||||||
| and usage_metadata.cached_content_token_count | ||||||||||||||||||||
| ): | ||||||||||||||||||||
| usage["cached_content_token_count"] = usage_metadata.cached_content_token_count | ||||||||||||||||||||
|
||||||||||||||||||||
| if ( | |
| usage_metadata | |
| and hasattr(usage_metadata, "cached_content_token_count") | |
| and usage_metadata.cached_content_token_count | |
| ): | |
| usage["cached_content_token_count"] = usage_metadata.cached_content_token_count | |
| cached_content_token_count = getattr(usage_metadata, "cached_content_token_count", None) if usage_metadata else None | |
| if cached_content_token_count is not None: | |
| usage["cached_content_token_count"] = cached_content_token_count |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -703,6 +703,72 @@ def test_aggregate_streaming_chunks_with_thought_signatures_and_thinking_tokens( | |||||||||||||||
| assert result.meta["thought_signatures"][0]["signature"] == "sig_xyz" | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def test_convert_google_chunk_to_streaming_chunk_with_cached_tokens(self, monkeypatch): | ||||||||||||||||
| """cached_content_token_count from usage_metadata is included in the streaming chunk's usage.""" | ||||||||||||||||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||||||||||||||||
| component_info = ComponentInfo.from_component(GoogleGenAIChatGenerator()) | ||||||||||||||||
|
|
||||||||||||||||
| mock_usage = Mock() | ||||||||||||||||
| mock_usage.prompt_token_count = 1000 | ||||||||||||||||
| mock_usage.candidates_token_count = 10 | ||||||||||||||||
| mock_usage.total_token_count = 1010 | ||||||||||||||||
| mock_usage.thoughts_token_count = None | ||||||||||||||||
| mock_usage.cached_content_token_count = 800 | ||||||||||||||||
|
|
||||||||||||||||
| mock_part = Mock() | ||||||||||||||||
| mock_part.text = "The answer is 4." | ||||||||||||||||
| mock_part.function_call = None | ||||||||||||||||
| mock_part.thought = False | ||||||||||||||||
| mock_part.thought_signature = None | ||||||||||||||||
| mock_content = Mock() | ||||||||||||||||
| mock_content.parts = [mock_part] | ||||||||||||||||
| mock_candidate = Mock() | ||||||||||||||||
| mock_candidate.content = mock_content | ||||||||||||||||
| mock_candidate.finish_reason = "STOP" | ||||||||||||||||
|
|
||||||||||||||||
| mock_chunk = Mock() | ||||||||||||||||
| mock_chunk.candidates = [mock_candidate] | ||||||||||||||||
| mock_chunk.usage_metadata = mock_usage | ||||||||||||||||
|
|
||||||||||||||||
| chunk = _convert_google_chunk_to_streaming_chunk(mock_chunk, 0, component_info, "gemini-2.5-flash") | ||||||||||||||||
|
||||||||||||||||
| chunk = _convert_google_chunk_to_streaming_chunk(mock_chunk, 0, component_info, "gemini-2.5-flash") | |
| chunk = _convert_google_chunk_to_streaming_chunk( | |
| chunk=mock_chunk, | |
| candidate_index=0, | |
| component_info=component_info, | |
| model="gemini-2.5-flash", | |
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the changes made here, the Changelog is generated automatically whenever we release a new version.