Skip to content

fix(anthropic): prevent NOT_GIVEN sentinel leak in tools event guard#4290

Open
cschanhniem wants to merge 1 commit into
traceloop:mainfrom
cschanhniem:main
Open

fix(anthropic): prevent NOT_GIVEN sentinel leak in tools event guard#4290
cschanhniem wants to merge 1 commit into
traceloop:mainfrom
cschanhniem:main

Conversation

@cschanhniem

@cschanhniem cschanhniem commented Jun 18, 2026

Copy link
Copy Markdown

Fixes #4230

The tools guard in emit_input_events() used is not None which lets the Anthropic SDK's NOT_GIVEN sentinel through when tools is omitted. NOT_GIVEN satisfies is not None, causing it to be placed in the MessageEvent body as {"tools": NOT_GIVEN}. The OTLP log exporter then fails to encode the unrepresentable type with:

Exception: Invalid type <class 'anthropic.NotGiven'> of value NOT_GIVEN

This drops the entire log batch and produces recurring traceback noise.

The system branch immediately above uses a truthiness check which correctly drops NOT_GIVEN (bool(NOT_GIVEN) is False). This fix aligns the tools guard with that pattern. NOT_GIVEN and empty lists are both falsy, so the event is correctly skipped in both cases.

Change: if kwargs.get("tools") is not None:if kwargs.get("tools"):

Summary by CodeRabbit

  • Bug Fixes
    • Corrected telemetry event emission behavior to properly handle empty tools values during instrumentation tracking.

The tools guard in emit_input_events() used  which lets
the Anthropic SDK's  sentinel through when tools is omitted.
This matches ANTHROPIC_NOT_GIVEN which is not None, causing the sentinel
to be placed in the MessageEvent body as {tools: NOT_GIVEN}. The OTLP
log exporter then fails to encode the unrepresentable type, dropping the
entire batch.

The  branch immediately above uses a truthiness check which
correctly drops NOT_GIVEN (bool(NOT_GIVEN) is False). This fix aligns
the tools guard with that pattern. NOT_GIVEN and empty lists are both
falsy, so the event is correctly skipped in both cases.

Fixes traceloop#4230
@coderabbitai

coderabbitai Bot commented Jun 18, 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

Run ID: b96af403-1609-4ae2-8e78-05d8fe9615c2

📥 Commits

Reviewing files that changed from the base of the PR and between fc33f1c and 46b544c.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/event_emitter.py

📝 Walkthrough

Walkthrough

In event_emitter.py, the guard condition for emitting a tools-as-user-message event in emit_input_events is changed from kwargs.get("tools") is not None to if kwargs.get("tools"), aligning it with the existing truthiness check used for the system branch.

Changes

Fix NOT_GIVEN sentinel leak in tools event emission

Layer / File(s) Summary
Truthiness guard for tools in emit_input_events
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/event_emitter.py
Replaces kwargs.get("tools") is not None with if kwargs.get("tools") so that falsy values — including the Anthropic SDK NOT_GIVEN sentinel and empty lists — no longer cause a MessageEvent with an unserializable body to be emitted.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

A single is not None slipped past the gate,
Letting NOT_GIVEN crash the export crate.
🐇 One truthiness check, neat and lean,
Keeps the sentinel hidden, unseen.
Now logs flow out, tidy and great!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 accurately and concisely describes the main change: replacing a non-None check with a truthiness check to prevent the NOT_GIVEN sentinel from leaking into tools events.
Linked Issues check ✅ Passed The code change directly addresses all four objectives: prevents sentinel leakage via truthiness check, aligns tools with system parameter's existing pattern, ensures omitted tools are not emitted, and restores reliable OTLP log export.
Out of Scope Changes check ✅ Passed The change is narrowly scoped to the single-line fix in emit_input_events() that was documented in the linked issue, with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cschanhniem

Copy link
Copy Markdown
Author

Just saw the CLA check — will sign the CLA now so this can move forward.

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: Anthropic event emitter leaks NOT_GIVEN sentinel into log Body, breaking OTLP log export

2 participants