From fe9683f58b652e4b9fac5da661f7d8544f31338f Mon Sep 17 00:00:00 2001 From: shuaiyafei <16618929+shuaiyafei@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:48:19 +0800 Subject: [PATCH] fix(model): propagate in-stream provider errors --- model/atlascloud/atlascloud.go | 6 ++ model/internal/openaiapi/stream.go | 6 ++ .../internal/openaiapi/stream_errors_test.go | 65 +++++++++++++++++++ model/openai/openai.go | 6 ++ 4 files changed, 83 insertions(+) create mode 100644 model/internal/openaiapi/stream_errors_test.go diff --git a/model/atlascloud/atlascloud.go b/model/atlascloud/atlascloud.go index 2270b15d52..e9a6715a40 100644 --- a/model/atlascloud/atlascloud.go +++ b/model/atlascloud/atlascloud.go @@ -354,6 +354,9 @@ func (s *atlasStream) Recv() (*model.Response, error) { return nil, io.EOF } var chunk struct { + Error *struct { + Message string `json:"message"` + } `json:"error"` Choices []struct { Delta struct { Content string `json:"content"` @@ -368,6 +371,9 @@ func (s *atlasStream) Recv() (*model.Response, error) { if err := json.Unmarshal([]byte(data), &chunk); err != nil { return nil, fmt.Errorf("failed to parse stream chunk: %w", err) } + if chunk.Error != nil { + return nil, fmt.Errorf("stream: %s", chunk.Error.Message) + } if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" { return &model.Response{Reply: chunk.Choices[0].Delta.Content}, nil } diff --git a/model/internal/openaiapi/stream.go b/model/internal/openaiapi/stream.go index aee191724c..3cc07c4ef9 100644 --- a/model/internal/openaiapi/stream.go +++ b/model/internal/openaiapi/stream.go @@ -76,6 +76,9 @@ func (s *StreamReader) Recv() (*model.Response, error) { return nil, io.EOF } var chunk struct { + Error *struct { + Message string `json:"message"` + } `json:"error"` Choices []struct { FinishReason string `json:"finish_reason"` Delta struct { @@ -91,6 +94,9 @@ func (s *StreamReader) Recv() (*model.Response, error) { if err := json.Unmarshal([]byte(data), &chunk); err != nil { return nil, fmt.Errorf("failed to parse stream chunk: %w", err) } + if chunk.Error != nil { + return nil, fmt.Errorf("stream: %s", chunk.Error.Message) + } response := &model.Response{} if len(chunk.Choices) > 0 { choice := chunk.Choices[0] diff --git a/model/internal/openaiapi/stream_errors_test.go b/model/internal/openaiapi/stream_errors_test.go new file mode 100644 index 0000000000..8f7deb178a --- /dev/null +++ b/model/internal/openaiapi/stream_errors_test.go @@ -0,0 +1,65 @@ +package openaiapi_test + +import ( + "context" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "go-micro.dev/v6/model" + _ "go-micro.dev/v6/model/atlascloud" + _ "go-micro.dev/v6/model/groq" + _ "go-micro.dev/v6/model/litellm" + _ "go-micro.dev/v6/model/minimax" + _ "go-micro.dev/v6/model/mistral" + _ "go-micro.dev/v6/model/openai" + _ "go-micro.dev/v6/model/openrouter" + _ "go-micro.dev/v6/model/together" +) + +func TestStreamProvidersPropagateSSEErrors(t *testing.T) { + providers := []string{"atlascloud", "groq", "litellm", "minimax", "mistral", "openai", "openrouter", "together"} + for _, provider := range providers { + t.Run(provider, func(t *testing.T) { + for _, afterContent := range []bool{false, true} { + name := "before_content" + if afterContent { + name = "after_content" + } + t.Run(name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + if afterContent { + _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}]}\n\n") + _, _ = io.WriteString(w, "data: {\"error\":{\"message\":\"upstream overloaded\",\"code\":502},\"choices\":[{\"delta\":{},\"finish_reason\":\"error\"}]}\n\n") + } else { + _, _ = io.WriteString(w, "data: {\"error\":{\"message\":\"upstream overloaded\",\"code\":502}}\n\n") + } + _, _ = io.WriteString(w, "data: [DONE]\n\n") + })) + defer server.Close() + + m := model.New(provider, model.WithAPIKey("test-key"), model.WithBaseURL(server.URL)) + stream, err := m.Stream(context.Background(), &model.Request{Prompt: "Hello"}) + if err != nil { + t.Fatalf("Stream returned error before reading the SSE event: %v", err) + } + defer stream.Close() + if afterContent { + chunk, err := stream.Recv() + if err != nil || chunk.Reply != "partial" { + t.Fatalf("first chunk = %#v, %v; want partial", chunk, err) + } + } + response, err := stream.Recv() + if response != nil || err == nil || errors.Is(err, io.EOF) || !strings.Contains(err.Error(), "upstream overloaded") { + t.Fatalf("Recv = %#v, %v; want the provider error", response, err) + } + }) + } + }) + } +} diff --git a/model/openai/openai.go b/model/openai/openai.go index 8efd22b95e..9ec703c0a0 100644 --- a/model/openai/openai.go +++ b/model/openai/openai.go @@ -122,6 +122,9 @@ func (s *openAIStream) Recv() (*model.Response, error) { return nil, io.EOF } var chunk struct { + Error *struct { + Message string `json:"message"` + } `json:"error"` Choices []struct { FinishReason string `json:"finish_reason"` Delta struct { @@ -137,6 +140,9 @@ func (s *openAIStream) Recv() (*model.Response, error) { if err := json.Unmarshal([]byte(data), &chunk); err != nil { return nil, fmt.Errorf("failed to parse stream chunk: %w", err) } + if chunk.Error != nil { + return nil, fmt.Errorf("stream: %s", chunk.Error.Message) + } response := &model.Response{} if len(chunk.Choices) > 0 { choice := chunk.Choices[0]