fix(openai): handle Azure stream chunks without delta#1648
Merged
Conversation
|
@claude review |
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.
Summary
Fixes Azure OpenAI streaming traces when Azure emits a trailing content-filter chunk whose chat choice has
delta=None.The OpenAI stream extractor previously dereferenced
delta.__dict__unconditionally for OpenAI v1 responses. In Azure deployments with custom content filters, the trailing metadata-only chunk can have no delta, causing final stream extraction to fail internally. Because finalization catches extraction errors, the generation span ended without assistant output or usage details.This PR:
delta=Noneas an empty delta while continuing to process prior streamed contentusageandfinish_reasonacross trailing metadata-only chunksVerification
uv run --frozen pytest tests/unit/test_openai.py -quv run --frozen ruff check langfuse/openai.py tests/unit/test_openai.pyuv run --frozen ruff format --check langfuse/openai.py tests/unit/test_openai.pyuv run --frozen mypy langfuse --no-error-summaryDisclaimer: Experimental PR review
Greptile Summary
Fixes a crash in Azure OpenAI streaming when Azure emits a trailing content-filter chunk with
delta=Noneandfinish_reason=None, which previously caused finalization to produce spans without output or usage data.delta is not Noneguard before callingdelta.__dict__for OpenAI v1 objects, then falls back to an empty dict so downstream logic runs safely.usageandfinish_reasonaccumulation to a "last non-null wins" pattern, preserving values from earlier normal chunks when a trailing metadata-only chunk clears them.delta=None).Confidence Score: 5/5
Safe to merge — the fix is narrowly scoped to the stream extractor, does not alter any public API or data model, and is covered by a direct regression test.
The change is a minimal three-part defensive fix in one function, each part independently safe: a null guard on delta before dict conversion, an empty-dict fallback so the rest of the loop body runs unchanged, and a non-null-only assignment pattern for usage and finish_reason. The regression test exercises exactly the failing scenario end-to-end and checks all three affected outputs (content, finish_reason, usage).
No files require special attention.
Sequence Diagram
sequenceDiagram participant Azure as Azure OpenAI API participant Extractor as _extract_streamed_openai_response participant Span as Langfuse Span Azure->>Extractor: "chunk 1 — delta with content, finish_reason=None, usage=None" Note over Extractor: model set, content accumulated Azure->>Extractor: "chunk 2 — delta empty, finish_reason="stop", usage={tokens}" Note over Extractor: finish_reason preserved (non-null), usage preserved (non-null) Azure->>Extractor: "chunk 3 — delta=None, finish_reason=None, usage=None (content-filter)" Note over Extractor: delta=None → skip __dict__ → treat as {}<br/>finish_reason=None → keep "stop"<br/>usage=None → keep prior usage Extractor->>Span: "finalize with content, finish_reason="stop", usage={tokens}"Reviews (1): Last reviewed commit: "fix(openai): handle Azure stream chunks ..." | Re-trigger Greptile