The issue arises because SpanAttributes.LLM_TOKEN_COUNT_COMPLETION expects a specific type, and logging an incorrect value like [16] causes a TypeError. This error is not handled gracefully, leading to a silent failure.
To address this, you can modify the code to include error handling that logs a more informative message. Here鈥檚 a potential solution:
1. Modify the insert_span function to handle the TypeError and log an appropriate message.
```
import logging
async def insert_span(
session: AsyncSession,
span: Span,
project_name: str,
) -> Optional[SpanInsertionEvent]:
try:
# Existing code...
cumulative_llm_token_count_completion = cast(
int, get_attribute_value(span.attributes, SpanAttributes.LLM_TOKEN_COUNT_COMPLETION) or 0
)
if accumulation := (
await session.execute(
select(
func.sum(models.Span.cumulative_error_count),
func.sum(models.Span.cumulative_llm_token_count_prompt),
func.sum(models.Span.cumulative_llm_token_count_completion),
).where(models.Span.parent_id == span.context.span_id)
)
).first():
cumulative_error_count += cast(int, accumulation[0] or 0)
cumulative_llm_token_count_prompt += cast(int, accumulation[1] or 0)
cumulative_llm_token_count_completion += cast(int, accumulation[2] or 0)
# More existing code...
except TypeError as e:
logging.error(f"TypeError encountered: {e}. Check the value of SpanAttributes.LLM_TOKEN_COUNT_COMPLETION.")
raise
LBKSWould you like more details on how to implement this or any other aspect of Phoenix?
Sources:
- [https://github.com/Arize-ai/phoenix/issues/3188](https://github.com/Arize-ai/phoenix/issues/3188)
- [https://github.com/Arize-ai/phoenix/issues/2012](https://github.com/Arize-ai/phoenix/issues/2012)