Skip to content

Commit

Permalink
fix(openai): allow for none values in usage details
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp committed Jan 15, 2025
1 parent 40cb0e1 commit 4d47959
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions langfuse/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from inspect import isclass
from typing import Optional


import openai.resources
from openai._types import NotGiven
from packaging.version import Version
Expand Down Expand Up @@ -452,7 +451,29 @@ def _create_langfuse_update(
update["model"] = model

if usage is not None:
update["usage"] = usage
usage_dict = usage.copy() if isinstance(usage, dict) else usage.__dict__

if (
"prompt_tokens_details" in usage_dict
and usage_dict["prompt_tokens_details"] is not None
):
usage_dict["prompt_tokens_details"] = {
k: v
for k, v in usage_dict["prompt_tokens_details"].entries()
if v is not None
}

if (
"completion_tokens_details" in usage_dict
and usage_dict["completion_tokens_details"] is not None
):
usage_dict["completion_tokens_details"] = {
k: v
for k, v in usage_dict["completion_tokens_details"].entries()
if v is not None
}

update["usage"] = usage_dict

generation.update(**update)

Expand Down

0 comments on commit 4d47959

Please # to comment.