Skip to content

Commit b396671

Browse files
author
Mateusz
committed
fix(types): widen UsageNormalizer signatures to match actual runtime behavior
- Add UsageSummary to normalize() parameter type (was handled at runtime but not declared in the signature) - Change merge_streaming_usage() params to accept None and dict[str, Any] (was handling None at runtime but typed as dict[str, int]) - Change merge_streaming_usage() return type to dict[str, Any] to reflect that it preserves nested dicts (completion_tokens_details, cost, etc.) - Remove unreachable isinstance(usage, dict) guard after type narrowing
1 parent b8193f3 commit b396671

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

src/core/transport/fastapi/adapters/usage/normalizer.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
if TYPE_CHECKING:
99
from src.core.domain.openrouter_usage import OpenRouterUsage
10+
from src.core.domain.usage_summary import UsageSummary
1011
from src.core.services.usage_calculation_service import UsageCalculationService
1112

1213
logger = logging.getLogger(__name__)
@@ -32,12 +33,13 @@ def __init__(
3233
self._usage_service = usage_service
3334

3435
def normalize(
35-
self, usage: dict[str, Any] | OpenRouterUsage | None
36+
self,
37+
usage: dict[str, Any] | OpenRouterUsage | UsageSummary | None,
3638
) -> dict[str, int]:
3739
"""Normalize usage to standard format.
3840
3941
Args:
40-
usage: Usage dictionary, OpenRouterUsage instance, or None
42+
usage: Usage dictionary, OpenRouterUsage, UsageSummary, or None
4143
4244
Returns:
4345
Normalized usage with standard fields as integers
@@ -61,13 +63,6 @@ def normalize(
6163
if isinstance(usage, UsageSummary):
6264
usage = usage.to_legacy_dict()
6365

64-
if not isinstance(usage, dict):
65-
return {
66-
"prompt_tokens": 0,
67-
"completion_tokens": 0,
68-
"total_tokens": 0,
69-
}
70-
7166
usage = dict(usage)
7267

7368
if "prompt_tokens" not in usage and "input_tokens" in usage:
@@ -180,13 +175,15 @@ def _ensure_total_valid(self, usage: dict[str, Any]) -> dict[str, int]:
180175
return usage
181176

182177
def merge_streaming_usage(
183-
self, existing: dict[str, int], new: dict[str, Any]
184-
) -> dict[str, int]:
178+
self,
179+
existing: dict[str, Any] | None,
180+
new: dict[str, Any] | None,
181+
) -> dict[str, Any]:
185182
"""Merge usage keeping highest values.
186183
187184
Args:
188-
existing: Existing usage dictionary
189-
new: New usage dictionary to merge
185+
existing: Existing usage dictionary or None
186+
new: New usage dictionary to merge or None
190187
191188
Returns:
192189
Merged usage dictionary with highest values

tests/unit/transport/fastapi/adapters/usage/test_usage_normalizer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ def test_responses_api_input_tokens_mapped_to_prompt_tokens(self):
177177
def test_responses_api_only_output_tokens_mapped(self):
178178
"""If only output_tokens present (no prompt_tokens), it maps correctly."""
179179
normalizer = UsageNormalizer()
180-
result = normalizer.normalize(
181-
{"output_tokens": 15, "total_tokens": 57}
182-
)
180+
result = normalizer.normalize({"output_tokens": 15, "total_tokens": 57})
183181
assert result["prompt_tokens"] == 0
184182
assert result["completion_tokens"] == 15
185183
assert result["total_tokens"] == 57

0 commit comments

Comments
 (0)