mirror of
https://github.com/ollama/ollama.git
synced 2026-12-04 00:21:20 +00:00
Merge pull request #20105 from ollama/fix/openai-midstream-errors
openai: emit SSE errors when completion streams fail
This commit is contained in:
2 files changed
+85
No files matched your search
@@ -77,7 +77,27 @@ func (w *BaseWriter) writeError(data []byte) (int, error) {
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func (w *BaseWriter) writeMidStreamError(message string, dataLen int) (int, error) {
|
||||
d, err := json.Marshal(openai.NewError(http.StatusInternalServerError, message))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err := w.ResponseWriter.Write([]byte(fmt.Sprintf("data: %s\n\n", d))); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err := w.ResponseWriter.Write([]byte("data: [DONE]\n\n")); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return dataLen, nil
|
||||
}
|
||||
|
||||
func (w *ChatWriter) writeResponse(data []byte) (int, error) {
|
||||
if w.stream {
|
||||
if message, ok := streamErrorMessage(data); ok {
|
||||
return w.writeMidStreamError(message, len(data))
|
||||
}
|
||||
}
|
||||
|
||||
var chatResponse api.ChatResponse
|
||||
err := json.Unmarshal(data, &chatResponse)
|
||||
if err != nil {
|
||||
@@ -184,6 +204,12 @@ func (w *ChatWriter) Write(data []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (w *CompleteWriter) writeResponse(data []byte) (int, error) {
|
||||
if w.stream {
|
||||
if message, ok := streamErrorMessage(data); ok {
|
||||
return w.writeMidStreamError(message, len(data))
|
||||
}
|
||||
}
|
||||
|
||||
var generateResponse api.GenerateResponse
|
||||
err := json.Unmarshal(data, &generateResponse)
|
||||
if err != nil {
|
||||
|
||||
@@ -284,6 +284,65 @@ func TestChatWriter_StreamSingleChunkPathStillEmitsOneChunk(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAIStreamMidStreamErrorEmitsErrorAndDone(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
writers := []struct {
|
||||
name string
|
||||
newWriter func(gin.ResponseWriter) io.Writer
|
||||
firstChunk string
|
||||
}{
|
||||
{
|
||||
name: "chat",
|
||||
newWriter: func(w gin.ResponseWriter) io.Writer {
|
||||
return &ChatWriter{stream: true, id: "chatcmpl-test", BaseWriter: BaseWriter{ResponseWriter: w}}
|
||||
},
|
||||
firstChunk: `{"model":"test-model","message":{"content":"partial"}}`,
|
||||
},
|
||||
{
|
||||
name: "completion",
|
||||
newWriter: func(w gin.ResponseWriter) io.Writer {
|
||||
return &CompleteWriter{stream: true, id: "cmpl-test", BaseWriter: BaseWriter{ResponseWriter: w}}
|
||||
},
|
||||
firstChunk: `{"model":"test-model","response":"partial"}`,
|
||||
},
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
payload string
|
||||
message string
|
||||
}{
|
||||
{"message", `{"error":"generation failed"}`, "generation failed"},
|
||||
{"empty message", `{"error":""}`, ""},
|
||||
}
|
||||
for _, writer := range writers {
|
||||
for _, tc := range cases {
|
||||
t.Run(writer.name+"/"+tc.name, func(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
context, _ := gin.CreateTestContext(recorder)
|
||||
w := writer.newWriter(context.Writer)
|
||||
if _, err := w.Write([]byte(writer.firstChunk)); err != nil {
|
||||
t.Fatalf("write chunk: %v", err)
|
||||
}
|
||||
if _, err := w.Write([]byte(tc.payload)); err != nil {
|
||||
t.Fatalf("write error payload: %v", err)
|
||||
}
|
||||
|
||||
frames := sseDataFrames(recorder.Body.String())
|
||||
if len(frames) != 3 || frames[2] != "[DONE]" {
|
||||
t.Fatalf("expected content, error and [DONE], got %q", frames)
|
||||
}
|
||||
var errResp openai.ErrorResponse
|
||||
if err := json.Unmarshal([]byte(frames[1]), &errResp); err != nil {
|
||||
t.Fatalf("unmarshal error frame: %v", err)
|
||||
}
|
||||
if errResp.Error.Type != "api_error" || errResp.Error.Message != tc.message {
|
||||
t.Fatalf("expected api_error with message %q, got %+v", tc.message, errResp.Error)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatWriter_StreamMixedThinkingAndToolCallsWithoutDoneEmitsChunksOnly(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
Reference in new issue
Block a user