Repository navigation
Skip Azure content filter chunks in OpenAI streams - #2763
Merged
alexmojaki merged 2 commits intoNov 10, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Tip: cubic used a learning from your PR history. Let your coding agent read cubic learnings directly with the cubic MCP.
View guided diff | Re-trigger cubic
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Skip Azure OpenAI content filter chunks when recording chat completion streams
Azure OpenAI starts every streamed chat completion with a chunk that carries only
prompt_filter_results. That chunk has"object": "","id": "","model": ""and no choices. With the asynchronous content filter turned on, Azure also sends more of these chunks during the stream, with content filter results but nodelta.OpenaiChatCompletionStreamState.record_chunkpasses every chunk to the SDK'sChatCompletionStreamState. The first chunk it sees becomes the completion snapshot, and later chunks never update itsidormodel. So for an Azure stream, the streaming response log recordsgen_ai.response.idas an empty string.gen_ai.response.modelis missing, so the processor falls back to the request model, which on Azure is the deployment name.operation.costis missing because the price lookup has no model. The SDK's ownclient.chat.completions.stream()helper already skips these chunks with_is_valid_chat_completion_chunk_weak(it checksobject == "chat.completion.chunk"), so onlycreate(stream=True)under Logfire was affected.Repro
On main the streaming log has
gen_ai.response.id == '',gen_ai.response.model == 'my-deployment'and nooperation.cost. If you remove the first chunk, the same stream recordschatcmpl-1,gpt-4o-mini-2024-07-18and a cost.Fix
record_chunknow skips chunks whoseobjectis an empty string, the same chunks the SDK's stream helper skips. A chunk without anobjectfield is still recorded, so OpenAI-compatible providers that leave the field out behave as before.Test
test_azure_content_filter_chunks_ignored_in_streamstreams an Azure-shaped response: a leadingprompt_filter_resultschunk, two content chunks, a usage chunk, and a trailing asynchronous filter chunk. It checks the response id, served model, finish reasons, token usage, cost and output messages on the streaming log.Results
assert '' == 'chatcmpl-1') and passes with the fix.tests/otel_integrations/test_openai.pyandtest_openai_stream_errors.pypass (101 tests).ruff format,ruff checkandpyrightare clean on the changed files.This change was prepared with the help of an AI assistant (Claude), and I reviewed and tested it.