Skip to content

Commit bedf496

Browse files
committed
Convert to anthropic messages without empty assisstant content
1 parent 4fc575b commit bedf496

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

web/src/app/api/v1/token-count/_post.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ function convertContentToAnthropic(
220220

221221
for (const part of content) {
222222
if (part.type === 'text') {
223-
anthropicContent.push({ type: 'text', text: part.text.trim() })
223+
const text = part.text.trim()
224+
if (text) {
225+
anthropicContent.push({ type: 'text', text })
226+
}
224227
} else if (part.type === 'tool-call' && role === 'assistant') {
225228
anthropicContent.push({
226229
type: 'tool_use',
@@ -238,13 +241,16 @@ function convertContentToAnthropic(
238241
},
239242
})
240243
} else if (part.type === 'json') {
241-
anthropicContent.push({
242-
type: 'text',
243-
text:
244-
typeof part.value === 'string'
245-
? part.value.trim()
246-
: JSON.stringify(part.value).trim(),
247-
})
244+
const text =
245+
typeof part.value === 'string'
246+
? part.value.trim()
247+
: JSON.stringify(part.value).trim()
248+
if (text) {
249+
anthropicContent.push({
250+
type: 'text',
251+
text,
252+
})
253+
}
248254
}
249255
}
250256

0 commit comments

Comments
 (0)