Skip to content

fix(anthropic): replace guard with truthiness check to prevent NOT_G…#4236

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

fix(anthropic): replace guard with truthiness check to prevent NOT_G…#4236
nidhithippana wants to merge 1 commit into
traceloop:mainfrom
nidhithippana:main

Conversation

@nidhithippana

@nidhithippana nidhithippana commented Jun 14, 2026

Copy link
Copy Markdown

Fixes #4230

When tools is omitted from a messages.create() call, the Anthropic SDK
defaults it to NOT_GIVEN. The previous is not None guard let this sentinel
through into the log body, causing OTLP export to fail with:

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

Changed the guard to a truthiness check (consistent with the existing system
branch above it), which correctly drops both NOT_GIVEN and empty lists.

Added a regression test to verify no sentinel leaks into emitted log bodies.

  • [yes] I have added tests that cover my changes.
  • [N/A] If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • [yes] PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • [N/A] (If applicable) I have updated the documentation accordingly.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed instrumentation event emission behavior for Anthropic message creation when tools are not provided or empty.
  • Tests

    • Added regression test to ensure instrumentation logging is correct when tools are omitted.

@CLAassistant

CLAassistant commented Jun 14, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jun 14, 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: 959b3f4c-0f14-41c7-920f-e0b4fbca9981

📥 Commits

Reviewing files that changed from the base of the PR and between 4643b88 and 0202d11.

⛔ Files ignored due to path filters (1)
  • packages/opentelemetry-instrumentation-anthropic/uv.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/event_emitter.py
  • packages/opentelemetry-instrumentation-anthropic/tests/cassettes/test_messages/test_anthropic_message_create_without_tools_no_sentinel_leak.yaml
  • packages/opentelemetry-instrumentation-anthropic/tests/test_messages.py

📝 Walkthrough

Walkthrough

Fixes a bug where omitting tools in an Anthropic messages.create() call caused the Anthropic SDK sentinel NOT_GIVEN to leak into the OTel log body. The guard in emit_input_events is changed from is not None to a truthiness check, matching the existing system branch. A VCR-backed regression test and cassette are added to verify no sentinel leaks.

Changes

NOT_GIVEN Sentinel Leak Fix

Layer / File(s) Summary
Truthiness guard for tools emission
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/event_emitter.py
emit_input_events now uses if kwargs.get("tools") instead of if kwargs.get("tools") is not None, so NOT_GIVEN and empty collections no longer produce a MessageEvent.
Regression test and VCR cassette
packages/opentelemetry-instrumentation-anthropic/tests/test_messages.py, packages/opentelemetry-instrumentation-anthropic/tests/cassettes/test_messages/test_anthropic_message_create_without_tools_no_sentinel_leak.yaml
Adds a VCR-backed test asserting that a messages.create call without tools emits exactly two logs and none contain the string NOT_GIVEN.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

🐇 A sentinel snuck in, NOT_GIVEN by name,
Into the logs it crept, causing export pain.
One truthiness check, a single line swapped,
The sneaky is not None was finally stopped.
Now tools left unset make nary a peep,
And OTLP exports continue their sleep! 🌙

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.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 describes the main change: replacing an is not None guard with a truthiness check for the tools parameter to fix the NOT_GIVEN sentinel leak issue.
Linked Issues check ✅ Passed All requirements from issue #4230 are met: the is not None guard is replaced with truthiness check, the fix aligns with existing system parameter handling, and a regression test is added.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the NOT_GIVEN sentinel leak issue documented in #4230; no out-of-scope modifications were introduced.

✏️ 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.

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