Skip to content

fix(anthropic): guard tools event emission with truthiness check#4248

Open
QuentinBisson wants to merge 2 commits into
traceloop:mainfrom
QuentinBisson:fix/anthropic-tools-not-given
Open

fix(anthropic): guard tools event emission with truthiness check#4248
QuentinBisson wants to merge 2 commits into
traceloop:mainfrom
QuentinBisson:fix/anthropic-tools-not-given

Conversation

@QuentinBisson

@QuentinBisson QuentinBisson commented Jun 15, 2026

Copy link
Copy Markdown

What

event_emitter.py checks if kwargs.get("tools") is not None: before emitting
tool MessageEvents. anthropic.NOT_GIVEN (the sentinel used for omitted
parameters) is not None, so on tool-free requests the sentinel leaks into the
OTLP log body.

Fix

Change the guard to if kwargs.get("tools"):, matching the system truthiness
check two lines above.

Severity: observability-only — produces noisy/wrong log records, no functional
impact on the instrumented application.

Summary by CodeRabbit

Bug Fixes

  • Corrected event emission behavior to prevent unnecessary tool-related events when tool definitions are not provided.

Tests

  • Added test coverage for edge cases in tool parameter handling.

tools=NOT_GIVEN (the anthropic sentinel) is not None, so the previous
is-not-None guard caused the sentinel to leak into the OTLP log body
on tool-free requests. Use a truthiness check, matching the system
check two lines above.
@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.

@coderabbitai

coderabbitai Bot commented Jun 15, 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: 84481421-312b-4d06-affe-0bab23338dfa

📥 Commits

Reviewing files that changed from the base of the PR and between d7aaf6c and 68f8d49.

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

📝 Walkthrough

Walkthrough

In emit_input_events, the condition guarding tool-definition event emission is changed from kwargs.get("tools") is not None to if kwargs.get("tools"):, preventing falsy values like anthropic.NOT_GIVEN from triggering a tools event. A regression test is added to verify the corrected behavior.

Changes

Tools guard fix and regression test

Layer / File(s) Summary
Tools truthiness guard fix and regression test
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/event_emitter.py, packages/opentelemetry-instrumentation-anthropic/tests/test_semconv_span_attrs.py
Switches the tools condition from an identity check (is not None) to a truthiness check (if kwargs.get("tools"):), and adds test_emit_input_events_not_given_tools_emits_no_tools_event to assert that passing tools=anthropic.NOT_GIVEN results in exactly one emitted event with name gen_ai.user.message.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related issues

Poem

A sentinel snuck through the gate, oh my!
NOT_GIVEN is falsy — let it pass by.
One truthiness check, a bunny's quick hop,
No phantom tool events, the leaking does stop.
🐇✨ The test now confirms it — hip-hop hooray!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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: implementing a truthiness check to guard tools event emission, which is the core fix addressing the sentinel value leaking issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

@QuentinBisson QuentinBisson marked this pull request as ready for review June 15, 2026 21:45
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