Repository navigation
fix(anthropic): keep the legacy SDK APIPromise and stop marking aborted streams OK - #506
Merged
Merged
Conversation
…ed streams OK The legacy messages.create wrapper was an async function, so it replaced the SDK's APIPromise with a plain Promise. On @anthropic-ai/sdk 0.27.3, withResponse() and asResponse() threw TypeError; on 0.133.0 the messages.stream() helper failed too. The wrapper now returns the SDK's own APIPromise (via _thenUnwrap), keeps lazy body parsing, ends the span for an asResponse()-only read, and records request errors. The SDK ends an aborted stream without an error, so a stream cancelled through stream.controller.abort() or the request signal was exported as OK with a truncated output. The span status is now left unset for a cancellation, as the SDK's native tracing does. Also: detect native SDKs and find Messages on the CommonJS module object (require() returns a callable wrapper), and declare @opentelemetry/core, which the instrumentation imports. CI now runs the real-SDK contract on the latest legacy SDK (0.133.0) and the first native one (0.134.0). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…streams ERROR - A non-stream result the attribute helpers cannot read (the SDK resolves an HTTP 204 body as null) no longer leaves the span open or turns the SDK's null into a TypeError. The span ends OK and the caller gets the SDK result. - A stream that ends before message_stop without an abort is now ERROR "The stream ended before message_stop.", as the SDK's native tracing does. An aborted stream still leaves the status unset. Tests: legacySdk (real SDK 0.27.3), plainPromise, and contract cases on 0.20.0, 0.27.3 and 0.133.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The legacy instrumentor observed request errors by attaching side handlers to the SDK's responsePromise and to the promise asResponse() returns. A side handler marks the rejection handled, so an application that never awaited a failing messages.create() (or asResponse()) no longer got Node's unhandled rejection. Without instrumentation, and with the SDK's native tracing, it does. Chain the observer into responsePromise before _thenUnwrap() reads it and rethrow, and return the chained promise from asResponse(). The span still records the error once. The native-boundary contract now checks dropped and awaited failures on SDK 0.20.0, 0.27.3, 0.133.0, 0.134.0 and 1.3.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eAI-331 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #331. Fixes problems in the legacy (non-native)
@traceai/anthropicpath that an independent review found and reproduced against the real SDKs.What was wrong
messages.createwrapper was anasyncfunction. AftermanuallyInstrument(Anthropic):@anthropic-ai/sdk@0.27.3(the declared peer),.withResponse()and.asResponse()threwTypeError: ... is not a function;0.133.0(the latest SDK without native OpenTelemetry),client.messages.stream()failed as well, because the helper callscreate(...).withResponse().stream.controller.abort()or through the requestsignal(for example a timeout). The instrumentation's copy of the stream then ran its success branch. The span got statusOKand a truncated output with no finish reason.require("@anthropic-ai/sdk")returns a callable wrapper whose ownprototypeis a plain object. On 0.134.0 and later (native, but still inside the<1auto-hook range) the hook loggedAnthropic.Messages.prototype not foundinstead of the native-SDK warning. On modern legacy SDKs such as 0.133.0 the module object has no.Messages, so the CommonJS hook could not patch either.@opentelemetry/corewas imported but not declared as a dependency. It resolved only through hoisting.message_stop, the application sees a normal end. The span got statusOKwith no finish reason.null. The wrapper tried to read output attributes from it and threw aTypeErrorat the caller.responsePromiseand to the promiseasResponse()returns. A side handler marks the rejection as handled. So a failingmessages.create()(or.asResponse()) that the application never awaited no longer reached Node'sunhandledRejection. Without instrumentation, and with the SDK's native tracing, it does.Changes
_thenUnwrap.withResponse(),asResponse(),request_idand lazy body parsing keep working. The span is still recorded when the result is read:asResponse()-only read ends the span as OK and leaves the body unread;responsePromise(before_thenUnwrapreads it) and rethrows, andasResponse()returns the chained promise. A failing call that the application drops is still an unhandled rejection, as without instrumentation.message_stop, keeps its partial attributes but leaves the span status unset. The SDK's native tracing does the same for a cancellation. If the caller aborts after the stream has completed, the span stays OK.message_stopwithout an abort is marked ERROR with the messageThe stream ended before message_stop., as the SDK's native tracing does.diag, as in native tracing..Anthropic/.defaultboth for native detection and forMessages.@opentelemetry/coreis now declared (^2.0.1, the version fi-core already resolves). The lockfile change adds only that importer entry.Tests
src/__tests__/legacySdk.test.tsuses the real peer SDK 0.27.3 with a synthetic fetch. It coverswithResponse,asResponsewith an unread body, streamwithResponse, themessages.stream()helper, abort through the controller, abort through the request signal, abort after completion, a stream that ends beforemessage_stop, an empty (204) response throughawaitandwithResponse(), and typed API errors throughawait,withResponse()andasResponse().plainPromise.test.tscovers acreate()that returns a plain promise resolving tonullor a non-object.nativeBoundary.test.tsadds cases for the CommonJS module object, for both a native and a legacy SDK.contract/native-boundary.cjs(real SDKs, OTLP over loopback) adds the same legacy cases, including the cut-short stream and the empty response. Every case also checks Node's unhandled rejections: none for awaited calls, and exactly one, with one ERROR span, for a failingcreate()orasResponse()the application drops. The native SDKs get the same dropped-call cases. (Jest reports any unhandled rejection as a test failure, so it cannot host the dropped-call cases.) CI now also runs it on0.133.0(latest legacy) and0.134.0(first native), in addition to0.20.0,0.27.3and1.3.0.Run locally with outbound network denied: jest, 74 passed and 3 skipped. The contract passed on 0.20.0, 0.27.3, 0.60.0, 0.133.0, 0.134.0 and 1.3.0, and the README Quick Start contract passed.
pnpm install --frozen-lockfilepassed andrelease_plan.py --checkpassed.Behaviour notes
asResponse()no longer produces a span, unless its request fails.APIUserAbortError) is still recorded as ERROR, which was already the behaviour.message_stopwithout an abort used to be OK and is now ERROR. The application still sees a normal end of stream.🤖 Generated with Claude Code