fix: Properly return abort error for streaming requests if the abort is triggered by scheduler (#19357)
This commit is contained in:
@@ -5,6 +5,7 @@ import json
|
||||
import logging
|
||||
import time
|
||||
import uuid
|
||||
from http import HTTPStatus
|
||||
from typing import TYPE_CHECKING, Any, AsyncGenerator, Dict, List, Optional, Union
|
||||
|
||||
import jinja2
|
||||
@@ -641,7 +642,7 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
routed_experts[index] = content["meta_info"].get("routed_experts", None)
|
||||
|
||||
# Handle logprobs
|
||||
finish_reason = content["meta_info"]["finish_reason"]
|
||||
finish_reason = content["meta_info"].get("finish_reason", None)
|
||||
choice_logprobs = None
|
||||
if request.logprobs:
|
||||
n_prev_token = n_prev_tokens.get(index, 0)
|
||||
@@ -661,7 +662,20 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
|
||||
# Track finish_reason for each index
|
||||
if finish_reason_type:
|
||||
finish_reasons[index] = finish_reason
|
||||
# If the abort is from scheduler.
|
||||
if finish_reason_type == "abort":
|
||||
code = finish_reason.get(
|
||||
"status_code", HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
error = self.create_streaming_error_response(
|
||||
finish_reason.get("message", "Generation aborted."),
|
||||
code.name,
|
||||
code.value,
|
||||
)
|
||||
yield f"data: {error}\n\n"
|
||||
break
|
||||
else:
|
||||
finish_reasons[index] = finish_reason
|
||||
|
||||
# First chunk with role
|
||||
if is_firsts.get(index, True):
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
from http import HTTPStatus
|
||||
from typing import TYPE_CHECKING, Any, AsyncGenerator, Dict, List, Optional, Union
|
||||
|
||||
from fastapi import Request
|
||||
@@ -270,13 +271,27 @@ class OpenAIServingCompletion(OpenAIServingBase):
|
||||
# Generate delta
|
||||
delta = text[len(stream_buffer) :]
|
||||
stream_buffers[index] = stream_buffer + delta
|
||||
finish_reason = content["meta_info"]["finish_reason"]
|
||||
finish_reason = content["meta_info"].get("finish_reason", None)
|
||||
finish_reason_type = finish_reason["type"] if finish_reason else None
|
||||
|
||||
# If the abort is from scheduler.
|
||||
if finish_reason_type == "abort":
|
||||
code = finish_reason.get(
|
||||
"status_code", HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
error = self.create_streaming_error_response(
|
||||
finish_reason.get("message", "Generation aborted."),
|
||||
code.name,
|
||||
code.value,
|
||||
)
|
||||
yield f"data: {error}\n\n"
|
||||
break
|
||||
|
||||
choice_data = CompletionResponseStreamChoice(
|
||||
index=index,
|
||||
text=delta,
|
||||
logprobs=logprobs,
|
||||
finish_reason=finish_reason["type"] if finish_reason else None,
|
||||
finish_reason=finish_reason_type,
|
||||
matched_stop=(
|
||||
finish_reason["matched"]
|
||||
if finish_reason and "matched" in finish_reason
|
||||
|
||||
Reference in New Issue
Block a user