diff --git a/server/routes.go b/server/routes.go index c7052f1b7..933c3e846 100644 --- a/server/routes.go +++ b/server/routes.go @@ -63,6 +63,15 @@ func shouldUseHarmony(model *Model) bool { return false } +func shouldForceThinking(model *Model) bool { + if model.Config.ModelFamily == "qwen3" && strings.Contains(model.Name, "thinking-2507") { + // Force thinking mode for the newer qwen3 thinking models otherwise they end up + // leaving the thinking trace in the return content w/ unmatched tags + return true + } + return false +} + func experimentEnabled(name string) bool { return slices.Contains(strings.Split(os.Getenv("OLLAMA_EXPERIMENT"), ","), name) } @@ -330,7 +339,10 @@ func (s *Server) GenerateHandler(c *gin.Context) { if req.Suffix != "" { caps = append(caps, model.CapabilityInsert) } - if req.Think != nil && req.Think.Bool() { + if req.Think == nil && shouldForceThinking(m) { + req.Think = &api.ThinkValue{Value: true} + caps = append(caps, model.CapabilityThinking) + } else if req.Think != nil && req.Think.Bool() { caps = append(caps, model.CapabilityThinking) // TODO(drifkin): consider adding a warning if it's false and the model // doesn't support thinking. It's not strictly required, but it can be a @@ -1871,7 +1883,10 @@ func (s *Server) ChatHandler(c *gin.Context) { if len(req.Tools) > 0 { caps = append(caps, model.CapabilityTools) } - if req.Think != nil && req.Think.Bool() { + if req.Think == nil && shouldForceThinking(m) { + req.Think = &api.ThinkValue{Value: true} + caps = append(caps, model.CapabilityThinking) + } else if req.Think != nil && req.Think.Bool() { caps = append(caps, model.CapabilityThinking) }