Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(sdk): accept the default-export anthropic class in instrumentmodules (#1064)

Rudy Mizrahi Celekli6d045a41bc74
diff --git a/docs/sdk/instrumentation.mdx b/docs/sdk/instrumentation.mdx
index 5e2f5e8e..e5dbbbc1 100644
--- a/docs/sdk/instrumentation.mdx
+++ b/docs/sdk/instrumentation.mdx
@@ -46,7 +46,7 @@ By default, the SDK creates OpenAI and Anthropic OpenLLMetry instrumentations wh
Pass modules to `instrumentModules` when they may be loaded before auto-instrumentation runs.
```typescript title="Patch already-loaded modules during initialization" lines
-import Anthropic from "@anthropic-ai/sdk";
+import * as Anthropic from "@anthropic-ai/sdk";
import OpenAI from "openai";
import { Observe } from "@asymptote/sdk";
diff --git a/docs/sdk/integrations-anthropic.mdx b/docs/sdk/integrations-anthropic.mdx
index 67d017e1..39c0754c 100644
--- a/docs/sdk/integrations-anthropic.mdx
+++ b/docs/sdk/integrations-anthropic.mdx
@@ -80,17 +80,17 @@ All supported Anthropic SDK calls are now traced through the configured Beacon e
If your application imports or creates the Anthropic client before initialization, pass the module to `instrumentModules` or patch it after initialization.
```typescript title="Instrument an already-loaded Anthropic module" lines
-import Anthropic from "@anthropic-ai/sdk";
+import * as AnthropicSDK from "@anthropic-ai/sdk";
import { Observe } from "@asymptote/sdk";
-const anthropic = new Anthropic({
+const anthropic = new AnthropicSDK.Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
Observe.initialize({
apiKey: process.env.ASYMPTOTE_API_KEY,
instrumentModules: {
- Anthropic,
+ Anthropic: AnthropicSDK,
},
});
```
diff --git a/packages/asymptote-sdk-js/src/index.ts b/packages/asymptote-sdk-js/src/index.ts
index 4ddef9f9..d34acde3 100644
--- a/packages/asymptote-sdk-js/src/index.ts
+++ b/packages/asymptote-sdk-js/src/index.ts
@@ -530,7 +530,7 @@ function patchOpenLLMetryModules(modules: InstrumentModules): void {
function manuallyInstrumentModules(instrumentations: Instrumentation[], modules: InstrumentModules): void {
const openAIModule = modules.OpenAI ?? modules.openAI ?? modules.openai;
- const anthropicModule = modules.Anthropic ?? modules.anthropic;
+ const anthropicModule = anthropicModuleShape(modules.Anthropic ?? modules.anthropic);
for (const instrumentation of instrumentations) {
const maybeManual = instrumentation as Instrumentation & {
manuallyInstrument?: (moduleValue: unknown) => void;
@@ -545,6 +545,15 @@ function manuallyInstrumentModules(instrumentations: Instrumentation[], modules:
}
}
+// The Anthropic instrumentation needs the module namespace (`module.Anthropic.Messages`, and
+// `module.APIPromise` for streams); the default-export client class carries neither.
+function anthropicModuleShape(moduleValue: unknown): unknown {
+ if (typeof moduleValue === "function" && "Messages" in moduleValue) {
+ throw new TypeError('instrumentModules.Anthropic must be the module namespace: import * as Anthropic from "@anthropic-ai/sdk"');
+ }
+ return moduleValue;
+}
+
function patchClaudeAgentSDK(moduleValue: unknown): void {
if (!moduleValue || typeof moduleValue !== "object") {
return;
diff --git a/packages/asymptote-sdk-js/src/provider-routing.test.ts b/packages/asymptote-sdk-js/src/provider-routing.test.ts
index a2ba103e..4755e8c9 100644
--- a/packages/asymptote-sdk-js/src/provider-routing.test.ts
+++ b/packages/asymptote-sdk-js/src/provider-routing.test.ts
@@ -1,4 +1,4 @@
-import * as Anthropic from "@anthropic-ai/sdk";
+import AnthropicClient, * as Anthropic from "@anthropic-ai/sdk";
import { InMemorySpanExporter } from "@opentelemetry/sdk-trace-base";
import { afterEach, expect, it } from "vitest";
import { initialize, observe, flush, shutdown } from "./index.js";
@@ -35,3 +35,11 @@ it("routes real provider instrumentation to the new exporter after reinitializat
}
expect(fetches).toBe(2);
});
+
+// The default export is the client class, which has neither the module's resources for the
+// instrumentation to patch nor the APIPromise its streaming wrapper needs.
+it("rejects the default-export Anthropic class with an error naming the namespace import", () => {
+ expect(() =>
+ initialize({ spanExporter: new InMemorySpanExporter(), disableDefaultExporter: true, instrumentationOptions: { openAI: false }, instrumentModules: { Anthropic: AnthropicClient } }),
+ ).toThrow(/import \* as Anthropic from "@anthropic-ai\/sdk"/);
+});