Skip to content

Commit 79eb901

Browse files
authored
fix(langchain): take only int values in parsed usage (#1299)
1 parent e286441 commit 79eb901

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

langfuse/langchain/CallbackHandler.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -875,22 +875,41 @@ def _parse_usage_model(usage: typing.Union[pydantic.BaseModel, dict]) -> Any:
875875
usage_model = cast(Dict, usage.copy()) # Copy all existing key-value pairs
876876

877877
# Skip OpenAI usage types as they are handled server side
878-
if not all(
879-
openai_key in usage_model
880-
for openai_key in ["prompt_tokens", "completion_tokens", "total_tokens"]
878+
if (
879+
all(
880+
openai_key in usage_model
881+
for openai_key in [
882+
"prompt_tokens",
883+
"completion_tokens",
884+
"total_tokens",
885+
"prompt_tokens_details",
886+
"completion_tokens_details",
887+
]
888+
)
889+
and len(usage_model.keys()) == 5
890+
) or (
891+
all(
892+
openai_key in usage_model
893+
for openai_key in [
894+
"prompt_tokens",
895+
"completion_tokens",
896+
"total_tokens",
897+
]
898+
)
899+
and len(usage_model.keys()) == 3
881900
):
882-
for model_key, langfuse_key in conversion_list:
883-
if model_key in usage_model:
884-
captured_count = usage_model.pop(model_key)
885-
final_count = (
886-
sum(captured_count)
887-
if isinstance(captured_count, list)
888-
else captured_count
889-
) # For Bedrock, the token count is a list when streamed
890-
891-
usage_model[langfuse_key] = (
892-
final_count # Translate key and keep the value
893-
)
901+
return usage_model
902+
903+
for model_key, langfuse_key in conversion_list:
904+
if model_key in usage_model:
905+
captured_count = usage_model.pop(model_key)
906+
final_count = (
907+
sum(captured_count)
908+
if isinstance(captured_count, list)
909+
else captured_count
910+
) # For Bedrock, the token count is a list when streamed
911+
912+
usage_model[langfuse_key] = final_count # Translate key and keep the value
894913

895914
if isinstance(usage_model, dict):
896915
if "input_token_details" in usage_model:
@@ -965,7 +984,7 @@ def _parse_usage_model(usage: typing.Union[pydantic.BaseModel, dict]) -> Any:
965984
if "input" in usage_model:
966985
usage_model["input"] = max(0, usage_model["input"] - value)
967986

968-
usage_model = {k: v for k, v in usage_model.items() if not isinstance(v, str)}
987+
usage_model = {k: v for k, v in usage_model.items() if isinstance(v, int)}
969988

970989
return usage_model if usage_model else None
971990

0 commit comments

Comments
 (0)