Skip to content

Commit aaacee4

Browse files
author
Piotr Stankiewicz
committed
Return error in case of runner crash
In case the runner crashes it would be nice to return an error to the user. So, add an error handler on the proxy and use it to try to figure out if the runner crashed and format the response error accordingly. Signed-off-by: Piotr Stankiewicz <piotr.stankiewicz@docker.com>
1 parent 64153a7 commit aaacee4

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

pkg/inference/scheduling/api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ type OpenAIInferenceRequest struct {
4444
Model string `json:"model"`
4545
}
4646

47+
// OpenAIErrorResponse is used to format an OpenAI API compatible error response
48+
// (see https://platform.openai.com/docs/api-reference/responses-streaming/error)
49+
type OpenAIErrorResponse struct {
50+
Type string `json:"type"` // always "error"
51+
Code *string `json:"code"`
52+
Message string `json:"message"`
53+
Param *string `json:"param"`
54+
SequenceNumber int `json:"sequence_number"`
55+
}
56+
4757
// BackendStatus represents information about a running backend
4858
type BackendStatus struct {
4959
// BackendName is the name of the backend

pkg/inference/scheduling/runner.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scheduling
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
@@ -143,11 +144,24 @@ func run(
143144
w.WriteHeader(http.StatusInternalServerError)
144145
select {
145146
case <-r.done:
147+
res := OpenAIErrorResponse{
148+
Type: "error",
149+
Code: nil,
150+
Message: r.err.Error(),
151+
Param: nil,
152+
SequenceNumber: 1,
153+
}
154+
errJson, err := json.Marshal(&res)
155+
if err == nil {
156+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
157+
w.Write(errJson)
158+
}
146159
return
147160
case <-time.After(30 * time.Second):
148161
}
149162
} else {
150163
w.WriteHeader(http.StatusBadGateway)
164+
151165
}
152166
}
153167

0 commit comments

Comments
 (0)