diff --git a/internal/llm/openai.go b/internal/llm/openai.go index 35fa337..604fee0 100644 --- a/internal/llm/openai.go +++ b/internal/llm/openai.go @@ -43,6 +43,35 @@ func (c *openAIClient) Vision(ctx context.Context, prompt string, images []Image "temperature": opts.Temperature, "messages": []map[string]any{{"role": "user", "content": content}}, } + + b, err := json.Marshal(reqBody) + if err != nil { + return "", err + } + + status, raw, err := c.postJSONRaw(ctx, "/v1/chat/completions", b) + if err != nil { + return "", err + } + + // Self-heal: newer OpenAI models reject max_tokens; retry with max_completion_tokens. + if status == http.StatusBadRequest && bytes.Contains(raw, []byte("max_completion_tokens")) { + reqBody["max_completion_tokens"] = reqBody["max_tokens"] + delete(reqBody, "max_tokens") + b, err = json.Marshal(reqBody) + if err != nil { + return "", err + } + status, raw, err = c.postJSONRaw(ctx, "/v1/chat/completions", b) + if err != nil { + return "", err + } + } + + if status >= 300 { + return "", fmt.Errorf("openai HTTP %d: %s", status, string(raw)) + } + var out struct { Choices []struct { Message struct { @@ -50,7 +79,7 @@ func (c *openAIClient) Vision(ctx context.Context, prompt string, images []Image } `json:"message"` } `json:"choices"` } - if err := c.postJSON(ctx, "/v1/chat/completions", reqBody, &out); err != nil { + if err := json.Unmarshal(raw, &out); err != nil { return "", err } if len(out.Choices) == 0 { @@ -59,6 +88,7 @@ func (c *openAIClient) Vision(ctx context.Context, prompt string, images []Image return out.Choices[0].Message.Content, nil } + func (c *openAIClient) Transcribe(ctx context.Context, audio []byte, filename string, opts Options) (string, error) { model := c.audioModel if opts.Model != "" { @@ -116,14 +146,10 @@ func (c *openAIClient) Transcribe(ctx context.Context, audio []byte, filename st return out.Text, nil } -func (c *openAIClient) postJSON(ctx context.Context, path string, body, out any) error { - b, err := json.Marshal(body) - if err != nil { - return err - } +func (c *openAIClient) postJSONRaw(ctx context.Context, path string, b []byte) (int, []byte, error) { req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.base+path, bytes.NewReader(b)) if err != nil { - return err + return 0, nil, err } req.Header.Set("Content-Type", "application/json") if c.key != "" { @@ -131,15 +157,15 @@ func (c *openAIClient) postJSON(ctx context.Context, path string, body, out any) } resp, err := c.http.Do(req) if err != nil { - return err + return 0, nil, err } defer func() { _ = resp.Body.Close() }() raw, err := io.ReadAll(resp.Body) if err != nil { - return err + return resp.StatusCode, nil, err } - if resp.StatusCode >= 300 { - return fmt.Errorf("openai HTTP %d: %s", resp.StatusCode, string(raw)) - } - return json.Unmarshal(raw, out) + return resp.StatusCode, raw, nil } + + + diff --git a/internal/llm/openai_test.go b/internal/llm/openai_test.go index 723d00a..8e7015a 100644 --- a/internal/llm/openai_test.go +++ b/internal/llm/openai_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "strings" + "sync/atomic" "testing" "time" ) @@ -35,6 +36,46 @@ func TestOpenAIVision(t *testing.T) { t.Fatalf("got %q err %v", got, err) } } +func TestOpenAIVisionMaxCompletionTokensRetry(t *testing.T) { + var reqCount atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/chat/completions" { + t.Errorf("path %s", r.URL.Path) + } + reqCount.Add(1) + body, err := io.ReadAll(r.Body) + if err != nil { + t.Errorf("read body: %v", err) + return + } + bodyStr := string(body) + if strings.Contains(bodyStr, `"max_tokens"`) && !strings.Contains(bodyStr, `"max_completion_tokens"`) { + w.WriteHeader(http.StatusBadRequest) + _, _ = io.WriteString(w, `{"error":{"message":"Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.","type":"invalid_request_error","code":"unsupported_parameter"}}`) + return + } + if strings.Contains(bodyStr, `"max_completion_tokens"`) { + _, _ = io.WriteString(w, `{"choices":[{"message":{"content":"ok"}}]}`) + return + } + w.WriteHeader(http.StatusBadRequest) + _, _ = io.WriteString(w, `{"error":"unexpected body"}`) + })) + defer srv.Close() + + c := &openAIClient{http: &http.Client{Timeout: 5 * time.Second}, base: srv.URL, key: "key", model: "gpt-5", maxTokens: 100} + got, err := c.Vision(context.Background(), "read", []Image{{MediaType: "image/png", Data: []byte{1}}}, Options{}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != "ok" { + t.Fatalf("got %q, want %q", got, "ok") + } + if n := reqCount.Load(); n != 2 { + t.Fatalf("expected 2 requests, got %d", n) + } +} + const maxMultipartMem = 1 << 20 // 1 MiB — bounded, used only in tests