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

fix(bedrock): Check if the 'result' object exists before attempting to retrieve token data #2139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,20 @@ def _record_usage_to_span(span, prompt_tokens, completion_tokens, metric_params)
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
prompt_tokens,
)
_set_span_attribute(
span,
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
completion_tokens,
)
if completion_tokens:
_set_span_attribute(
span,
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
completion_tokens,
)

_set_span_attribute(
span,
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
prompt_tokens + completion_tokens,
prompt_tokens + completion_tokens if completion_tokens else prompt_tokens,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, we should guard this with an if prompt_tokens and completion tokens

)


metric_attributes = _metric_shared_attributes(metric_params.vendor, metric_params.model, metric_params.is_stream)

if metric_params.duration_histogram:
Expand Down Expand Up @@ -572,7 +575,8 @@ def _set_amazon_span_attributes(span, request_body, response_body, metric_params
_record_usage_to_span(
span,
response_body.get("inputTextTokenCount"),
sum(int(result.get("tokenCount")) for result in response_body.get("results")),
sum(int(result.get("tokenCount")) for result in response_body.get("results")) if response_body.get("results")
else None,
metric_params,
)

Expand Down