Skip to content

Commit ce6f058

Browse files
committed
fix: make usage property optional in AnthropicResponse and remove usage assignment in translateChunkToAnthropicEvents message_start
1 parent e0c83ee commit ce6f058

3 files changed

Lines changed: 5 additions & 13 deletions

File tree

src/routes/messages/anthropic-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export interface AnthropicResponse {
101101
| "refusal"
102102
| null
103103
stop_sequence: string | null
104-
usage: {
104+
usage?: {
105105
input_tokens: number
106106
output_tokens: number
107107
cache_creation_input_tokens?: number

src/routes/messages/stream-translation.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ export function translateChunkToAnthropicEvents(
4141
model: chunk.model,
4242
stop_reason: null,
4343
stop_sequence: null,
44-
usage: {
45-
input_tokens:
46-
(chunk.usage?.prompt_tokens ?? 0)
47-
- (chunk.usage?.prompt_tokens_details?.cached_tokens ?? 0),
48-
output_tokens: 0, // Will be updated in message_delta when finished
49-
...(chunk.usage?.prompt_tokens_details?.cached_tokens
50-
!== undefined && {
51-
cache_read_input_tokens:
52-
chunk.usage.prompt_tokens_details.cached_tokens,
53-
}),
54-
},
5544
},
5645
})
5746
state.messageStartSent = true

tests/anthropic-response.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ describe("OpenAI to Anthropic Non-Streaming Response Translation", () => {
100100

101101
expect(anthropicResponse.id).toBe("chatcmpl-123")
102102
expect(anthropicResponse.stop_reason).toBe("end_turn")
103-
expect(anthropicResponse.usage.input_tokens).toBe(9)
103+
expect(anthropicResponse.usage).toBeDefined()
104+
if (anthropicResponse.usage) {
105+
expect(anthropicResponse.usage.input_tokens).toBe(9)
106+
}
104107
expect(anthropicResponse.content[0].type).toBe("text")
105108
if (anthropicResponse.content[0].type === "text") {
106109
expect(anthropicResponse.content[0].text).toBe(

0 commit comments

Comments
 (0)