Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -580,18 +580,23 @@ async def _process_token_stream(
# as nvext.routed_experts); disaggregated_params stays KV-transfer only.
out["engine_data"] = {"routed_experts": routed_experts}
if finish_reason:
input_tokens = meta_info["prompt_tokens"]
completion_tokens = meta_info["completion_tokens"]
cached_tokens = meta_info["cached_tokens"]
input_tokens = meta_info.get("prompt_tokens")
completion_tokens = meta_info.get("completion_tokens")
cached_tokens = meta_info.get("cached_tokens")
prefill_prompt_tokens_details = None
if cached_tokens is not None and cached_tokens > 0:
prefill_prompt_tokens_details = {"cached_tokens": cached_tokens}
out["completion_usage"] = {
"prompt_tokens": input_tokens,
"completion_tokens": completion_tokens,
"total_tokens": input_tokens + completion_tokens,
"prompt_tokens_details": prefill_prompt_tokens_details,
}
if input_tokens is not None and completion_tokens is not None:
completion_usage = {
"prompt_tokens": input_tokens,
"completion_tokens": completion_tokens,
"total_tokens": input_tokens + completion_tokens,
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
if prefill_prompt_tokens_details is not None:
completion_usage[
"prompt_tokens_details"
] = prefill_prompt_tokens_details
out["completion_usage"] = completion_usage
if metadata_uploader is not None:
try:
await metadata_uploader.upload_choice(output_idx, meta_info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,53 @@ async def test_metadata_upload_normalizes_numpy_values(tmp_path):
assert uploaded_array["data"] == expected.tobytes()


@pytest.mark.asyncio
async def test_process_token_stream_treats_completion_usage_as_optional():
handler = _new_decode_handler()

chunks = await _collect(
handler._process_token_stream(
_stream(
[
{
"index": 0,
"output_ids": [],
"meta_info": {
"id": "request-1",
"finish_reason": {"type": "stop"},
},
},
{
"index": 1,
"output_ids": [],
"meta_info": {
"id": "request-1",
"finish_reason": {"type": "stop"},
"prompt_tokens": 2,
"completion_tokens": 3,
},
},
]
),
_Context(),
)
)

assert chunks == [
{"index": 0, "finish_reason": "stop", "token_ids": []},
{
"index": 1,
"finish_reason": "stop",
"token_ids": [],
"completion_usage": {
"prompt_tokens": 2,
"completion_tokens": 3,
"total_tokens": 5,
},
},
]


@pytest.mark.asyncio
async def test_process_token_stream_tracks_logprobs_per_choice_index():
handler = _new_decode_handler()
Expand Down
Loading