Skip to content

fix(anthropic): enable messages.stream() in auto-instrumentation e2e tests#1696

Open
Stephen Belanger (Qard) wants to merge 1 commit intomainfrom
fix/anthropic-stream-helper-auto-instrumentation
Open

fix(anthropic): enable messages.stream() in auto-instrumentation e2e tests#1696
Stephen Belanger (Qard) wants to merge 1 commit intomainfrom
fix/anthropic-stream-helper-auto-instrumentation

Conversation

@Qard
Copy link
Copy Markdown
Contributor

Summary

Closes #1592.

The root cause of this issue was fixed by PR #1617, which patched TracingChannel.prototype.tracePromise to preserve the original APIPromise object rather than wrapping it in a plain .then() chain that strips .withResponse().

This PR removes the e2e workaround that was preventing client.messages.stream() from being tested in the auto-hook instrumentation path.

What was the workaround?

runAutoAnthropicInstrumentation was forcing useMessagesStreamHelper: false, which caused the "anthropic-stream-with-response-operation" scenario to use client.messages.create({ stream: true }) instead of client.messages.stream(). This avoided the failure but left the real code path untested.

Why is the fix correct?

client.messages.stream() internally calls this.create({...}).withResponse(). The instrumented create returns the original APIPromise (not a wrapped plain Promise), so .withResponse() is still available. The APIPromise constructor check (result.constructor !== Promise) correctly routes to the preserve-original branch in the patched tracePromise.

All 140 anthropic e2e tests pass with both wrapper and auto-hook instrumentation paths using messages.stream().

Test plan

  • All 140 anthropic e2e tests pass with messages.stream() enabled (pnpm vitest run scenarios/anthropic-instrumentation/scenario.test.ts)
  • CI e2e and js-test checks pass

🤖 Generated with Claude Code

…tests (#1592)

The root cause of #1592 was fixed by PR #1617: `patch-tracing-channel.ts` now
preserves the original `APIPromise` object (with `.withResponse()`) instead of
wrapping it in a plain `.then()` chain when the result is a Promise subclass.

Remove the `useMessagesStreamHelper: false` workaround that was causing the
auto-hook instrumentation path to avoid `client.messages.stream()`. The 140
anthropic e2e tests all pass with `messages.stream()` enabled in both the
wrapper and auto-hook paths.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
export async function runAutoAnthropicInstrumentation(Anthropic, options) {
await runAnthropicInstrumentationScenario(Anthropic, {
...options,
useMessagesStreamHelper: false,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should probably also remove the code that used this argument since it's only used once I think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-instrumentation via node hook breaks Anthropic messages.stream()

2 participants