Skip to content

Commit c50ad1b

Browse files
committed
apply message truncation to Google GenAI gen_ai.request.messages
1 parent bf1003e commit c50ad1b

File tree

1 file changed

+10
-11
lines changed
  • packages/core/src/utils/google-genai

1 file changed

+10
-11
lines changed

packages/core/src/utils/google-genai/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE,
2323
GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,
2424
} from '../ai/gen-ai-attributes';
25+
import { truncateGenAiMessages } from '../ai/messageTruncation';
2526
import { buildMethodPath, getFinalOperationName, getSpanOperation } from '../ai/utils';
2627
import { handleCallbackErrors } from '../handleCallbackErrors';
2728
import { CHAT_PATH, CHATS_CREATE_METHOD, GOOGLE_GENAI_SYSTEM_NAME } from './constants';
@@ -128,25 +129,23 @@ function extractRequestAttributes(
128129
return attributes;
129130
}
130131

131-
/**
132-
* Add private request attributes to spans.
133-
* This is only recorded if recordInputs is true.
134-
* Handles different parameter formats for different Google GenAI methods.
135-
*/
136132
function addPrivateRequestAttributes(span: Span, params: Record<string, unknown>): void {
137-
// For models.generateContent: ContentListUnion: Content | Content[] | PartUnion | PartUnion[]
138133
if ('contents' in params) {
139-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(params.contents) });
134+
const contents = params.contents;
135+
const truncatedContents = truncateGenAiMessages(contents as unknown[]);
136+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedContents) });
140137
}
141138

142-
// For chat.sendMessage: message can be string or Part[]
143139
if ('message' in params) {
144-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(params.message) });
140+
const message = params.message;
141+
const truncatedMessage = truncateGenAiMessages(message as unknown[]);
142+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedMessage) });
145143
}
146144

147-
// For chats.create: history contains the conversation history
148145
if ('history' in params) {
149-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(params.history) });
146+
const history = params.history;
147+
const truncatedHistory = truncateGenAiMessages(history as unknown[]);
148+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedHistory) });
150149
}
151150
}
152151

0 commit comments

Comments
 (0)