Debugging the Algorithm

Debugging the Algorithm

You've written your first complex Algorithm in a single sitting, and it compiles perfectly on the first try, right? Unfortunately, our first attempt at writing code is rarely perfect; there's usually some bug that we need to track down and fix. On our local machine, we have advanced IDEs and debugging tools to help. But how do we debug code running in a remote serverless environment?

Algorithmia's Web IDE provides a basic editing environment, and when you build or run your Algorithm, it will provide a stacktrace at the bottom of the screen if there is a problem. This can sometimes be hard to read in that small window, so copy it into a text editor to see it better. You problem might be as simple as a typo or missing import statement, or it may simply be that you provided invalid input to the console, either forgetting to double-quote your text or providing the wrong datatype (often indicated by "JSON SyntaxError" or "Expected STRING but was ____" in the output).

First, check the basics: make sure you have clicked "Build" after your last change, then provide some console input and read through the stack trace carefully to find the lines of code that caused the problem -- specifically looking for line numbers in your Algorithm's main file. Isolate the offending section, and if necessary, alter your apply() method temporarily, to directly test out that section of code. If the error indicates a missing library/module, click the Dependencies button and verify that the library is there, with the version number you expect.

Since the Web IDE doesn't have an attachable debugger to inspect the state of variables during execution, you'll need to use print() statements to show variable values in the console during execution. If they aren't showing up or don't look right, short-circuit your Algorithm by returning early, sending back the variables you want to examine so they show up as the results of the apply() call -- just don't forget to change things back after.

# Example: returning early to examine variable state

# Normal code:
def apply(input):
foo = some_calculation(input['i'])
bar = other_calculation(foo)
return bar

# Temporarily change it to:
def apply(input):
foo = some_calculation(input['i'])
return foo
# bar = other_calculation(foo)
# return bar

If the solution is still unclear, try running the same code locally. If you git clone (or manually copy) the code onto your own computer and call the apply() method, does it work? If so, do your local dependencies exactly match the ones specified on the Algorithm? For example, run pip freeze in Python or sessionInfo() in R to get exact version numbers, then compare them to the ones shown via the Dependencies button in the Web IDE.

Also take a look at 10 Debugging Tips for Beginners. Even if you're an experienced coder, this article is a great refresher on how to attack a problem without the aid of a purpose-built debugging tool.

If you've tried all these steps and are still stuck, it may be time to get help from Algorithmia's support specialists. Look for the "Help" icon in the lower-right corner and let us know the details of your problem, including the name of the Algorithm you're editing, the output you're seeing, and the steps you've taken so far. We're here to help you succeed!

Ready to continue? Let's move on to publishing your algorithm.