Skip to content

Commit

Permalink
refactor (backend): add default 0.0 value to resolve TypeError: unsup…
Browse files Browse the repository at this point in the history
…ported operand type(s) for 'float' and 'NoneType'
  • Loading branch information
aybruhm committed Nov 26, 2024
1 parent 88a18e6 commit 2f3469c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions agenta-backend/agenta_backend/services/aggregation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def aggregate_float(results: List[Result]) -> Result:
"""

try:
average_value = sum(result.value for result in results) / len(results)
average_value = sum(result.value or 0.0 for result in results) / len(results)
return Result(type="number", value=average_value)
except Exception as exc:
return Result(
Expand All @@ -90,7 +90,7 @@ def aggregate_float_from_llm_app_response(
raise ValueError("Key is required to aggregate InvokationResult objects.")

values = [
getattr(inv_result, key)
getattr(inv_result, key) or 0.0
for inv_result in invocation_results
if hasattr(inv_result, key) and getattr(inv_result, key) is not None
]
Expand All @@ -116,15 +116,18 @@ def sum_float_from_llm_app_response(
raise ValueError("Key is required to aggregate InvokationResult objects.")

values = [
getattr(inv_result, key)
getattr(inv_result, key) or 0.0
for inv_result in invocation_results
if hasattr(inv_result, key) and getattr(inv_result, key) is not None
]

if not values:
raise ValueError(f"No valid values found for {key} sum aggregation.")

total_value = sum(values)
total_value = sum(
values
) # sum([3.453, None]) -> TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

return Result(type=key, value=total_value)
except Exception as exc:
return Result(
Expand Down

0 comments on commit 2f3469c

Please sign in to comment.