From abf8e8e9c81c00270876c250ad886dfc4a20c8b2 Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Fri, 13 Mar 2026 14:50:49 -0700 Subject: [PATCH] middleware: handle non-JSON error responses gracefully (#14828) writeError in both OpenAI and Anthropic middleware writers would return a raw json.SyntaxError when the error payload wasn't valid JSON (e.g. "invalid character 'e' looking for beginning of value"). Fall back to using the raw bytes as the error message instead. Also use the actual HTTP status code rather than hardcoding 500, so error types map correctly --- middleware/anthropic.go | 7 ++++--- middleware/openai.go | 10 +++++----- middleware/openai_test.go | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/middleware/anthropic.go b/middleware/anthropic.go index 7a064e14..6e1f66ea 100644 --- a/middleware/anthropic.go +++ b/middleware/anthropic.go @@ -34,12 +34,13 @@ func (w *AnthropicWriter) writeError(data []byte) (int, error) { Error string `json:"error"` } if err := json.Unmarshal(data, &errData); err != nil { - return 0, err + // If the error response isn't valid JSON, use the raw bytes as the + // error message rather than surfacing a confusing JSON parse error. + errData.Error = string(data) } w.ResponseWriter.Header().Set("Content-Type", "application/json") - err := json.NewEncoder(w.ResponseWriter).Encode(anthropic.NewError(w.Status(), errData.Error)) - if err != nil { + if err := json.NewEncoder(w.ResponseWriter).Encode(anthropic.NewError(w.Status(), errData.Error)); err != nil { return 0, err } diff --git a/middleware/openai.go b/middleware/openai.go index ca694eb2..8527f00d 100644 --- a/middleware/openai.go +++ b/middleware/openai.go @@ -54,14 +54,14 @@ type EmbedWriter struct { func (w *BaseWriter) writeError(data []byte) (int, error) { var serr api.StatusError - err := json.Unmarshal(data, &serr) - if err != nil { - return 0, err + if err := json.Unmarshal(data, &serr); err != nil { + // If the error response isn't valid JSON, use the raw bytes as the + // error message rather than surfacing a confusing JSON parse error. + serr.ErrorMessage = string(data) } w.ResponseWriter.Header().Set("Content-Type", "application/json") - err = json.NewEncoder(w.ResponseWriter).Encode(openai.NewError(http.StatusInternalServerError, serr.Error())) - if err != nil { + if err := json.NewEncoder(w.ResponseWriter).Encode(openai.NewError(w.ResponseWriter.Status(), serr.Error())); err != nil { return 0, err } diff --git a/middleware/openai_test.go b/middleware/openai_test.go index 06c7ada5..39fc2c90 100644 --- a/middleware/openai_test.go +++ b/middleware/openai_test.go @@ -1222,7 +1222,7 @@ func TestRetrieveMiddleware(t *testing.T) { "code": null, "message": "model not found", "param": null, - "type": "api_error" + "type": "invalid_request_error" } }`, },