Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/core/src/tracing/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ function extractRequestAttributes(args: unknown[], operationName: string): Recor
}

// Extract and record AI request inputs, if present. This is intentionally separate from response attributes.
function addRequestAttributes(span: Span, params: Record<string, unknown>, operationName: string): void {
function addRequestAttributes(
span: Span,
params: Record<string, unknown>,
operationName: string,
enableTruncation: boolean,
): void {
// Store embeddings input on a separate attribute and do not truncate it
if (operationName === 'embeddings' && 'input' in params) {
const input = params.input;
Expand Down Expand Up @@ -119,8 +124,10 @@ function addRequestAttributes(span: Span, params: Record<string, unknown>, opera
span.setAttribute(GEN_AI_SYSTEM_INSTRUCTIONS_ATTRIBUTE, systemInstructions);
}

const truncatedInput = getTruncatedJsonString(filteredMessages);
span.setAttribute(GEN_AI_INPUT_MESSAGES_ATTRIBUTE, truncatedInput);
span.setAttribute(
GEN_AI_INPUT_MESSAGES_ATTRIBUTE,
enableTruncation ? getTruncatedJsonString(filteredMessages) : JSON.stringify(filteredMessages),
);
Comment thread
cursor[bot] marked this conversation as resolved.

if (Array.isArray(filteredMessages)) {
span.setAttribute(GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE, filteredMessages.length);
Expand Down Expand Up @@ -162,7 +169,7 @@ function instrumentMethod<T extends unknown[], R>(
originalResult = originalMethod.apply(context, args);

if (options.recordInputs && params) {
addRequestAttributes(span, params, operationName);
addRequestAttributes(span, params, operationName, options.enableTruncation ?? true);
}

// Return async processing
Expand Down Expand Up @@ -200,7 +207,7 @@ function instrumentMethod<T extends unknown[], R>(
originalResult = originalMethod.apply(context, args);

if (options.recordInputs && params) {
addRequestAttributes(span, params, operationName);
addRequestAttributes(span, params, operationName, options.enableTruncation ?? true);
}

return originalResult.then(
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/tracing/openai/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface OpenAiOptions {
* Enable or disable output recording.
*/
recordOutputs?: boolean;
/**
* Enable or disable truncation of recorded input messages.
* Defaults to `true`.
*/
enableTruncation?: boolean;
}

export interface OpenAiClient {
Expand Down
86 changes: 86 additions & 0 deletions packages/core/test/tracing/openai-enable-truncation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { OpenAiClient } from '../../src';
import { getCurrentScope, getGlobalScope, getIsolationScope, setCurrentClient, spanToJSON, startSpan } from '../../src';
import { GEN_AI_INPUT_MESSAGES_ATTRIBUTE } from '../../src/tracing/ai/gen-ai-attributes';
import { instrumentOpenAiClient } from '../../src/tracing/openai';
import type { Span } from '../../src/types-hoist/span';
import { getSpanDescendants } from '../../src/utils/spanUtils';
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';

function createMockOpenAiClient() {
return {
chat: {
completions: {
create: async (params: { model: string; messages: Array<{ role: string; content: string }> }) => ({
id: 'chatcmpl-test',
model: params.model,
choices: [{ message: { content: 'Hello!' }, finish_reason: 'stop' }],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
}),
},
},
};
}

type MockClient = ReturnType<typeof createMockOpenAiClient>;

describe('OpenAI enableTruncation option', () => {
beforeEach(() => {
getCurrentScope().clear();
getIsolationScope().clear();
getGlobalScope().clear();

const options = getDefaultTestClientOptions({ tracesSampleRate: 1 });
const client = new TestClient(options);
setCurrentClient(client);
client.init();
});

afterEach(() => {
getCurrentScope().clear();
getIsolationScope().clear();
getGlobalScope().clear();
});

async function callWithOptions(
options: { recordInputs?: boolean; enableTruncation?: boolean },
messages: Array<{ role: string; content: string }>,
): Promise<string | undefined> {
const mockClient = createMockOpenAiClient();
const instrumented = instrumentOpenAiClient(mockClient as unknown as OpenAiClient, options) as MockClient;

let rootSpan: Span | undefined;

await startSpan({ name: 'test' }, async span => {
rootSpan = span;
await instrumented.chat.completions.create({ model: 'gpt-4', messages });
});

const spans = getSpanDescendants(rootSpan!);
const aiSpan = spans.find(s => spanToJSON(s).op === 'gen_ai.chat');
return spanToJSON(aiSpan!).data?.[GEN_AI_INPUT_MESSAGES_ATTRIBUTE] as string | undefined;
}

const longContent = 'A'.repeat(200_000);
const messages = [{ role: 'user', content: longContent }];

it('truncates input messages by default', async () => {
const inputMessages = await callWithOptions({ recordInputs: true }, messages);
expect(inputMessages).toBeDefined();
expect(inputMessages!.length).toBeLessThan(longContent.length);
});

it('truncates input messages when enableTruncation is true', async () => {
const inputMessages = await callWithOptions({ recordInputs: true, enableTruncation: true }, messages);
expect(inputMessages).toBeDefined();
expect(inputMessages!.length).toBeLessThan(longContent.length);
});

it('does not truncate input messages when enableTruncation is false', async () => {
const inputMessages = await callWithOptions({ recordInputs: true, enableTruncation: false }, messages);
expect(inputMessages).toBeDefined();

const parsed = JSON.parse(inputMessages!);
expect(parsed).toEqual(messages);
});
});
Comment thread
andreiborza marked this conversation as resolved.
Outdated
Loading