diff --git a/src/promptflow-tracing/CHANGELOG.md b/src/promptflow-tracing/CHANGELOG.md index a313c1d328b..b840ed12e49 100644 --- a/src/promptflow-tracing/CHANGELOG.md +++ b/src/promptflow-tracing/CHANGELOG.md @@ -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. diff --git a/src/promptflow-tracing/promptflow/tracing/_trace.py b/src/promptflow-tracing/promptflow/tracing/_trace.py index 4ea13733750..ab53d6c986a 100644 --- a/src/promptflow-tracing/promptflow/tracing/_trace.py +++ b/src/promptflow-tracing/promptflow/tracing/_trace.py @@ -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