Commit fe1a8df
fix: make usage chunk in stream mode of gemini compatible with openai (#1503)
**Description**
Users found the simply use "usage" information does not work for
streaming responses of gemini models.
<img width="964" height="70" alt="image"
src="https://github.com/user-attachments/assets/334a5515-a57b-4f83-9bab-f296422409b8"
/>
This is because for openai models, the usage chunk would be a separate
chunk. For example, this is an example response from gpt-4o:
```
...
chunk=ChatCompletionChunk(id='chatcmpl-CYv6DPkWfT1xrsS2ySOoRztQKnZDg', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None), finish_reason='stop', index=0, logprobs=None, content_filter_result={'error': {'code': 'content_filter_error', 'message': 'The contents are not filtered'}})], created=1762438677, model='azure.gpt-4o', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_4a331a0222', usage=None, obfuscation='2xP')
chunk=ChatCompletionChunk(id='chatcmpl-CYv6DPkWfT1xrsS2ySOoRztQKnZDg', choices=[], created=1762438677, model='azure.gpt-4o', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_4a331a0222', usage=CompletionUsage(completion_tokens=17, prompt_tokens=12, total_tokens=29, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)), obfuscation='xY40HsJr')
```
There is a finish_reason chunk, and then a usage chunk.
Thus, want to make it compatible with Openai. (Actually, in anthropic
translation, it's already compatible)
---------
Signed-off-by: yxia216 <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Co-authored-by: Dan Sun <[email protected]>
Co-authored-by: Takeshi Yoneda <[email protected]>
Co-authored-by: Aaron Choo <[email protected]>1 parent d78bbb9 commit fe1a8df
File tree
3 files changed
+47
-23
lines changed- internal/translator
- tests/extproc
3 files changed
+47
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
198 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
199 | 232 | | |
200 | 233 | | |
201 | 234 | | |
| |||
209 | 242 | | |
210 | 243 | | |
211 | 244 | | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | 245 | | |
223 | 246 | | |
224 | 247 | | |
| |||
381 | 404 | | |
382 | 405 | | |
383 | 406 | | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | 407 | | |
391 | 408 | | |
392 | 409 | | |
393 | 410 | | |
394 | 411 | | |
395 | | - | |
396 | | - | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
397 | 415 | | |
398 | 416 | | |
399 | 417 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1019 | 1019 | | |
1020 | 1020 | | |
1021 | 1021 | | |
1022 | | - | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
1023 | 1025 | | |
1024 | 1026 | | |
1025 | 1027 | | |
| |||
1228 | 1230 | | |
1229 | 1231 | | |
1230 | 1232 | | |
1231 | | - | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
1232 | 1236 | | |
1233 | 1237 | | |
1234 | 1238 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
579 | 579 | | |
580 | 580 | | |
581 | 581 | | |
582 | | - | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
583 | 585 | | |
584 | 586 | | |
585 | 587 | | |
| |||
0 commit comments