Skip to content

fix(closes OPEN-10343): handle Google ADK callback exceptions cleanly#638

Merged
gustavocidornelas merged 1 commit intomainfrom
vini/open-10343-google-adk-tracer-exceptions-raised-inside
May 7, 2026
Merged

fix(closes OPEN-10343): handle Google ADK callback exceptions cleanly#638
gustavocidornelas merged 1 commit intomainfrom
vini/open-10343-google-adk-tracer-exceptions-raised-inside

Conversation

@viniciusdsmello
Copy link
Copy Markdown
Contributor

Summary

When a user-defined before_model_callback raises (e.g. a guardrail short-circuit), the Openlayer Google ADK tracer used to:

  1. Surface a chained ValueError("<Token> was created in a different Context") from its contextvar cleanup paths during async exception unwinding, polluting the user's traceback.
  2. Overwrite step.output with a "Error: ..." string, polluting traces.
  3. Emit logger.error("Error in agent execution: ...") / Error in LLM call / Error in tool execution, which duplicated the user's exception in the output.

This change makes exception unwinding boring:

  • _safe_reset_contextvar lifted into lib/tracing/tracer.py as a shared helper. Wraps ContextVar.reset(token) with the same cross-context ValueError swallow that tracer.create_step already used inline. Applied at the three bare reset sites in google_adk_tracer.py (agent transfer-token, async/sync callback wrappers) — these were the actual chain producers — and at the previously-inline site in create_step.
  • _record_step_error in google_adk_tracer.py records step.metadata["error"] = {"type", "message"} instead of overwriting step.output. Applied to all five wrappers (agent / LLM / tool / async + sync callback).
  • The three pass-through logger.error("Error in ...") lines demoted to logger.debug — the exception already shows in the user's traceback.
  • New tests/test_google_adk_integration.py (7 tests) asserts: user exception is the only entry in the chain, metadata["error"] is recorded, step.output is not overwritten, plus unit coverage for the helpers.

Test plan

  • New ADK regression suite (tests/test_google_adk_integration.py) — 7/7 pass.
  • Unaffected suites still green: test_offline_buffering.py, test_portkey_integration.py — 45/45 pass.
  • CI green on this branch.

🤖 Generated with Claude Code

@viniciusdsmello viniciusdsmello force-pushed the vini/open-10343-google-adk-tracer-exceptions-raised-inside branch from 46b7e9d to 31aacc1 Compare May 7, 2026 16:24
@viniciusdsmello viniciusdsmello self-assigned this May 7, 2026
@viniciusdsmello viniciusdsmello force-pushed the vini/open-10343-google-adk-tracer-exceptions-raised-inside branch from 31aacc1 to 1771fb0 Compare May 7, 2026 16:27
When a user-defined `before_model_callback` raises (e.g. a guardrail
short-circuit), the Openlayer tracer used to surface a chained
`ValueError("<Token> was created in a different Context")` from its
contextvar cleanup paths and overwrite span output with `"Error: ..."`,
polluting tracebacks and traces. Make exception unwinding boring:

- Add `_safe_reset_contextvar`: wraps `ContextVar.reset(token)` with the
  same `try/except ValueError` pattern used by `tracer.create_step`, so
  cross-context tokens during async unwind no longer raise a chained
  exception. Apply it to the three bare reset sites in the agent
  transfer-token path, async callback wrapper, and sync callback wrapper.
- Add `_record_step_error`: stores `metadata["error"] = {type, message}`
  on the step instead of overwriting `step.output` with `"Error: ..."`.
  Apply it to all five wrappers (agent, LLM, tool, async/sync callback).
- Demote the three pass-through `logger.error("Error in ...")` lines to
  `logger.debug` — the exception already shows up in the user's
  traceback, our duplicate log only added confusion.
- Add `tests/test_google_adk_integration.py` with end-to-end coverage
  asserting the user's exception is the sole exception in the chain,
  that metadata records the error, and that `step.output` is not
  overwritten — plus unit tests for the new helpers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@viniciusdsmello viniciusdsmello force-pushed the vini/open-10343-google-adk-tracer-exceptions-raised-inside branch from 1771fb0 to d46d48e Compare May 7, 2026 16:29
@gustavocidornelas gustavocidornelas merged commit 8ec3f76 into main May 7, 2026
5 checks passed
@gustavocidornelas gustavocidornelas deleted the vini/open-10343-google-adk-tracer-exceptions-raised-inside branch May 7, 2026 18:25
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.

2 participants