Skip to content

Commit 886e3b7

Browse files
committed
fix: coerce None token fields to 0 in OpenAIHelper.get_ai_usage_from_response; update LangChain README example
1 parent 41b660d commit 886e3b7

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

packages/ai-providers/server-ai-openai/src/ldai_openai/openai_helper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def get_ai_usage_from_response(response: Any) -> Optional[TokenUsage]:
3838
if hasattr(response, 'usage') and response.usage is not None:
3939
u = response.usage
4040
usage = TokenUsage(
41-
total=getattr(u, 'total_tokens', 0),
42-
input=getattr(u, 'prompt_tokens', 0),
43-
output=getattr(u, 'completion_tokens', 0),
41+
total=getattr(u, 'total_tokens', None) or 0,
42+
input=getattr(u, 'prompt_tokens', None) or 0,
43+
output=getattr(u, 'completion_tokens', None) or 0,
4444
)
4545
if usage is not None and usage.total == 0 and usage.input == 0 and usage.output == 0:
4646
return None

0 commit comments

Comments
 (0)