forked from BerriAI/litellm
-
Notifications
You must be signed in to change notification settings - Fork 0
Responses usage fix #55
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
Open
lukesorvikDO
wants to merge
5
commits into
release/v1.94.0-do.2
Choose a base branch
from
lsorvik/responses-do.2-fix
base: release/v1.94.0-do.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
edd1afc
fix: preserve cache_creation fields in responses API usage transform
lukesorvikDO c290ec5
remove messages endpoint changes
lukesorvikDO 86a2185
fix: propagate cache tokens through streaming and non-streaming Anthr…
lukesorvikDO 3024cfa
revert: remove /v1/messages cache token changes from responses branch
lukesorvikDO 6d6117c
Adds fields so that litellm tests pass
lukesorvikDO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...lm/llms/anthropic/experimental_pass_through/responses_adapters/test_streaming_iterator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| """ | ||
| Tests for AnthropicResponsesStreamWrapper cache token usage mapping. | ||
|
|
||
| Bug: streaming_iterator.py assigned input_tokens_details (an object) to | ||
| cache_creation_tokens, and output_tokens_details (wrong field entirely) to | ||
| cache_read_tokens. Both caused streaming responses to report zero cache tokens. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| from unittest.mock import MagicMock | ||
|
|
||
| sys.path.insert(0, os.path.abspath("../../../../../../..")) | ||
|
|
||
| from litellm.llms.anthropic.experimental_pass_through.responses_adapters.streaming_iterator import ( | ||
| AnthropicResponsesStreamWrapper, | ||
| ) | ||
|
|
||
|
|
||
| def _make_completed_event(usage: MagicMock, status: str = "completed") -> MagicMock: | ||
| response_obj = MagicMock() | ||
| response_obj.status = status | ||
| response_obj.usage = usage | ||
| response_obj.output = [] | ||
|
|
||
| event = MagicMock() | ||
| event.type = "response.completed" | ||
| event.response = response_obj | ||
| return event | ||
|
|
||
|
|
||
| def _get_message_delta_usage(wrapper: AnthropicResponsesStreamWrapper) -> dict: | ||
| for chunk in wrapper._chunk_queue: | ||
| if chunk.get("type") == "message_delta": | ||
| return chunk["usage"] | ||
| raise AssertionError("no message_delta chunk was queued") | ||
|
|
||
|
|
||
| class TestStreamingCacheTokenMapping: | ||
| """response.completed -> message_delta usage cache token mapping.""" | ||
|
|
||
| def test_should_read_cache_creation_tokens_as_int_not_object(self): | ||
| """cache_creation_input_tokens must be an integer count, not input_tokens_details.""" | ||
| usage = MagicMock() | ||
| usage.input_tokens = 1936 | ||
| usage.output_tokens = 246 | ||
| usage.cache_creation_input_tokens = 1900 | ||
| usage.cache_read_input_tokens = 0 | ||
| usage.input_tokens_details = MagicMock(cached_tokens=0) | ||
|
|
||
| wrapper = AnthropicResponsesStreamWrapper(responses_stream=iter([]), model="m") | ||
| wrapper._process_event(_make_completed_event(usage)) | ||
|
|
||
| usage_delta = _get_message_delta_usage(wrapper) | ||
| assert usage_delta["cache_creation_input_tokens"] == 1900 | ||
| assert "cache_read_input_tokens" not in usage_delta | ||
|
|
||
| def test_should_read_cache_read_tokens_from_correct_field(self): | ||
| """cache_read_input_tokens must come from cache_read_input_tokens, not output_tokens_details.""" | ||
| usage = MagicMock() | ||
| usage.input_tokens = 1936 | ||
| usage.output_tokens = 246 | ||
| usage.cache_creation_input_tokens = 0 | ||
| usage.cache_read_input_tokens = 1900 | ||
| usage.input_tokens_details = MagicMock(cached_tokens=0) | ||
|
|
||
| wrapper = AnthropicResponsesStreamWrapper(responses_stream=iter([]), model="m") | ||
| wrapper._process_event(_make_completed_event(usage)) | ||
|
|
||
| usage_delta = _get_message_delta_usage(wrapper) | ||
| assert usage_delta["cache_read_input_tokens"] == 1900 | ||
| assert "cache_creation_input_tokens" not in usage_delta | ||
|
|
||
| def test_should_fall_back_to_input_tokens_details_cached_tokens(self): | ||
| """When cache_read_input_tokens is 0, fall back to input_tokens_details.cached_tokens.""" | ||
| usage = MagicMock() | ||
| usage.input_tokens = 1936 | ||
| usage.output_tokens = 246 | ||
| usage.cache_creation_input_tokens = 0 | ||
| usage.cache_read_input_tokens = 0 | ||
| usage.input_tokens_details = MagicMock(cached_tokens=1900) | ||
|
|
||
| wrapper = AnthropicResponsesStreamWrapper(responses_stream=iter([]), model="m") | ||
| wrapper._process_event(_make_completed_event(usage)) | ||
|
|
||
| usage_delta = _get_message_delta_usage(wrapper) | ||
| assert usage_delta["cache_read_input_tokens"] == 1900 | ||
|
|
||
| def test_should_not_add_cache_keys_when_no_cache_activity(self): | ||
| """No cache_* keys when there's no cache activity.""" | ||
| usage = MagicMock() | ||
| usage.input_tokens = 100 | ||
| usage.output_tokens = 50 | ||
| usage.cache_creation_input_tokens = 0 | ||
| usage.cache_read_input_tokens = 0 | ||
| usage.input_tokens_details = MagicMock(cached_tokens=0) | ||
|
|
||
| wrapper = AnthropicResponsesStreamWrapper(responses_stream=iter([]), model="m") | ||
| wrapper._process_event(_make_completed_event(usage)) | ||
|
|
||
| usage_delta = _get_message_delta_usage(wrapper) | ||
| assert "cache_creation_input_tokens" not in usage_delta | ||
| assert "cache_read_input_tokens" not in usage_delta | ||
| assert usage_delta["input_tokens"] == 100 | ||
| assert usage_delta["output_tokens"] == 50 | ||
|
|
||
| def test_should_handle_input_tokens_details_none(self): | ||
| """input_tokens_details=None should not raise when falling back.""" | ||
| usage = MagicMock() | ||
| usage.input_tokens = 100 | ||
| usage.output_tokens = 50 | ||
| usage.cache_creation_input_tokens = 0 | ||
| usage.cache_read_input_tokens = 0 | ||
| usage.input_tokens_details = None | ||
|
|
||
| wrapper = AnthropicResponsesStreamWrapper(responses_stream=iter([]), model="m") | ||
| wrapper._process_event(_make_completed_event(usage)) | ||
|
|
||
| usage_delta = _get_message_delta_usage(wrapper) | ||
| assert "cache_read_input_tokens" not in usage_delta |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You PR is adding four new tests in this file that asserts
translate_response()includes cache_creation_input_tokens / cache_read_input_tokens in Anthropic usage, but you are reverting the corresponding production changes inresponses_adapters/transformation.py, sotranslate_response()no longer sets those fields.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.
I see I will make a commit to fix
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.
Addressed in new commit ty