Skip to content

Commit 7df9993

Browse files
committed
apply message truncation to Google GenAI with proper type handling
1 parent 50d1d6d commit 7df9993

File tree

1 file changed

+18
-6
lines changed
  • packages/core/src/utils/google-genai

1 file changed

+18
-6
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,32 @@ function extractRequestAttributes(
132132
function addPrivateRequestAttributes(span: Span, params: Record<string, unknown>): void {
133133
if ('contents' in params) {
134134
const contents = params.contents;
135-
const truncatedContents = truncateGenAiMessages(contents as unknown[]);
136-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedContents) });
135+
if (Array.isArray(contents)) {
136+
const truncatedContents = truncateGenAiMessages(contents);
137+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedContents) });
138+
} else {
139+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(contents) });
140+
}
137141
}
138142

139143
if ('message' in params) {
140144
const message = params.message;
141-
const truncatedMessage = truncateGenAiMessages(message as unknown[]);
142-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedMessage) });
145+
if (Array.isArray(message)) {
146+
const truncatedMessage = truncateGenAiMessages(message);
147+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedMessage) });
148+
} else {
149+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(message) });
150+
}
143151
}
144152

145153
if ('history' in params) {
146154
const history = params.history;
147-
const truncatedHistory = truncateGenAiMessages(history as unknown[]);
148-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedHistory) });
155+
if (Array.isArray(history)) {
156+
const truncatedHistory = truncateGenAiMessages(history);
157+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedHistory) });
158+
} else {
159+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(history) });
160+
}
149161
}
150162
}
151163

0 commit comments

Comments
 (0)