From 1dc5f586b952e71178e68cd243645507c921b904 Mon Sep 17 00:00:00 2001 From: newkirk Date: Sun, 17 May 2026 19:57:59 -0400 Subject: [PATCH] fix: improve OpenAI error body capture and log request body on 400 - Use resp.Body() instead of resp.RawBody() for non-streaming error responses - Fall back to RawBody() for streaming responses - Log the full request body on API errors for debugging --- internal/providers/openai.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/providers/openai.go b/internal/providers/openai.go index aa51cb4c..6941b4da 100644 --- a/internal/providers/openai.go +++ b/internal/providers/openai.go @@ -72,11 +72,14 @@ func (p *OpenAIProvider) ChatCompletion(ctx context.Context, req *models.Unified if !resp.IsSuccess() { msg := resp.String() if msg == "" { - if body, err := io.ReadAll(resp.RawBody()); err == nil { - msg = string(body) + if b := resp.Body(); len(b) > 0 { + msg = string(b) } } + // Log the request body for debugging + reqJSON, _ := json.Marshal(body) log.Printf("OpenAI API Error (%d): %s", resp.StatusCode(), msg) + log.Printf("OpenAI request body: %s", string(reqJSON)) return nil, fmt.Errorf("OpenAI API error (%d): %s", resp.StatusCode(), msg) } @@ -182,11 +185,18 @@ func (p *OpenAIProvider) ChatCompletionStream(ctx context.Context, req *models.U if !resp.IsSuccess() { msg := resp.String() if msg == "" { - if body, err := io.ReadAll(resp.RawBody()); err == nil { - msg = string(body) + if b := resp.Body(); len(b) > 0 { + msg = string(b) + } + if msg == "" { + if b, err := io.ReadAll(resp.RawBody()); err == nil { + msg = string(b) + } } } + reqJSON, _ := json.Marshal(body) log.Printf("OpenAI API Error (%d): %s", resp.StatusCode(), msg) + log.Printf("OpenAI request body: %s", string(reqJSON)) return nil, fmt.Errorf("OpenAI API error (%d): %s", resp.StatusCode(), msg) }