Skip to content

Fix cancelled GenAI wrapper finalization - #656

Open
bhushanasati25 wants to merge 3 commits into
open-telemetry:mainfrom
bhushanasati25:fix/base-exception-finalization-646
Open

Fix cancelled GenAI wrapper finalization#656
bhushanasati25 wants to merge 3 commits into
open-telemetry:mainfrom
bhushanasati25:fix/base-exception-finalization-646

Conversation

@bhushanasati25

@bhushanasati25 bhushanasati25 commented Sep 8, 2026

Copy link
Copy Markdown

Fixes #646.

This widens telemetry-finalizing wrapper handlers from Exception to BaseException so cancellations such as asyncio.CancelledError fail and end invocations before being re-raised. It covers shared stream wrappers/managers plus OpenAI, Anthropic, Bedrock, Portkey, Qwen Agent, and Google GenAI hand-written wrapper paths.

Also updates instrumentation reviewer instructions to flag telemetry-finalizing except Exception handlers.

Known gaps: none.

@bhushanasati25
bhushanasati25 requested a review from a team as a code owner September 8, 2026 19:30
Copilot AI lite review requested due to automatic review settings September 8, 2026 19:30

Copilot AI 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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Fixes cancellation handling in GenAI telemetry wrappers so cancellations (e.g., asyncio.CancelledError) properly finalize spans/invocations before being re-raised.

Changes:

  • Broaden exception handling from Exception to BaseException in shared stream wrappers/managers to capture cancellation paths.
  • Update multiple vendor instrumentations (OpenAI/Anthropic/Portkey/Qwen Agent/Smolagents) to record failures on cancellation.
  • Add unit tests covering cancellation behavior and add changelog entries.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py Catch BaseException in stream wrapper/manager paths to ensure telemetry finalization on cancellation.
util/opentelemetry-util-genai/tests/test_stream.py Add cancellation-focused tests for stream wrappers and manager wrappers.
util/opentelemetry-util-genai/.changelog/656.fixed Document cancellation finalization behavior change for shared GenAI stream utilities.
instrumentation/opentelemetry-instrumentation-genai-smolagents/src/opentelemetry/instrumentation/genai/smolagents/patch.py Record failed invocations for cancellations while creating stream wrappers.
instrumentation/opentelemetry-instrumentation-genai-smolagents/.changelog/656.fixed Document Smolagents cancellation failure recording.
instrumentation/opentelemetry-instrumentation-genai-qwen-agent/src/opentelemetry/instrumentation/genai/qwen_agent/patch.py Record failed tool invocations for cancellations.
instrumentation/opentelemetry-instrumentation-genai-qwen-agent/.changelog/656.fixed Document Qwen Agent cancellation failure recording.
instrumentation/opentelemetry-instrumentation-genai-portkey/src/opentelemetry/instrumentation/genai/portkey/patch.py Record failed sync/async invocations for cancellations.
instrumentation/opentelemetry-instrumentation-genai-portkey/.changelog/656.fixed Document Portkey cancellation failure recording.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/patch.py Record failed OpenAI invocations for cancellations in chat/embeddings wrappers.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/patch_responses.py Record failed OpenAI responses invocations for cancellations (sync + async).
instrumentation/opentelemetry-instrumentation-genai-openai/tests/test_embedding_invocation_unit.py Add tests asserting cancellation is recorded and re-raised for embeddings (sync + async).
instrumentation/opentelemetry-instrumentation-genai-openai/.changelog/656.fixed Document OpenAI cancellation failure recording.
instrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/patch.py Record failed Anthropic invocations for cancellations (sync + async).
instrumentation/opentelemetry-instrumentation-genai-anthropic/.changelog/656.fixed Document Anthropic cancellation failure recording.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 82 to 87
_set_response_properties(chat_invocation, result, capture_content)
chat_invocation.stop()
return result
except Exception as error:
except BaseException as error:
chat_invocation.fail(error)
raise
Comment on lines 170 to 175
def close(self) -> None:
try:
self._self_stream.close()
except Exception as error:
except BaseException as error:
self._finalize_failure(error)
raise
Comment on lines +295 to +298
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
assert span.attributes["error.type"] == "asyncio.exceptions.CancelledError"

@lmolkova lmolkova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks for working on this!

it seems there is still except Exception in bedrock and google-genai - could you please update them too?

And please update agents.md in instrumentation folder and copilot review instructions for instrumentations to suggest BaseException and to flag narrow ones.

@bhushanasati25
bhushanasati25 force-pushed the fix/base-exception-finalization-646 branch from dba8394 to 379ffca Compare September 9, 2026 04:36
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 9, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-09-09 04:38 UTC

Respond to 4 review items (e.g. link a commit, explain why not, ask a follow-up):

  • Inline threads: 1, 2, 3
  • Top-level threads: 4
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Follow-up to #520: except Exception wrappers still leak spans on cancellation

3 participants