Skip to content

The port 49999 not open error

This error does not mean that you should expose a sandbox port publicly. The Code Interpreter service listens on port 49999 inside the sandbox. The runCode and run_code methods use this service to execute code.

The error occurs when a custom template starts from a generic image that does not contain the Code Interpreter service or its start command. Exposing the port does not fix the missing service.

How to fix it

Base your custom template on the managed code-interpreter template. It already includes the execution service, its start command, and the readiness check.

The important line in the following example is fromTemplate("code-interpreter"). The other calls demonstrate that you can install a package on top, build your own template, and still execute code:

mjs
const templateName = process.env.TEMPLATE_NAME ?? "custom-code-interpreter";
const template = Template()
  .fromTemplate("code-interpreter")
  .pipInstall("humanize==4.13.0");

await Template.build(template, templateName);

const sandbox = await Sandbox.create(templateName);
try {
  const execution = await sandbox.runCode(
    "import humanize; humanize.intcomma(1234567)",
  );
  console.log(execution.text);
} finally {
  await sandbox.kill();
}

If you do not need Code Interpreter, use a regular sandbox and run programs as commands. Use the managed template, or a template derived from it, with the Code Interpreter SDK.