[chore] Remove exception handling around instrumentation code#148
[chore] Remove exception handling around instrumentation code#148lmolkova wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
⚠️ Not ready to approve
It changes exception propagation so instrumentation hook failures can mask original stream/user exceptions and also breaks the repository’s existing stream-wrapper test expectations that these hook errors are swallowed/logged.
Pull request overview
This PR updates the shared GenAI stream wrapper utilities (SyncStreamWrapper / AsyncStreamWrapper) to remove internal “safe” exception-handling paths around instrumentation hooks (chunk processing and stream finalization), based on the expectation that instrumentation code should not raise.
Changes:
- Replace calls to
_safe_finalize_success/_safe_finalize_failurewith direct_finalize_success/_finalize_failure. - Remove swallowing/logging behavior for exceptions raised by
_process_chunk,_on_stream_end, and_on_stream_error(and remove the helper methods that performed that logging).
File summaries
| File | Description |
|---|---|
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py | Removes “safe” wrappers around instrumentation hook execution in sync/async stream wrappers, changing how exceptions propagate during iteration/close/finalization. |
Copilot's findings
Comments suppressed due to low confidence (2)
util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py:126
_on_stream_end()exceptions can now escape from_finalize_success(), which can turn a normal end-of-stream into an unexpected exception (maskingStopIteration/StopAsyncIteration) and break the wrapper’s "instrumentation must not fail the app" contract (existing tests expect finalize errors to be swallowed).
Catch and debug-log exceptions from the instrumentation hook inside _finalize_success() to keep iteration semantics stable.
def _finalize_success(self) -> None:
if self._self_finalized:
return
self._self_finalized = True
self._on_stream_end()
util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py:232
- Async
_finalize_success()now directly calls_on_stream_end()without guarding against instrumentation exceptions. If_on_stream_end()raises, it can turn a clean end-of-stream into an exception (maskingStopAsyncIteration) and break the wrapper’s best-effort instrumentation behavior.
Catch and debug-log exceptions from _on_stream_end() inside _finalize_success().
def _finalize_success(self) -> None:
if self._self_finalized:
return
self._self_finalized = True
self._on_stream_end()
- Files reviewed: 1/1 changed files
- Comments generated: 4
Note
Your feedback helps us improve the quality of this feature.
Please use 👍 or 👎 to tell us whether this assessment is correct.
…va/opentelemetry-python-genai into defensive-code-in-streams
Remove
_safe_finalize_success,_safe_finalize_failuremethods and other places where we attempt to handle exceptions raised by instrumentation code.Instrumentation code is not expected to raise. We must write it in the way that it doesn't.