[GH-ISSUE #23927] fix: Inject stream_options for Anthropic OpenAI-compat endpoint to include usage in SSE #35642

Closed
opened 2026-04-25 09:48:17 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @pvyswiss on GitHub (Apr 21, 2026).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/23927

Bug Description

When streaming from Anthropic's OpenAI-compatible endpoint without stream_options: {"include_usage": true}, the final SSE chunk contains no usage field. The middleware's normalize_usage finds nothing.

Related to: #23917 (Bug 7)

Root Cause

Anthropic's /v1/chat/completions supports stream_options but only returns usage when explicitly requested. Open WebUI doesn't inject it -- relies on the client. The Web UI browser sends it, but API scripts and plugins don't.

Fix

if (
    is_anthropic_url(url)
    and isinstance(payload, dict)
    and payload.get('stream')
):
    payload.setdefault('stream_options', {'include_usage': True})

setdefault preserves client-provided values.

Impact

  • Web UI: None (already sends stream_options)
  • API scripts / curl: Usage now included in SSE stream

File

backend/open_webui/routers/openai.py

Originally created by @pvyswiss on GitHub (Apr 21, 2026). Original GitHub issue: https://github.com/open-webui/open-webui/issues/23927 ## Bug Description When streaming from Anthropic's OpenAI-compatible endpoint without `stream_options: {"include_usage": true}`, the final SSE chunk contains no `usage` field. The middleware's `normalize_usage` finds nothing. Related to: #23917 (Bug 7) ## Root Cause Anthropic's `/v1/chat/completions` supports `stream_options` but only returns usage when explicitly requested. Open WebUI doesn't inject it -- relies on the client. The Web UI browser sends it, but API scripts and plugins don't. ## Fix ```python if ( is_anthropic_url(url) and isinstance(payload, dict) and payload.get('stream') ): payload.setdefault('stream_options', {'include_usage': True}) ``` `setdefault` preserves client-provided values. ## Impact - **Web UI**: None (already sends `stream_options`) - **API scripts / curl**: Usage now included in SSE stream ## File `backend/open_webui/routers/openai.py`
Author
Owner

@tjbck commented on GitHub (Apr 24, 2026):

Intended behaviour.

<!-- gh-comment-id:4311324361 --> @tjbck commented on GitHub (Apr 24, 2026): Intended behaviour.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#35642