alibaba/spring-ai-alibaba · error · Exception

Failed to execute code

Error message

Failed to execute code

What it means

executeCodeLocally wraps IOException thrown while spawning the language interpreter process into a plain Exception with message 'Failed to execute code'. Non-zero exit codes are handled separately (returned as CodeExecutionResult), so this error specifically means the process could not be started or I/O with it failed.

Source

Thrown at spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/node/code/LocalCommandlineCodeExecutor.java:164

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
		executor.setStreamHandler(new PumpStreamHandler(outputStream, errorStream));

		// Set timeout
		executor.setWatchdog(new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(config.getTimeout())));

		try {
			executor.execute(commandLine);
			return new CodeExecutionResult(0, outputStream.toString().trim());
		}
		catch (ExecuteException e) {
			String errorOutput = errorStream.toString()
				.replace(Path.of(workDir).toAbsolutePath() + File.separator, "")
				.trim();
			return new CodeExecutionResult(e.getExitValue(), errorOutput);
		}
		catch (IOException e) {
			throw new Exception("Failed to execute code", e);
		}
		finally {
			// Cleanup Java class files
			if ("java".equals(language)) {
				FileUtils.deleteFile(workDir, filename.replace(".java", ".class"));
			}
		}
	}

}

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Check the cause: 'Cannot run program ...' means the interpreter is missing — install it or add it to PATH.
  2. Verify the configured workDir (CodeExecutionConfig.getWorkDir()) exists and is writable.
  3. Test manually that '<interpreter> --version' works in the same environment/JVM process.
  4. Use DockerCodeExecutor for hermetic environments instead of relying on host tooling.

Example fix

// before
new LocalCommandlineCodeExecutor().executeCodeBlocks(blocks, config); // 'Failed to execute code'
// after
File workDirFile = new File(config.getWorkDir());
if (!workDirFile.exists()) workDirFile.mkdirs(); // and ensure python/node on PATH before executing
Defensive patterns

Strategy: validation

Validate before calling

String exe = CodeUtils.getExecutableForLanguage(language); // throws if unsupported
File wd = new File(config.getWorkDir());
if (!wd.isDirectory()) throw new IllegalStateException("workDir missing: " + wd);
if (new ProcessBuilder(exe, "--version").start().waitFor() != 0) throw new IllegalStateException(exe + " not runnable");

Try / catch

try {
    return executor.executeCodeBlocks(blocks, config);
} catch (Exception e) {
    if (e.getCause() instanceof IOException ioe && ioe.getMessage().contains("Cannot run program")) {
        throw new IllegalStateException("Interpreter not installed or not on PATH", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Running a code block locally when the interpreter executable (python/node/java/sh) is not on PATH, the work directory does not exist or is unreadable, or reading the process streams fails mid-execution.

Common situations: Python 3 installed as 'python3' but configured environment uses a minimal container without it; Node not installed; workDir points to a deleted/nonexistent directory; wrong PATH in the service runtime vs. the developer shell.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/159ccc48241ae8bb. Report an issue: GitHub.