Skip to content

Commit 8efeee6

Browse files
nirgaclaude
andcommitted
fix: improve type safety for thinking parameters
- Add proper TypeScript interfaces for thinking parameters - Replace 'any' types with specific typed interfaces - Reduce lint warnings from 25 to 21 - Improve code maintainability and type checking 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d97bd7e commit 8efeee6

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

packages/instrumentation-anthropic/src/instrumentation.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
CONTEXT_KEY_ALLOW_TRACE_CONTENT,
3232
SpanAttributes,
3333
} from "@traceloop/ai-semantic-conventions";
34-
import { AnthropicInstrumentationConfig } from "./types";
34+
import { AnthropicInstrumentationConfig, MessageCreateParamsWithThinking } from "./types";
3535
import { version } from "../package.json";
3636
import type * as anthropic from "@anthropic-ai/sdk";
3737
import type {
@@ -214,14 +214,11 @@ export class AnthropicInstrumentation extends InstrumentationBase {
214214
attributes[SpanAttributes.LLM_TOP_K] = params.top_k;
215215

216216
// Handle thinking parameters
217-
if ((params as any).thinking) {
218-
const thinking = (params as any).thinking;
219-
if (thinking.type) {
220-
attributes["llm.request.thinking.type"] = thinking.type;
221-
}
222-
if (thinking.budget_tokens) {
223-
attributes["llm.request.thinking.budget_tokens"] = thinking.budget_tokens;
224-
}
217+
const paramsWithThinking = params as MessageCreateParamsWithThinking;
218+
if (paramsWithThinking.thinking) {
219+
const thinking = paramsWithThinking.thinking;
220+
attributes["llm.request.thinking.type"] = thinking.type;
221+
attributes["llm.request.thinking.budget_tokens"] = thinking.budget_tokens;
225222
}
226223

227224
if (type === "completion") {

packages/instrumentation-anthropic/src/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ export interface AnthropicInstrumentationConfig extends InstrumentationConfig {
88
traceContent?: boolean;
99

1010
/**
11-
* A custom logger to log any exceptions that happen during span creation.
11+
* A custom logger to log any exceptions that happen during span created.
1212
*/
1313
exceptionLogger?: (e: Error) => void;
1414
}
15+
16+
export interface ThinkingParams {
17+
type: "enabled";
18+
budget_tokens: number;
19+
}
20+
21+
export interface MessageCreateParamsWithThinking {
22+
thinking?: ThinkingParams;
23+
}

packages/instrumentation-anthropic/test/instrumentation.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,14 @@ describe("Test Anthropic instrumentation", async function () {
385385
const content = JSON.parse(chatSpan.attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.content`] as string);
386386
assert.ok(Array.isArray(content));
387387

388-
const thinkingBlock = content.find((block: any) => block.type === "thinking");
389-
const textBlock = content.find((block: any) => block.type === "text");
388+
interface ContentBlock {
389+
type: string;
390+
thinking?: string;
391+
text?: string;
392+
}
393+
394+
const thinkingBlock = content.find((block: ContentBlock) => block.type === "thinking");
395+
const textBlock = content.find((block: ContentBlock) => block.type === "text");
390396

391397
assert.ok(thinkingBlock, "Should contain a thinking block");
392398
assert.ok(thinkingBlock.thinking, "Thinking block should have thinking content");

0 commit comments

Comments
 (0)