Skip to content
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

Fixed TypeError in Tracing issue with tokens that are dicts #3885

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/promptflow-tracing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# promptflow-tracing package

## v1.16.3 (2024.12.14)

- Fix token count issue when the value is None or it is a Dict

## v1.16.1 (2024.10.8)

- Fix token count issue when the value is None.
Expand Down
17 changes: 12 additions & 5 deletions src/promptflow-tracing/promptflow/tracing/_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,18 @@ def collect_openai_tokens_for_parent_span(self, span):
parent_span_id = span.parent.span_id
with self._lock:
if parent_span_id in self._span_id_to_tokens:
merged_tokens = {
# When token count is None for some reason, we should default to 0.
key: (self._span_id_to_tokens[parent_span_id].get(key, 0) or 0) + (tokens.get(key, 0) or 0)
for key in set(self._span_id_to_tokens[parent_span_id]) | set(tokens)
}
merged_tokens = {}
for key in set(self._span_id_to_tokens[parent_span_id]) | set(tokens):
parent_value = self._span_id_to_tokens[parent_span_id].get(key, 0)
token_value = tokens.get(key, 0)
if isinstance(parent_value, dict) and isinstance(token_value, dict):
# Handle the case where both values are dictionaries
merged_tokens[key] = {**parent_value, **token_value}
elif isinstance(parent_value, dict) or isinstance(token_value, dict):
# Handle the case where one value is a dictionary and the other is not
merged_tokens[key] = parent_value if isinstance(parent_value, dict) else token_value
else:
merged_tokens[key] = int(parent_value or 0) + int(token_value or 0)
self._span_id_to_tokens[parent_span_id] = merged_tokens
else:
self._span_id_to_tokens[parent_span_id] = tokens
Expand Down
Loading