diff --git a/middleware/anthropic.go b/middleware/anthropic.go index f697f4078..ff55b6ebf 100644 --- a/middleware/anthropic.go +++ b/middleware/anthropic.go @@ -118,6 +118,9 @@ func AnthropicMessagesMiddleware() gin.HandlerFunc { return } + // Set think to nil when being used with Anthropic API to connect to tools like claude code + c.Set("relax_thinking", true) + var b bytes.Buffer if err := json.NewEncoder(&b).Encode(chatReq); err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, anthropic.NewError(http.StatusInternalServerError, err.Error())) diff --git a/middleware/anthropic_test.go b/middleware/anthropic_test.go index 40df7fbb4..a913fd3c4 100644 --- a/middleware/anthropic_test.go +++ b/middleware/anthropic_test.go @@ -582,3 +582,26 @@ func TestAnthropicWriter_ErrorFromRoutes(t *testing.T) { }) } } + +func TestAnthropicMessagesMiddleware_SetsRelaxThinkingFlag(t *testing.T) { + gin.SetMode(gin.TestMode) + + var flagSet bool + router := gin.New() + router.Use(AnthropicMessagesMiddleware()) + router.POST("/v1/messages", func(c *gin.Context) { + _, flagSet = c.Get("relax_thinking") + c.Status(http.StatusOK) + }) + + body := `{"model": "test-model", "max_tokens": 100, "messages": [{"role": "user", "content": "Hi"}]}` + req, _ := http.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + + resp := httptest.NewRecorder() + router.ServeHTTP(resp, req) + + if !flagSet { + t.Error("expected relax_thinking flag to be set in context") + } +} diff --git a/server/routes.go b/server/routes.go index 7748e7629..aad1c8c01 100644 --- a/server/routes.go +++ b/server/routes.go @@ -2072,8 +2072,14 @@ func (s *Server) ChatHandler(c *gin.Context) { } } else { if req.Think != nil && req.Think.Bool() { - c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support thinking", req.Model)}) - return + // Set think to nil when being used with Anthropic API to connect to tools like claude code + if _, ok := c.Get("relax_thinking"); ok { + slog.Warn("model does not support thinking, relaxing thinking to nil", "model", req.Model) + req.Think = nil + } else { + c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support thinking", req.Model)}) + return + } } }