mirror of
https://github.com/open-webui/open-webui.git
synced 2026-04-29 17:06:40 -05:00
fix: persist token usage data for non-streaming chat responses (#22166)
The non-streaming response handler was saving assistant messages without their usage/token data. While the streaming handler correctly extracted and saved usage information, the non-streaming path discarded it entirely. This caused assistant messages from non-streaming completions to have NULL usage in the chat_message table, making them invisible to the analytics token aggregation queries and contributing to the '0 tokens' display in Admin Panel Analytics. Extract and normalize the usage data from the API response and include it in the database upsert, matching the pattern already used by the streaming handler.
This commit is contained in:
@@ -3093,6 +3093,9 @@ async def non_streaming_chat_response_handler(response, ctx):
|
||||
)
|
||||
|
||||
# Save message in the database
|
||||
raw_usage = response_data.get("usage", {}) or {}
|
||||
usage = normalize_usage(raw_usage) if raw_usage else None
|
||||
|
||||
Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
metadata["chat_id"],
|
||||
metadata["message_id"],
|
||||
@@ -3100,6 +3103,7 @@ async def non_streaming_chat_response_handler(response, ctx):
|
||||
"role": "assistant",
|
||||
"content": content,
|
||||
"output": response_output,
|
||||
**({"usage": usage} if usage else {}),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user