Skip to content

fix(models): request stream usage for Azure OpenAI - #1541

Open
pcbeingused33 wants to merge 1 commit into
dataease:mainfrom
pcbeingused33:fix/azure-stream-usage
Open

pcbeingused33 wants to merge 1 commit into
dataease:mainfrom
pcbeingused33:fix/azure-stream-usage

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Fixes token usage not being recorded for Azure OpenAI models. Follow-up to #1538, which did the same for the OpenAI-compatible client.

Root cause

OpenAIAzureLLM builds AzureChatOpenAI with streaming=True but without stream_usage. With langchain-openai 0.3, the request only includes stream_options: {"include_usage": true} when stream_usage is set, so Azure never returns a usage chunk while streaming. get_token_usage in apps/chat/task/llm.py reads chunk.usage_metadata, so token counts for Azure models stay empty. OpenAILLM already defaults stream_usage to True.

Fix

Default stream_usage to True for Azure, the same way OpenAILLM does, and let an explicit stream_usage model parameter override it. It is merged into the keyword arguments, so a configured value can't trigger a duplicate-keyword error.

Test

tests/test_model_validation_parameters.py::test_azure_defaults_stream_usage_without_overriding_configuration covers the default plus explicit True/False and checks that api_version/deployment_name are still passed. Without the fix, the default case fails. Checked against the real AzureChatOpenAI from langchain-openai 0.3 with a mocked HTTP transport: before the fix the request had no stream_options; after it, the request includes {"include_usage": true}.

AzureChatOpenAI was created with streaming=True but without stream_usage,
so streamed responses never asked for usage and token counts stayed empty.
Default stream_usage to true like OpenAILLM, letting an explicit model
parameter override it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant