Skip to content

fix(vertexai): make dont_throw work on async functions - #4415

Open
chiruu12 wants to merge 3 commits into
traceloop:mainfrom
chiruu12:fix/vertexai-dont-throw-async
Open

fix(vertexai): make dont_throw work on async functions#4415
chiruu12 wants to merge 3 commits into
traceloop:mainfrom
chiruu12:fix/vertexai-dont-throw-async

Conversation

@chiruu12

@chiruu12 chiruu12 commented Aug 12, 2026

Copy link
Copy Markdown

dont_throw in the VertexAI instrumentation defines only a synchronous wrapper. Calling an async def returns a coroutine immediately, so the try block exits before the function body runs and the caller awaits the coroutine outside the guard. The decorator has no effect on async functions.

It is applied to async functions in this package: span_utils.set_input_attributes and _handle_request. _handle_request is awaited at __init__.py with no surrounding try, and before the wrapped model call, so an instrumentation error in async prompt capture propagates into the user's application and the LLM call never happens.

Before:

await async_boom() -> RuntimeError: instrumentation failed

After:

await async_boom() -> None
await async_ok()   -> ok

The Anthropic, OpenAI and google-generativeai packages already branch on asyncio.iscoroutinefunction and return an async wrapper. This copies that shape into VertexAI.

Cohere has the same gap, but its async streaming entry points are async generators, which need a third branch. Left out to keep this focused.

Tests added in tests/test_dont_throw.py cover both wrappers, the exception path and the return-value path. They fail on main for the async cases.

Fixes #4414

Summary by CodeRabbit

  • Bug Fixes

    • Improved exception handling for asynchronous operations so errors are suppressed and logged consistently with synchronous operations.
    • Preserved return values for both synchronous and asynchronous operations.
    • Ensured errors from optional error reporting do not interrupt application behavior.
  • Tests

    • Added coverage for asynchronous exception handling, return values, wrapper selection, and resilient error reporting across supported operation types.

Copilot AI lite review requested due to automatic review settings August 12, 2026 20:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CLAassistant

CLAassistant commented Aug 12, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: db987bf1-c29d-451a-9edd-d3e566c75d12

📥 Commits

Reviewing files that changed from the base of the PR and between 690254a and ba78b62.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py

📝 Walkthrough

Walkthrough

The VertexAI dont_throw decorator now supports coroutine functions. It awaits asynchronous functions, suppresses their exceptions, and preserves synchronous and asynchronous return values. Tests cover exception handling and wrapper selection.

Changes

VertexAI exception suppression

Layer / File(s) Summary
Async-aware dont_throw decorator
packages/opentelemetry-instrumentation-vertexai/opentelemetry/instrumentation/vertexai/utils.py
The decorator detects coroutine functions, uses an async wrapper when needed, awaits asynchronous calls, and centralizes exception handling.
Decorator behavior tests
packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py
Tests verify exception suppression, return-value preservation, wrapper selection, and resilience when the configured exception logger raises.

Estimated code review effort: 2 (Simple) | ~10 minutes

Mergeability Score: ⚪ Minimal · up to ba78b

The change is localized to async error handling with dedicated tests, and no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.64% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: adding async support to the VertexAI dont_throw decorator.
Linked Issues check ✅ Passed The changes add async exception handling, preserve synchronous behavior, and include tests required by issue #4414.
Out of Scope Changes check ✅ Passed The changes are limited to VertexAI dont_throw behavior and focused tests, with no unrelated scope identified.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py (1)

7-26: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover the configured exception callback.

The tests verify suppression and return values, but they do not configure Config.exception_logger. Add synchronous and asynchronous cases that verify the callback receives the original exception. Add a callback-that-raises case to verify that dont_throw still returns None.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py`
around lines 7 - 26, Add synchronous and asynchronous tests around dont_throw
that set Config.exception_logger, assert it receives the original raised
exception, and restore the configuration afterward. Also add a case where the
configured callback raises, verifying both sync and async dont_throw calls still
return None without propagating either exception.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/opentelemetry-instrumentation-vertexai/opentelemetry/instrumentation/vertexai/utils.py`:
- Around line 29-36: Update _handle_exception so Config.exception_logger(e) is
invoked within its own suppression boundary; catch any exception raised by the
callback, log the callback failure through logger.debug, and do not re-raise it,
preserving the guarantee that instrumentation errors cannot escape to the Vertex
AI call.

---

Nitpick comments:
In `@packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py`:
- Around line 7-26: Add synchronous and asynchronous tests around dont_throw
that set Config.exception_logger, assert it receives the original raised
exception, and restore the configuration afterward. Also add a case where the
configured callback raises, verifying both sync and async dont_throw calls still
return None without propagating either exception.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d72bac5d-8fb6-4dbb-b515-3122e5ccd7ec

📥 Commits

Reviewing files that changed from the base of the PR and between 62e24c2 and f40fd2d.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-vertexai/opentelemetry/instrumentation/vertexai/utils.py
  • packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py

@chiruu12
chiruu12 force-pushed the fix/vertexai-dont-throw-async branch from f40fd2d to af1460e Compare August 13, 2026 02:51

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py`:
- Around line 7-12: Extend the tests for dont_throw in
test_dont_throw_swallows_sync_exceptions and the corresponding asynchronous test
to capture debug logging and assert that each swallowed exception emits the
expected log through _handle_exception. Preserve the existing assertions that
synchronous and asynchronous exceptions do not reach the caller.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6865caed-9b9c-47bc-bda7-1677cf9e3a5a

📥 Commits

Reviewing files that changed from the base of the PR and between f40fd2d and 690254a.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-vertexai/opentelemetry/instrumentation/vertexai/utils.py
  • packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/opentelemetry-instrumentation-vertexai/opentelemetry/instrumentation/vertexai/utils.py

Comment thread packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py Outdated
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.

🐛 Bug Report: dont_throw has no effect on async functions in VertexAI instrumentation

3 participants