Skip to content

Commit

Permalink
Fixed TypeError in Tracing issue with tokens that are dicts (#3885)
Browse files Browse the repository at this point in the history
# Description

In the latest version of OpenAI some keys have tokens that are not ints.
This is causing TypeErrors like
`TypeError: unsupported operand type(s) for +: 'int' and 'dict'`
This aims to handle None types and Dicts better in tracing.

Should resolve bug #3834 

# All Promptflow Contribution checklist:
- [X] **The pull request does not introduce [breaking changes].**
- [X] **CHANGELOG is updated for new features, bug fixes or other
significant changes.**
- [X] **I have read the [contribution
guidelines](https://github.com/microsoft/promptflow/blob/main/CONTRIBUTING.md).**
- [X] **I confirm that all new dependencies are compatible with the MIT
license.**
- [X] **Create an issue and link to the pull request to get dedicated
review from promptflow team. Learn more: [suggested
workflow](../CONTRIBUTING.md#suggested-workflow).**

## General Guidelines and Best Practices
- [X] Title of the pull request is clear and informative.
- [X] There are a small number of commits, each of which have an
informative message. This means that previously merged commits do not
appear in the history of the PR. For more information on cleaning up the
commits in your PR, [see this
page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md).

### Testing Guidelines
- [X] Pull request includes test coverage for the included changes.
  • Loading branch information
KronemeyerJoshua authored Dec 17, 2024
1 parent 40c84b4 commit e8641e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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

0 comments on commit e8641e6

Please sign in to comment.