-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(openai): scope @dont_throw's protection to actually cover streami… #4485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nandithasalim
wants to merge
1
commit into
traceloop:main
Choose a base branch
from
nandithasalim:fix/streaming-dont-throw-generator-gap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+214
−98
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
65 changes: 65 additions & 0 deletions
65
...pentelemetry-instrumentation-openai/tests/traces/test_streaming_tracing_failure_safety.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """ | ||
| Regression test for the @dont_throw / generator gap. | ||
|
|
||
| _build_from_streaming_response (and its async twin) are generator functions. | ||
| @dont_throw only wraps the instant they're *called*, which just creates a | ||
| paused generator object and runs none of the body -- it never protects the | ||
| `for item in response:` loop, which only actually executes later, when the | ||
| caller iterates via `for chunk in stream:`. Before this fix, a real bug in | ||
| the tracing bookkeeping (_accumulate_stream_items) during that loop was | ||
| uncaught and crashed straight into the caller's own streaming loop, silently | ||
| dropping every remaining chunk of the real response -- despite the function | ||
| being decorated with @dont_throw. | ||
|
|
||
| This test forces exactly that: one chunk in the middle of the stream is | ||
| missing its "choices" key, which makes `for choice in item.get("choices"):` | ||
| inside _accumulate_stream_items raise TypeError (`NoneType not iterable`). | ||
| """ | ||
|
|
||
| import logging | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from opentelemetry.instrumentation.openai.shared.chat_wrappers import ( | ||
| _build_from_streaming_response, | ||
| ) | ||
|
|
||
|
|
||
| def test_tracing_failure_on_one_chunk_does_not_drop_later_chunks(monkeypatch, caplog): | ||
| # Skip the openai-v1 model_as_dict(...) conversion inside | ||
| # _accumulate_stream_items -- our fake chunks are plain dicts already. | ||
| monkeypatch.setattr( | ||
| "opentelemetry.instrumentation.openai.shared.chat_wrappers.is_openai_v1", | ||
| lambda: False, | ||
| ) | ||
|
|
||
| good_chunk_1 = { | ||
| "id": "c1", | ||
| "model": "gpt-4", | ||
| "choices": [{"index": 0, "delta": {"role": "assistant", "content": "Hel"}}], | ||
| } | ||
| # Malformed: no "choices" key at all -> item.get("choices") is None -> | ||
| # `for choice in None:` raises TypeError inside _accumulate_stream_items. | ||
| malformed_chunk = {"id": "c2", "model": "gpt-4"} | ||
| good_chunk_3 = { | ||
| "id": "c3", | ||
| "model": "gpt-4", | ||
| "choices": [{"index": 0, "delta": {"content": "lo"}}], | ||
| } | ||
|
|
||
| fake_response = [good_chunk_1, malformed_chunk, good_chunk_3] | ||
| fake_span = MagicMock() | ||
|
|
||
| gen = _build_from_streaming_response(fake_span, fake_response) | ||
|
|
||
| with caplog.at_level(logging.WARNING): | ||
| yielded = list(gen) | ||
|
|
||
| # The actual point of the fix: nothing gets dropped just because tracing | ||
| # broke on one chunk. All three real chunks still reach the caller, in order. | ||
| assert yielded == [good_chunk_1, malformed_chunk, good_chunk_3] | ||
|
|
||
| # The failure was logged, not silently swallowed and not left to crash. | ||
| assert any( | ||
| "failed to trace a streaming chunk" in record.getMessage() | ||
| for record in caplog.records | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: traceloop/openllmetry
Length of output: 11754
🏁 Script executed:
Repository: traceloop/openllmetry
Length of output: 12290
🏁 Script executed:
sed -n '120,155p' packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/utils.pyRepository: traceloop/openllmetry
Length of output: 1168
🏁 Script executed:
sed -n '132,180p' packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/utils.pyRepository: traceloop/openllmetry
Length of output: 1518
Wrap stream iteration and finalization in one outer
try/finally.All four builders iterate before the finalization
try/finally. An iterator exception, generatorclose()/aclose(), or cancellation can exit during iteration beforespan.end()runs. Keep source exceptions and cancellation unchanged.🤖 Prompt for AI Agents