AgentOps-AI / AgentOps-AI/agentops

TokenUsageExtractor drops mapping-based usage metadata

Đang mở
#1,432 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
5.8k
Fork
619
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

## Description

`TokenUsageExtractor.extract_from_response()` silently drops all token counts when a provider exposes `usage` or `usage_metadata` as a mapping instead of an attribute-based object.

The shared streaming wrapper explicitly accepts chunks with `usage_metadata`, and the existing test fixture uses a dictionary shape, but `_extract_from_usage_object()` reads every field with `getattr()`. As a result, dictionary-backed usage produces a `TokenUsage` whose fields are all `None`, so the span receives no token usage attributes.

## Minimal reproduction

```python
from types import SimpleNamespace

from agentops.instrumentation.common.token_counting import TokenUsageExtractor

response = SimpleNamespace(
usage_metadata={
prompt_tokens: 10,
completion_tokens: 5,
total_tokens: 15,
}
)

print(TokenUsageExtractor.extract_from_response(response))
```

Current output on `main`:

```text
TokenUsage(prompt_tokens=None, completion_tokens=None, total_tokens=None, cached_prompt_tokens=None, cached_read_tokens=None, reasoning_tokens=None)
```

## Expected behavior

Dictionary and attribute-based usage containers should be normalized consistently, including the cache and reasoning token fields already supported by `TokenUsage`.

## Suggested fix

Use a small mapping-aware field accessor in `_extract_from_usage_object()` and add regression coverage for both `usage` and `usage_metadata` mappings. The existing object behavior should remain unchanged.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.