Skip to content

Skip Azure content filter chunks in OpenAI streams - #2763

Merged
alexmojaki merged 2 commits into
pydantic:mainfrom
anubhavlal07:fix/openai-azure-stream-chunks
Nov 10, 2026
Merged

alexmojaki merged 2 commits into
pydantic:mainfrom
anubhavlal07:fix/openai-azure-stream-chunks

Conversation

@anubhavlal07

@anubhavlal07 anubhavlal07 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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 no delta.

OpenaiChatCompletionStreamState.record_chunk passes every chunk to the SDK's ChatCompletionStreamState. The first chunk it sees becomes the completion snapshot, and later chunks never update its id or model. So for an Azure stream, the streaming response log records gen_ai.response.id as an empty string. gen_ai.response.model is missing, so the processor falls back to the request model, which on Azure is the deployment name. operation.cost is missing because the price lookup has no model. The SDK's own client.chat.completions.stream() helper already skips these chunks with _is_valid_chat_completion_chunk_weak (it checks object == "chat.completion.chunk"), so only create(stream=True) under Logfire was affected.

Repro

import json
import httpx, openai, logfire

logfire.configure(send_to_logfire=False)

chunks = [
    {'id': '', 'object': '', 'created': 0, 'model': '', 'choices': [], 'prompt_filter_results': [{'prompt_index': 0, 'content_filter_results': {}}]},
    {'id': 'chatcmpl-1', 'object': 'chat.completion.chunk', 'created': 1, 'model': 'gpt-4o-mini-2024-07-18', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': 'Nine'}, 'finish_reason': None}]},
    {'id': 'chatcmpl-1', 'object': 'chat.completion.chunk', 'created': 1, 'model': 'gpt-4o-mini-2024-07-18', 'choices': [{'index': 0, 'delta': {}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 10, 'completion_tokens': 1, 'total_tokens': 11}},
]
sse = ''.join(f'data: {json.dumps(c)}\n\n' for c in chunks) + 'data: [DONE]\n\n'
client = openai.OpenAI(api_key='x', http_client=httpx.Client(transport=httpx.MockTransport(
    lambda r: httpx.Response(200, text=sse, headers={'content-type': 'text/event-stream'}))))
logfire.instrument_openai(client)
list(client.chat.completions.create(model='my-deployment', messages=[{'role': 'user', 'content': 'hi'}], stream=True))

On main the streaming log has gen_ai.response.id == '', gen_ai.response.model == 'my-deployment' and no operation.cost. If you remove the first chunk, the same stream records chatcmpl-1, gpt-4o-mini-2024-07-18 and a cost.

Fix

record_chunk now skips chunks whose object is an empty string, the same chunks the SDK's stream helper skips. A chunk without an object field is still recorded, so OpenAI-compatible providers that leave the field out behave as before.

Test

test_azure_content_filter_chunks_ignored_in_stream streams an Azure-shaped response: a leading prompt_filter_results chunk, 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

  • The new test fails on main (assert '' == 'chatcmpl-1') and passes with the fix.
  • tests/otel_integrations/test_openai.py and test_openai_stream_errors.py pass (101 tests).
  • ruff format, ruff check and pyright are clean on the changed files.

This change was prepared with the help of an AI assistant (Claude), and I reviewed and tested it.

View guided diff

@veria-ai

veria-ai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread tests/otel_integrations/test_openai.py Resolved
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/_internal/integrations/llm_providers
  openai.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki
alexmojaki added this pull request to the merge queue Nov 10, 2026
Merged via the queue into pydantic:main with commit 76b88d1 Nov 10, 2026
32 checks passed
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.

2 participants